Skip to content

Commit 4016bd8

Browse files
authored
Refactor zsc.js for Bun compatibility and new commands
Updated CLI argument parsing to be Bun-safe and added new package commands.
1 parent 548b548 commit 4016bd8

File tree

1 file changed

+57
-35
lines changed

1 file changed

+57
-35
lines changed

compiler/zsc.js

Lines changed: 57 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -16,40 +16,13 @@ import { SemanticAnalyzer } from "./semantic/SemanticAnalyzer.js";
1616
METADATA
1717
========================= */
1818

19-
const VERSION = "0.3.4";
19+
const VERSION = "0.3.5";
2020

2121
/* =========================
22-
CLI ARG NORMALIZATION
22+
CLI ARG PARSING (BUN-SAFE)
2323
========================= */
2424

25-
/*
26-
Works for:
27-
- bun zsc.js ...
28-
- node zsc.js ...
29-
- zsc.exe ...
30-
*/
31-
32-
let argv = [...process.argv];
33-
34-
// Remove runtime (node / bun)
35-
if (
36-
argv[0].endsWith("node") ||
37-
argv[0].endsWith("node.exe") ||
38-
argv[0].endsWith("bun") ||
39-
argv[0].endsWith("bun.exe")
40-
) {
41-
argv.shift();
42-
}
43-
44-
// Remove script / binary path
45-
if (
46-
argv[0]?.endsWith(".js") ||
47-
argv[0]?.endsWith(".exe") ||
48-
argv[0] === "zsc"
49-
) {
50-
argv.shift();
51-
}
52-
25+
const argv = Bun.argv.slice(2);
5326
const command = argv[0];
5427
const args = argv.slice(1);
5528

@@ -67,18 +40,28 @@ Usage:
6740
zsc build <entry.zs>
6841
zsc run <entry.zs> [-- args...]
6942
43+
Package commands:
44+
zsc add <pkg>
45+
zsc install
46+
zsc remove <pkg>
47+
zsc update
48+
7049
Options:
7150
-h, --help Show this help
7251
-v, --version Show compiler version
7352
74-
Commands:
75-
build Compile project into ./bin/js
76-
run Compile project into ./.zsc-cache and run it
53+
Bun proxy:
54+
zsc bun <args...> Run Bun directly (full Bun CLI)
55+
7756
7857
Examples:
7958
zsc build main.zs
80-
zsc run main.zs
81-
zsc run src/main.zs -- hello 123
59+
zsc run main.zs -- hello 123
60+
zsc add antlr4
61+
zsc bun --help
62+
zsc bun install
63+
zsc bun run index.js
64+
zsc bun add antlr4
8265
`.trim());
8366
}
8467

@@ -100,6 +83,45 @@ if (command === "-v" || command === "--version") {
10083
process.exit(0);
10184
}
10285

86+
/* =========================
87+
BUN PACKAGE COMMAND PROXY
88+
========================= */
89+
90+
const BUN_COMMANDS = new Set([
91+
"add",
92+
"install",
93+
"remove",
94+
"update",
95+
]);
96+
97+
if (BUN_COMMANDS.has(command)) {
98+
const result = spawnSync(
99+
"bun",
100+
[command, ...args],
101+
{ stdio: "inherit" }
102+
);
103+
process.exit(result.status ?? 0);
104+
}
105+
106+
/* =========================
107+
BUN FULL PROXY
108+
========================= */
109+
110+
if (command === "bun") {
111+
const result = spawnSync(
112+
"bun",
113+
args, // forward EVERYTHING after `bun`
114+
{ stdio: "inherit" }
115+
);
116+
process.exit(result.status ?? 0);
117+
}
118+
119+
120+
121+
/* =========================
122+
VALIDATE COMMAND
123+
========================= */
124+
103125
if (command !== "build" && command !== "run") {
104126
console.error(`error: unknown command '${command}'`);
105127
printHelp();

0 commit comments

Comments
 (0)