(code: string, path: string)
| 25 | export const INVALID_YAML_ERROR_STRING = "is not a valid YAML file"; |
| 26 | |
| 27 | export function compile(code: string, path: string): string { |
| 28 | if (Path.fileExtension(path) === "sqlx") { |
| 29 | return compileSqlx(SyntaxTreeNode.create(code), path); |
| 30 | } |
| 31 | if (Path.fileExtension(path) === "yaml" || Path.fileExtension(path) === "yml") { |
| 32 | try { |
| 33 | const yamlAsJson = loadYaml(code); |
| 34 | return `exports.asJson = ${JSON.stringify(yamlAsJson)}`; |
| 35 | } catch (e) { |
| 36 | if (e instanceof YAMLException) { |
| 37 | throw new Error(`${path} ${INVALID_YAML_ERROR_STRING}: ${e}`); |
| 38 | } |
| 39 | throw e; |
| 40 | } |
| 41 | } |
| 42 | if (Path.fileExtension(path) === "ipynb") { |
| 43 | let codeAsJson = {}; |
| 44 | try { |
| 45 | codeAsJson = JSON.parse(code); |
| 46 | } catch (e) { |
| 47 | throw new Error(`Error parsing ${path} as JSON: ${e}`); |
| 48 | } |
| 49 | const notebookAsJson = JSON.stringify(codeAsJson); |
| 50 | return `exports.asJson = ${notebookAsJson}`; |
| 51 | } |
| 52 | if (Path.fileExtension(path) === "sql") { |
| 53 | const escapedCode = code |
| 54 | .replace(/\\/g, "\\\\") |
| 55 | .replace(/`/g, "\\`") |
| 56 | .replace(/\${/g, "\\${"); |
| 57 | return `exports.query = \`${escapedCode}\`;`; |
| 58 | } |
| 59 | if (Path.fileExtension(path) === "md") { |
| 60 | const contents = code |
| 61 | .replace(/\r\n/g, "\n") |
| 62 | .replace(/\\/g, "\\\\") |
| 63 | .replace(/`/g, "\\`") |
| 64 | .replace(/\${/g, "\\${"); |
| 65 | return `exports.contents = \`${contents}\`;`; |
| 66 | } |
| 67 | return code; |
| 68 | } |
| 69 | |
| 70 | export function extractJsBlocks(code: string): { sql: string; js: string } { |
| 71 | const JS_REGEX = /^\s*\/\*[jJ][sS]\s*[\r\n]+((?:[^*]|[\r\n]|(?:\*+(?:[^*/]|[\r\n])))*)\*+\/|^\s*\-\-[jJ][sS]\s(.*)/gm; |
no test coverage detected