JSTQL-JS-Transform/index.ts

56 lines
1.5 KiB
TypeScript
Raw Normal View History

2024-06-13 18:23:15 +00:00
import {
2024-06-13 18:23:15 +00:00
transform,
2024-05-31 14:26:18 +00:00
} from "./src/transform/transform";
import { parseJSTQL } from "./src/langium/langiumRunner";
2024-05-31 14:09:49 +00:00
import { parseArgs } from "util";
2024-05-31 14:09:49 +00:00
const { values: argVals, tokens: positional } = parseArgs({
2024-05-31 14:26:18 +00:00
options: {
o: {
type: "string",
},
},
2024-05-31 14:09:49 +00:00
tokens: true,
allowPositionals: true,
});
const main = async () => {
//transform(selfHostedTransformExampleMultiStmt, codeFromFile);
2024-05-31 14:09:49 +00:00
console.log(positional);
2024-05-31 14:26:18 +00:00
console.log(argVals);
if (!positional) {
throw new Error("Something is wrong with args");
}
if (
!positional[0] &&
positional[0].kind === "positional" &&
!positional[0].value.endsWith(".jstql")
) {
2024-05-31 14:09:49 +00:00
throw new Error("First positional argument is current JSTQL file");
}
if (!positional[1] || !positional[1].value.endsWith(".js")) {
throw new Error(
"Second positional argument is JS code to be transformed"
);
}
const jstql_file = positional[0].value;
const code_file = positional[1].value;
2024-05-31 14:09:49 +00:00
let jstqlString = await Bun.file(jstql_file).text();
let codeString = await Bun.file(code_file).text();
2024-05-12 18:06:37 +00:00
2024-05-31 14:09:49 +00:00
let parsedJSTQL = await parseJSTQL(jstqlString);
2024-05-31 14:09:49 +00:00
for (let proposal of parsedJSTQL) {
let [resultString, matches] = transform(proposal.cases, codeString);
2024-05-31 14:26:18 +00:00
let path = "./out.js";
2024-05-31 14:09:49 +00:00
if (argVals["o"]) {
path = argVals["o"];
}
2024-05-31 14:26:18 +00:00
await Bun.write(path, resultString);
}
2023-12-11 18:38:30 +00:00
};
main();