forked from web-infra-dev/rspack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
x.mjs
executable file
·283 lines (249 loc) · 6.67 KB
/
x.mjs
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
#!/usr/bin/env zx
import "zx/globals";
import { Command } from "commander";
import {
launchJestWithArgs,
launchRspackCli
} from "./scripts/debug/launch.mjs";
import { publish_handler } from "./scripts/release/publish.mjs";
import { version_handler } from "./scripts/release/version.mjs";
process.env.CARGO_TERM_COLOR = "always"; // Assume every terminal that using zx supports color
process.env.FORCE_COLOR = 3; // Fix zx losing color output in subprocesses
const program = new Command();
program
.name("Rspack Development CLI")
.description("CLI for development of Rspack")
.showHelpAfterError(true)
.showSuggestionAfterError(true);
// x ready
program
.command("ready")
.alias("r")
.description("ready to create a pull request, build and run all tests")
.action(async () => {
await $`cargo check`;
await $`cargo lint`;
await $`cargo test`;
await $`pnpm install`;
await $`pnpm run build:cli:release`;
await $`pnpm run test:unit`;
console.log(chalk.green("All passed."));
});
// x install
program
.command("install")
.alias("i")
.description("install all dependencies")
.action(async () => {
await $`pnpm install`;
});
// x clean
const cleanCommand = program
.command("clean")
.description("clean target/ directory");
// x clean all
cleanCommand.command("all").action(async () => {
await $`./x clean rust`;
});
// x clean rust
cleanCommand
.command("rust")
.description("clean target/ directory")
.action(async () => {
await $`cargo clean`;
});
// x build
const buildCommand = program.command("build").alias("b").description("build");
const watchCommand = program.command("watch").alias("w").description("watch");
buildCommand
.option("-a", "build all")
.option("-b", "build rust binding")
.option("-j", "build js packages")
.option("-r", "release")
.option("-f", "force")
.action(async ({ a, b = a, j = a, r, f }) => {
const mode = r ? "release" : "debug";
try {
if (b === undefined && j === undefined) {
b = j = true;
}
b && (await $`pnpm --filter @rspack/binding build:${mode}`);
j && (await $`pnpm --filter "@rspack/*" build ${f ? "--force" : ""}`);
} catch (e) {
process.exit(e.exitCode);
}
});
watchCommand
.option("-a", "watch all")
.option("-b", "watch rust binding")
.option("-j", "watch js packages")
.option("-r", "release")
.action(async ({ a, b = a, j = a, r }) => {
const mode = r ? "release" : "debug";
try {
b && (await $`pnpm --filter @rspack/binding watch:${mode}`);
j && (await $`pnpm --filter "@rspack/*" watch`);
} catch (e) {
process.exit(e.exitCode);
}
});
// x build binding
buildCommand
.command("binding")
.description("build rust binding")
.action(async () => {
await $`pnpm --filter @rspack/binding build:debug`;
});
// x build js
buildCommand
.command("js")
.description("build js packages")
.action(async () => {
await $`pnpm --filter "@rspack/*" build`;
});
// x test
const testCommand = program.command("test").alias("t").description("test");
// x test rust
testCommand
.command("rust")
.description("run cargo tests")
.action(async () => {
await $`cargo test`;
});
// x test unit
testCommand
.command("unit")
.description("run all unit tests")
.action(async () => {
await $`./x build js`;
await $`pnpm --filter "@rspack/*" test`;
});
// x test ci
testCommand
.command("ci")
.description("run tests for ci")
.action(async () => {
await $`./x test unit`;
});
// x test webpack
testCommand
.command("webpack")
.description("run webpack test suites")
.action(async () => {
await $`pnpm --filter "webpack-test" test`;
});
// x test plugin
testCommand
.command("plugin")
.description("run plugin test suites")
.action(async () => {
await $`pnpm --filter "plugin-test" test`;
});
// x api-extractor
const extractorCommand = program
.command("api-extractor")
.alias("ae")
.description("api extractor");
extractorCommand
.command("update")
.description("update api extractor snapshots")
.action(async () => {
await $`pnpm -w build:js`;
await $`pnpm --filter '@rspack/*' api-extractor --local`;
});
extractorCommand
.command("ci")
.description("test api extractor snapshots")
.action(async () => {
try {
await $`pnpm --filter '@rspack/*' api-extractor:ci`;
} catch (e) {
console.error(
`Api-extractor testing failed. Did you forget to update the snapshots locally?
Run the command below locally to fix this error (in the *ROOT* of rspack workspace).
$ ./x api-extractor update`
);
process.exit(e.exitCode);
}
});
// x rspack / x rs
const rspackCommand = program.command("rspack").alias("rs").description(`
$ x rspack -- [your-rspack-cli-args...]
$ x rspack --debug -- build
$ x rs -d -- build
$ x rsd -- build
`);
rspackCommand
.option("-d, --debug", "Launch debugger in VSCode")
.action(async ({ debug }) => {
try {
if (!debug) {
await $`npx rspack ${getVariadicArgs()}`;
return;
}
await launchRspackCli(getVariadicArgs());
} catch (e) {
process.exit(e.exitCode);
}
});
// x rsd
program
.command("rspack-debug")
.alias("rsd")
.description("Alias for `x rspack --debug`")
.action(async () => {
await launchRspackCli(getVariadicArgs());
});
// x jest / x j
const jestCommand = program.command("jest").alias("j").description(`
$ x jest -- [your-jest-args...]
$ x jest --debug -- -t <test-name-pattern>
$ x j -d -- [test-path-pattern]
$ x jd -- [your-jest-args...]
`);
jestCommand
.option("-d, --debug", "Launch debugger in VSCode")
.action(async ({ debug }) => {
if (!debug) {
await $`npx jest ${getVariadicArgs()}`;
return;
}
await launchJestWithArgs(getVariadicArgs());
});
// x jd
program
.command("jest-debug")
.alias("jd")
.description("Alias for `x jest --debug`")
.action(async () => {
await launchJestWithArgs(getVariadicArgs());
});
program
.command("version")
.argument("<bump_version>", "bump version to (major|minor|patch|snapshot)")
.description("bump version")
.action(version_handler);
program
.command("publish")
.argument("<mode>", "publish mode (snapshot|stable)")
.requiredOption("--tag <char>", "publish tag")
.option(
"--dry-run",
"Does everything a publish would do except actually publishing to the registry"
)
.option("--no-dry-run", "negative dry-run")
.option("--push-tags", "push tags to github")
.option("--no-push-tags", "don't push tags to github")
.description("publish package after version bump")
.action(publish_handler);
let argv = process.argv.slice(2); // remove the `node` and script call
if (argv[0] && /x.mjs/.test(argv[0])) {
// Called from `zx x.mjs`
argv = argv.slice(1);
}
program.parse(argv, { from: "user" });
// Get args after `--`
function getVariadicArgs() {
const idx = argv.findIndex(c => c === "--");
return idx === -1 ? [] : argv.slice(idx + 1);
}