-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexecute.ts
67 lines (55 loc) · 1.68 KB
/
execute.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import compile from "./compile";
const yellowBoldTermPrefix = "\x1b[33m\x1b[1m";
const resetStyle = "\x1b[0m";
const yellow = (str) => yellowBoldTermPrefix + str + resetStyle;
const usageInstructions = `
Usage:
chicory [options] [file]
Options:
--compile Compile the file and print the compiled code
--help Print this help message
--version Print the version number
File:
The file to compile/exec
`
const options = Bun.argv.slice(2);
const filePath = options[options.length - 1];
if (options.includes("--help") || options.includes("-h")) {
console.log(usageInstructions);
process.exit(0);
}
if (options.includes("--version") || options.includes("-v")) {
// read from package.json
const packageJson = await Bun.file("package.json").json();
console.log(packageJson.version);
process.exit(0);
}
if (!filePath) {
console.error("No file specified");
process.exit(1);
}
const file = Bun.file(filePath);
const source = await file.text();
console.log(yellow(" ⚡ Compiling Chicory Source ⚡"));
const { code, errors } = compile(source) || { code: "", errors: [] };
if (errors.length > 0) {
console.error(errors.map((error, index) =>
`Error ${index}:\n${JSON.stringify(error.range.start)}\n${
JSON.stringify(error.message)
}`
).join("\n---\n"));
}
// if "--compile" flag is passed, print the compiled code and exit:
if (options.includes("--compile") || options.includes("-c")) {
console.log(yellow(" ⚡ Compiled Code ⚡"));
console.log(code);
process.exit(0);
}
console.log(yellow(" ⚡ Executing ⚡"));
// run the compiled code:
const proc = Bun.spawn(["bun", "run", "-"], {
stdin: "pipe",
stdout: "inherit",
});
proc.stdin.write(code);
proc.stdin.end();