forked from uber-web/jazelle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
408 lines (376 loc) · 14.5 KB
/
index.js
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
// @flow
const {getRootDir} = require('./utils/get-root-dir.js');
const {parse, normalize} = require('./utils/parse-argv.js');
const {cli} = require('./utils/cli.js');
const {version} = require('./commands/version.js');
const {init} = require('./commands/init.js');
const {scaffold} = require('./commands/scaffold.js');
const {install} = require('./commands/install.js');
const {ci} = require('./commands/ci.js');
const {focus} = require('./commands/focus.js');
const {add} = require('./commands/add.js');
const {remove} = require('./commands/remove.js');
const {upgrade} = require('./commands/upgrade.js');
const {purge} = require('./commands/purge.js');
const {check} = require('./commands/check.js');
const {outdated} = require('./commands/outdated.js');
const {resolutions} = require('./commands/resolutions.js');
const {sort} = require('./commands/sort.js');
const {align} = require('./commands/align.js');
const {localize} = require('./commands/localize.js');
const {changes} = require('./commands/changes.js');
const {plan} = require('./commands/plan.js');
const {batch} = require('./commands/batch.js');
const {build} = require('./commands/build.js');
const {dev} = require('./commands/dev.js');
const {test} = require('./commands/test.js');
const {lint} = require('./commands/lint.js');
const {flow} = require('./commands/flow.js');
const {typecheck} = require('./commands/typecheck.js');
const {start} = require('./commands/start.js');
const {node} = require('./commands/node.js');
const {yarn} = require('./commands/yarn.js');
const {exec} = require('./commands/exec.js');
const {each} = require('./commands/each.js');
const {binPath} = require('./commands/bin-path.js');
const {bazel} = require('./commands/bazel.js');
const {bump} = require('./commands/bump.js');
const {doctor} = require('./commands/doctor.js');
const {script} = require('./commands/script.js');
const {
reportMismatchedTopLevelDeps,
} = require('./utils/report-mismatched-top-level-deps.js');
const {getBinaryPath} = require('./utils/binary-paths.js');
const {findChangedTargets} = require('./utils/find-changed-targets.js');
const {getTestGroups} = require('./utils/get-test-groups.js');
/*::
export type RunCLI = (Array<string>) => Promise<void>;
*/
const runCLI /*: RunCLI */ = async argv => {
const [command, ...rest] = argv;
const args = normalize(parse(rest));
await cli(
command,
args,
{
version: [`Display the version number`, version],
init: [
`Initializes a workspace`,
async () => init({cwd: process.cwd()}), // actually runs from bin/cli.sh because it needs to generate Bazel files
],
setup: [
`Installs Jazelle hermetically`, // installation happens in bin/cli.sh, nothing to do here
async () => {},
],
scaffold: [
`Scaffolds a project from a template
--skipPreinstall Skip the preinstall hook
--skipPostinstall Skip the postinstall hook`,
async ({from, to, name, skipPreinstall, skipPostinstall}) =>
scaffold({
root: await rootOf(args),
cwd: process.cwd(),
from,
to,
name,
skipPreinstall: Boolean(skipPreinstall),
skipPostinstall: Boolean(skipPostinstall),
}),
],
install: [
`Install all dependencies for all projects, modifying lockfiles and Bazel BUILD files if necessary
--cwd [cwd] Project directory to use
--skipPreinstall Skip the preinstall hook
--skipPostinstall Skip the postinstall hook
--mode If set to skip-build, skips build scripts. If set to update-lockfile, skips link step
--verbose`,
async ({cwd, skipPreinstall, skipPostinstall, mode, verbose}) =>
install({
root: await rootOf(args),
cwd,
skipPreinstall: Boolean(skipPreinstall),
skipPostinstall: Boolean(skipPostinstall),
mode,
verbose: Boolean(verbose),
}),
],
ci: [
`Install all dependencies for all project without modifying source files
--cwd [cwd] Project directory to use`,
async ({cwd}) => ci({root: await rootOf(args), cwd}),
],
focus: [
`Install all dependencies for one or more projects without installing the rest
--cwd [cwd] Project directory to use
--all Install all dependencies, like regular yarn install
--production Install only production dependencies, not devDependencies
--skipPreinstall Skip the preinstall hook
--skipPostinstall Skip the postinstall hook
--verbose`,
async ({
cwd,
all,
production,
skipPreinstall,
skipPostinstall,
verbose,
}) =>
focus({
root: await rootOf(args),
cwd,
all: Boolean(all),
production: Boolean(production),
packages: rest.filter(v => !v.startsWith('-')),
skipPreinstall: Boolean(skipPreinstall),
skipPostinstall: Boolean(skipPostinstall),
verbose: Boolean(verbose),
}),
],
add: [
`Install a package and any packages that it depends on
[deps...] Package(s) to add at a specific version. ie., [email protected]
--dev Whether to install as devDependency
--cwd [cwd] Project directory to use`,
async ({cwd, dev}) =>
add({
root: await rootOf(args),
cwd,
args: rest.filter(arg => arg != '--dev'), // if dev is passed before name, resolve to correct value
dev: Boolean(dev), // FIXME all args can technically be boolean, but we don't want Flow complaining about it everywhere
}),
],
remove: [
`Remove a package
[deps...] Package(s) to remove
--cwd [cwd] Project directory to use`,
async ({cwd, name}) =>
remove({root: await rootOf(args), cwd, args: rest}),
],
upgrade: [
`Upgrade a package version across all projects
[args...] Packages to upgrade and optionally their version ranges. e.g., foo@^1.2.3 bar@^1.2.3`,
async () => upgrade({root: await rootOf(args), args: rest}),
],
purge: [
`Remove generated files (i.e. node_modules folders and bazel output files)`,
async () => purge({root: await rootOf(args)}),
],
check: [
`Display deps w/ multiple versions installed across projects
--json Whether to print as JSON (e.g. for piping to jq)
--all Includes all dependencies, including those with only a single version installed`,
async ({json, all}) =>
check({
root: await rootOf(args),
json: Boolean(json),
all: Boolean(all),
}),
],
outdated: [
`Displays deps whose version is behind the latest version
--json Whether to print as JSON (e.g. for piping to jq)
--dedup De-duplicates results by combining used versions into a single result
--limit Limits the number of external packages to simultaneously resolve with requests to npm`,
async ({json, dedup, limit}) => {
return outdated({
root: await rootOf(args),
json: Boolean(json),
dedup: Boolean(dedup),
limit: limit ? Number(limit) : undefined,
});
},
],
resolutions: [
`Displays list of yarn resolutions`,
async () => console.log(await resolutions({root: await rootOf(args)})),
],
sort: [
`Sorts package.json fields`,
async () => await sort({root: await rootOf(args)}),
],
align: [
`Align a project's dependency versions to respect the version policy, if there is one
--cwd [cwd] Project directory to use
--skipPreinstall Skip the preinstall hook
--skipPostinstall Skip the postinstall hook`,
async ({cwd, skipPreinstall, skipPostinstall}) =>
await align({
root: await rootOf(args),
cwd,
skipPreinstall: Boolean(skipPreinstall),
skipPostinstall: Boolean(skipPostinstall),
}),
],
localize: [
`Align dependency versions to local versions, if a local version exists`,
async () => await localize({root: await rootOf(args)}),
],
changes: [
`Lists Bazel test targets that changed given a list of changed files
[files] A file containing a list of changed files (one per line). Defaults to stdin
--format [format] 'targets' or 'dirs'. Defaults to 'targets'`,
async ({name, format = 'targets'}) =>
changes({root: await rootOf(args), files: name, format}),
],
plan: [
`Outputs a plan that can be passed to \`jazelle batch\` for parallelizing a group of tests across workers
[targets] A file containing a list of targets (typically from \`jazelle changes\`). Defaults to stdin
--nodes [nodes] The number of nodes (i.e. cloud machines). Defaults to 1`,
async ({name, workers, nodes}) =>
plan({root: await rootOf(args), targets: name, workers, nodes}),
],
batch: [
`Runs a plan from \`jazelle plan\`, parallelizing tests across workers
[plan] A file containing a plan (typically from \`jazelle plan\`). Defaults to stdin
--index Which group of tests to execute. Defaults to 0
--cores [cores] Number of CPUs to use. Defaults to \`os.cpus().length\``,
async ({name, index, cores}) =>
batch({root: await rootOf(args), plan: name, index, cores}),
],
build: [
`Build a project
--cwd [cwd] Project directory to use`,
async ({cwd}) => build({root: await rootOf(args), cwd}),
],
dev: [
`Run a project
--cwd [cwd] Project directory to use`,
async ({cwd}) => dev({root: await rootOf(args), cwd, args: rest}),
],
test: [
`Test a project
--cwd [cwd] Project directory to use`,
async ({cwd}) => test({root: await rootOf(args), cwd, args: rest}),
],
lint: [
`Lint a project
--cwd [cwd] Project directory to use`,
async ({cwd}) => lint({root: await rootOf(args), cwd, args: rest}),
],
flow: [
`Typecheck a project using FlowType
--cwd [cwd] Project directory to use`,
async ({cwd}) => flow({root: await rootOf(args), cwd, args: rest}),
],
typecheck: [
`Typecheck a project
--cwd [cwd] Project directory to use`,
async ({cwd}) => typecheck({root: await rootOf(args), cwd, args: rest}),
],
start: [
`Run a project
--cwd [cwd] Project directory to use`,
async ({cwd}) => start({root: await rootOf(args), cwd, args: rest}),
],
'bin-path': [
`Print the local path of a binary
[name] 'bazel', 'node', or 'yarn'`,
async ({name}) => binPath(name),
],
bazel: [
`Run a Bazel command
[args...] A space separated list of arguments`,
async ({cwd}) =>
bazel({root: await rootOf(args).catch(() => cwd), args: rest}),
],
node: [
`Runs Node
[args...] A space separated list of arguments
--cwd [cwd] Project directory to use`,
async ({cwd}) => node({root: await rootOf(args), cwd, args: rest}),
],
yarn: [
`Runs a Yarn command
[name] A yarn command name
[args...] A space separated list of arguments
--cwd [cwd] Project directory to use`,
async ({cwd}) => yarn({cwd, args: rest}),
],
exec: [
`Runs a bash script
[args...] A space separated list of arguments
--cwd [cwd] Project directory to use`,
async ({cwd}) => exec({root: await rootOf(args), cwd, args: rest}),
],
each: [
`Runs a script in each project
[args...] A space separated list of arguments`,
async ({cwd}) => each({root: await rootOf(args), cwd, args: rest}),
],
bump: [
`Bump version for the specified package, plus changed dependencies
[type] major, premajor, minor, preminor, patch, prepatch, prerelease or none
--frozenPackageJson If true, throws if changes to package.json are required
--cwd [cwd] Project directory to use`,
async ({cwd, name: type, frozenPackageJson: frozen}) =>
bump({
root: await rootOf(args),
cwd,
type: type || frozen, // if frozen is passed before type, resolve to correct value
frozenPackageJson: Boolean(frozen),
}),
],
doctor: [
`Provides advice for some types of issues
--cwd [cwd] Project directory to use`,
async ({cwd}) => doctor({root: await rootOf(args), cwd}),
],
script: [
`Runs a npm script
[args...] A space separated list of arguments
--cwd [cwd] Project directory to use`,
async ({cwd}) =>
script({
root: await rootOf(args),
cwd,
args: rest,
}),
],
},
async ({cwd}) =>
script({
root: await rootOf(args),
cwd,
args: [command, ...rest],
})
);
};
async function rootOf(args) {
return getRootDir({dir: args.cwd});
}
module.exports = {
runCLI,
version: require('./package.json').version,
init,
scaffold,
install,
ci,
focus,
add,
remove,
upgrade,
purge,
check: reportMismatchedTopLevelDeps,
outdated,
resolutions,
sort,
align,
localize,
changes: findChangedTargets,
plan: getTestGroups,
batch,
build,
dev,
test,
lint,
flow,
start,
binPath: getBinaryPath,
bazel,
node,
yarn,
bump,
doctor,
script,
getRootDir,
};