|
10 | 10 |
|
11 | 11 | 'use strict'; |
12 | 12 |
|
13 | | -const {bundleCommand: bc} = require('@react-native/community-cli-plugin'); |
14 | | -const commander = require('commander'); |
| 13 | +const { |
| 14 | + bundleCommand: bundleCommandBase, |
| 15 | + unstable_createBundleCommandParser, |
| 16 | +} = require('@react-native/community-cli-plugin'); |
15 | 17 | const {execSync} = require('node:child_process'); |
16 | 18 |
|
17 | | -// Commander 12.0.0 changes from the global to named export |
18 | | -// $FlowFixMe[signature-verification-failure] |
19 | | -const program = commander.program ?? commander; |
| 19 | +/*:: |
| 20 | +type BundleOptions = { |
| 21 | + configCmd?: string, |
| 22 | + loadConfig?: string, |
| 23 | + ... |
| 24 | +}; |
| 25 | +*/ |
20 | 26 |
|
21 | | -program |
22 | | - .name(bc.name) |
23 | | - .description(bc.description ?? '') |
24 | | - .option( |
25 | | - '--config-cmd <string>', |
26 | | - 'Command to generate a JSON project config', |
27 | | - 'npx react-native config', |
28 | | - ) |
29 | | - .option('--load-config <string>', 'JSON project config') |
30 | | - .option('--verbose', 'Additional logs', () => true, false) |
31 | | - .allowUnknownOption() |
32 | | - .action(async function handleAction() { |
33 | | - let config = null; |
34 | | - let options = program |
35 | | - .opts /*::<{ |
36 | | - configCmd?: string, |
37 | | - loadConfig?: string, |
38 | | - verbose: boolean, |
39 | | - ... |
40 | | - }>*/ |
41 | | - (); |
42 | | - if (options.loadConfig != null) { |
43 | | - config = JSON.parse( |
44 | | - options.loadConfig.replace(/^\W*'/, '').replace(/'\W*$/, ''), |
45 | | - ); |
46 | | - } else if (options.configCmd != null) { |
47 | | - config = JSON.parse( |
48 | | - execSync(options.configCmd.trim(), {encoding: 'utf8'}), |
49 | | - ); |
50 | | - } |
| 27 | +const baseOptions = [ |
| 28 | + { |
| 29 | + name: '--config-cmd <string>', |
| 30 | + description: 'Command to generate a JSON project config', |
| 31 | + default: 'npx react-native config', |
| 32 | + }, |
| 33 | + { |
| 34 | + name: '--load-config <string>', |
| 35 | + description: 'JSON project config', |
| 36 | + }, |
| 37 | +]; |
51 | 38 |
|
52 | | - if (config == null) { |
53 | | - throw new Error('No config provided'); |
54 | | - } |
| 39 | +const { |
| 40 | + parser: bundleCommandParser, |
| 41 | + baseHelpInformation: bundleCommandBaseHelp, |
| 42 | +} = unstable_createBundleCommandParser(baseOptions); |
55 | 43 |
|
56 | | - await bc.func(program.args, config, options); |
57 | | - }); |
| 44 | +function formatOptions( |
| 45 | + options /*: ReadonlyArray<Readonly<{description?: string, name: string, ...}>> */, |
| 46 | +) { |
| 47 | + return options |
| 48 | + .map(option => ` ${option.name.padEnd(35)} ${option.description ?? ''}`) |
| 49 | + .join('\n'); |
| 50 | +} |
| 51 | + |
| 52 | +function printHelp() { |
| 53 | + console.log(`${bundleCommandBaseHelp} |
| 54 | +Additional options: |
| 55 | +${formatOptions(baseOptions)} |
| 56 | +`); |
| 57 | +} |
| 58 | + |
| 59 | +async function main(argv /*: ReadonlyArray<string> */ = process.argv.slice(2)) { |
| 60 | + if (argv.includes('--help')) { |
| 61 | + printHelp(); |
| 62 | + return; |
| 63 | + } |
58 | 64 |
|
59 | | -if (bc.options != null) { |
60 | | - for (const o of bc.options) { |
61 | | - program.option( |
62 | | - o.name, |
63 | | - o.description ?? '', |
64 | | - o.parse ?? (value => value), |
65 | | - o.default, |
| 65 | + bundleCommandParser.parse(argv, {from: 'user'}); |
| 66 | + const options = bundleCommandParser |
| 67 | + .opts /*::<BundleOptions>*/ |
| 68 | + (); |
| 69 | + |
| 70 | + let config = null; |
| 71 | + if (options.loadConfig != null) { |
| 72 | + config = JSON.parse( |
| 73 | + options.loadConfig.replace(/^\W*'/, '').replace(/'\W*$/, ''), |
66 | 74 | ); |
| 75 | + } else if (options.configCmd != null) { |
| 76 | + config = JSON.parse(execSync(options.configCmd.trim(), {encoding: 'utf8'})); |
| 77 | + } |
| 78 | + |
| 79 | + if (config == null) { |
| 80 | + throw new Error('No config provided'); |
67 | 81 | } |
| 82 | + |
| 83 | + await bundleCommandBase.func(bundleCommandParser.args, config, options); |
68 | 84 | } |
69 | 85 |
|
70 | 86 | if (require.main === module) { |
71 | | - program.parse(process.argv); |
| 87 | + void main(); |
72 | 88 | } |
73 | | - |
74 | | -module.exports = program; |
|
0 commit comments