|
1 | 1 | #!/usr/bin/env node |
2 | | -import { program } from "commander"; |
| 2 | +// eslint-disable-next-line n/no-unsupported-features/node-builtins |
| 3 | +import { parseArgs } from "node:util"; |
3 | 4 | import assertNodeEnvironmentSuitable from "#cli/assert-node-environment-suitable.mjs"; |
4 | 5 | import cli from "#cli/index.mjs"; |
5 | 6 | import meta from "#meta.cjs"; |
6 | 7 |
|
7 | | -function formatError(pError) { |
8 | | - process.stderr.write(pError.message); |
9 | | - process.exitCode = 1; |
| 8 | +function showHelp() { |
| 9 | + process.stdout |
| 10 | + .write(`Usage: depcruise-baseline [options] <files-or-directories...> |
| 11 | +
|
| 12 | +Writes all known violations of rules in a .dependency-cruiser.js to a file. |
| 13 | +Alias for depcruise -c -T baseline -f .dependency-cruiser-known-violations.json [files-or-directories] |
| 14 | +Details: https://github.com/sverweij/dependency-cruiser |
| 15 | +
|
| 16 | +Options: |
| 17 | + -c, --config [file] read rules and options from [file] (default: true) |
| 18 | + -f, --output-to [file] file to write output to; - for stdout (default: ".dependency-cruiser-known-violations.json") |
| 19 | + -V, --version display version number |
| 20 | + -h, --help display help for command |
| 21 | +`); |
10 | 22 | } |
11 | 23 |
|
12 | 24 | try { |
13 | 25 | assertNodeEnvironmentSuitable(); |
14 | 26 |
|
15 | | - program |
16 | | - .description( |
17 | | - "Writes all known violations of rules in a .dependency-cruiser.js to a file.\n" + |
18 | | - "Alias for depcruise -c -T baseline -f .dependency-cruiser-known-violations.json [files-or-directories]\n" + |
19 | | - "Details: https://github.com/sverweij/dependency-cruiser", |
20 | | - ) |
21 | | - .option("-c, --config [file]", "read rules and options from [file]", true) |
22 | | - .option( |
23 | | - "-f, --output-to [file]", |
24 | | - "file to write output to; - for stdout", |
25 | | - ".dependency-cruiser-known-violations.json", |
26 | | - ) |
27 | | - .version(meta.version) |
28 | | - .argument("<files-or-directories...>") |
29 | | - .parse(process.argv); |
| 27 | + const { values: options, positionals } = parseArgs({ |
| 28 | + options: { |
| 29 | + config: { |
| 30 | + type: "string", |
| 31 | + short: "c", |
| 32 | + default: "", |
| 33 | + }, |
| 34 | + "output-to": { |
| 35 | + type: "string", |
| 36 | + short: "f", |
| 37 | + default: ".dependency-cruiser-known-violations.json", |
| 38 | + }, |
| 39 | + version: { |
| 40 | + type: "boolean", |
| 41 | + short: "V", |
| 42 | + }, |
| 43 | + help: { |
| 44 | + type: "boolean", |
| 45 | + short: "h", |
| 46 | + }, |
| 47 | + }, |
| 48 | + allowPositionals: true, |
| 49 | + }); |
30 | 50 |
|
31 | | - if (program.args[0]) { |
32 | | - process.exitCode = await cli(program.args, { |
33 | | - ...program.opts(), |
| 51 | + if (options.version) { |
| 52 | + process.stdout.write(`${meta.version}\n`); |
| 53 | + } else if (options.help || positionals.length === 0) { |
| 54 | + showHelp(); |
| 55 | + } else { |
| 56 | + if (options.config === "") { |
| 57 | + options.config = true; |
| 58 | + } |
| 59 | + process.exitCode = await cli(positionals, { |
| 60 | + config: options.config, |
| 61 | + outputTo: options["output-to"], |
34 | 62 | cache: false, |
35 | 63 | outputType: "baseline", |
36 | 64 | }); |
37 | | - } else { |
38 | | - program.help(); |
39 | 65 | } |
40 | 66 | } catch (pError) { |
41 | | - formatError(pError); |
| 67 | + process.stderr.write(pError.message); |
42 | 68 | process.exitCode = 1; |
43 | 69 | } |
0 commit comments