-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathindex.ts
More file actions
84 lines (69 loc) · 2.46 KB
/
Copy pathindex.ts
File metadata and controls
84 lines (69 loc) · 2.46 KB
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
import { styleText } from 'node:util';
import { logger } from '@rock-js/tools';
import { Command } from 'commander';
import { ExampleUsage } from './shared/index.js';
import brownfieldCommands, {
groupName as brownfieldCommandsGroupName,
} from './brownfield/index.js';
import brownieCommands, {
groupName as brownieCommandsGroupName,
} from './brownie/index.js';
const program = new Command();
program
.name(styleText('magenta', 'brownie'))
.usage(styleText('yellow', '[options] [command]'))
.description(
styleText('magentaBright', 'React Native Brownfield CLI - ') +
styleText(['magenta', 'bold', 'underline'], 'Brownie')
)
.version(process.env.npm_package_version ?? '0.0.0');
program
.optionsGroup('Global options:')
.option('--verbose', 'enable verbose logging')
.hook('preAction', () => {
const opts = program.opts();
if (opts.verbose) {
logger.setVerbose(opts.verbose ?? false);
}
});
program.configureHelp({
styleTitle: (str) => styleText('bold', str),
styleCommandText: (str) => styleText('cyan', str),
styleArgumentText: (str) => styleText('yellow', str),
styleSubcommandText: (str) => styleText('blue', str),
});
function registrationHelper(
commandsRegistration: Record<string, unknown | Command | ExampleUsage>,
groupName: string
) {
program.commandsGroup(groupName);
const exampleUsageItems: ExampleUsage[] = [];
Object.values(commandsRegistration).forEach((commandOrExampleUsage) => {
if (commandOrExampleUsage instanceof Command) {
// command
program.addCommand(commandOrExampleUsage);
} else if (commandOrExampleUsage instanceof ExampleUsage) {
// piece of example usage for the command group
exampleUsageItems.push(commandOrExampleUsage);
}
});
if (exampleUsageItems.length) {
const longestUsageItemCommandLength = exampleUsageItems.reduce(
(max, item) => Math.max(max, item.command.length),
0
);
program.addHelpText(
'after',
`\nExamples:\n${exampleUsageItems.map((item) => `\t ${styleText('dim', item.command.padEnd(longestUsageItemCommandLength))}\t${item.description}`).join('\n')}\n`
);
}
}
registrationHelper(brownfieldCommands, brownfieldCommandsGroupName);
registrationHelper(brownieCommands, brownieCommandsGroupName);
program.commandsGroup('Utility commands').helpCommand('help [command]');
export function runCLI(argv: string[]): void {
program.parse(argv);
if (!argv.slice(2).length) {
program.outputHelp();
}
}