-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.ts
181 lines (163 loc) · 5.39 KB
/
cli.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#!/usr/bin/env node
import fs from "node:fs";
import path from "node:path";
import { fileURLToPath } from "node:url";
import { build, run } from "./lib/index.js";
const __dirname = path.dirname(fileURLToPath(import.meta.url));
type ParsedArgs = {
command: string | undefined;
filePath: string | undefined;
outputDirectory: string | undefined;
args: string[];
nodeArgs: string[];
};
// Helper function to parse arguments
function parseArgs(): ParsedArgs {
const result: ParsedArgs = {
command: undefined,
filePath: undefined,
outputDirectory: undefined,
args: [],
nodeArgs: [],
};
let args = process.argv.slice(2);
// --nodeargs
const nodeArgsFlagIndex = args.indexOf("--nodeargs");
if (nodeArgsFlagIndex !== -1) {
let hyphenHyphenIndex = args.indexOf("--", nodeArgsFlagIndex);
if (hyphenHyphenIndex === -1) hyphenHyphenIndex = args.length;
result.nodeArgs = args.slice(nodeArgsFlagIndex + 1, hyphenHyphenIndex);
if (!result.nodeArgs[0]?.startsWith("-")) {
console.error(
`Error: Expected --nodeargs args to start with '-', found '${result.nodeArgs[0]}'.`
);
console.error("Run 'xnr --help' for usage information.");
process.exit(1);
}
args = [...args.slice(0, nodeArgsFlagIndex), ...args.slice(hyphenHyphenIndex + 1)];
}
// --outdir
const outputDirectoryFlagIndex = args.indexOf("--outdir");
if (outputDirectoryFlagIndex !== -1) {
result.outputDirectory = args[outputDirectoryFlagIndex + 1];
args = [
...args.slice(0, outputDirectoryFlagIndex),
...args.slice(outputDirectoryFlagIndex + 2),
];
}
const commandOrFilePath = args.shift();
if (commandOrFilePath === "run" || commandOrFilePath === "build") {
result.command = commandOrFilePath;
result.filePath = args.shift();
} else if (commandOrFilePath?.startsWith("-")) {
handleOtherArgFound(commandOrFilePath);
} else {
result.command = "run";
result.filePath = commandOrFilePath;
}
if (result.command === "build" && !result.outputDirectory) {
result.outputDirectory = args.shift();
}
result.args = args;
return result;
}
// Handle the build command
async function handleBuild({ filePath, outputDirectory }: ParsedArgs) {
if (!filePath) {
console.error("Error: You must specify <filePath> for the 'build' command.");
console.error("Run 'xnr --help' for usage information.");
process.exit(1);
}
if (filePath.startsWith("-")) {
console.error(`Error: Unexpected flag '${filePath}'.`);
console.error("Run 'xnr --help' for usage information.");
process.exit(1);
}
if (!outputDirectory) {
console.error("Error: You must specify <outputDirectory> for the 'build' command.");
console.error("Run 'xnr --help' for usage information.");
process.exit(1);
}
if (outputDirectory.startsWith("-")) {
handleOtherArgFound(filePath);
}
try {
const result = await build({ filePath, outputDirectory });
console.log(
`Build completed with ${result.files.length} file${
result.files.length === 1 ? "" : "s"
}. Run with:`
);
console.log(` node '${path.relative(process.cwd(), result.entry)}'`);
} catch (error) {
console.error("Build failed:", error);
process.exit(1);
}
}
// Handle the run command
async function handleRun({ filePath, outputDirectory, args, nodeArgs }: ParsedArgs) {
if (!filePath) {
console.error("Error: You must specify <filePath> for the 'run' command.");
console.error("Run 'xnr --help' for usage information.");
process.exit(1);
}
if (filePath.startsWith("-")) {
handleOtherArgFound(filePath);
}
try {
const exitCode = await run({ filePath, outputDirectory, args, nodeArgs });
process.exit(exitCode);
} catch (error) {
console.error("Run failed:", error);
process.exit(1);
}
}
function handleOtherArgFound(arg: string) {
if (arg === "-h" || arg === "--help") {
console.log(
"Usage: xnr [command (default: run)] [...]" +
"\n" +
"\nCommands:" +
"\n run <filePath> [args...]" +
"\n" +
"\n Available options for run:" +
"\n --outdir ./outDir" +
"\n --nodeargs '--inspect' '--max-old-space-size=4096' --" +
"\n (use -- to indicate end of list)" +
"\n" +
"\n build <filePath> <outputDirectory>" +
"\n" +
"\n Available options for build:" +
"\n --outdir ./outDir (required if not specified as the second argument)" +
""
);
process.exit(0);
} else if (arg === "-v" || arg === "--version") {
let dirname = __dirname;
while (dirname !== "/") {
if (fs.existsSync(path.join(dirname, "package.json"))) break;
dirname = path.dirname(dirname);
}
const pkgJson = JSON.parse(fs.readFileSync(path.join(dirname, "package.json"), "utf8")) as {
version: string;
};
console.log(`xnr v${pkgJson.version}`);
process.exit(0);
}
console.error(`Error: Found unknown arg '${arg}'.`);
console.error("Run 'xnr --help' for usage information.");
process.exit(1);
}
// Main function to dispatch commands
async function main() {
const inputArgs = parseArgs();
if (inputArgs.command === "build") {
await handleBuild(inputArgs);
} else if (inputArgs.command === "run") {
await handleRun(inputArgs);
} else {
console.error(`Error: Unknown command '${inputArgs.command}'.`);
process.exit(1);
}
}
await main();