|
1 | 1 | import { join } from 'node:path'; |
2 | | -import { parseArgs } from 'node:util'; |
3 | 2 | import { getConfigState } from '../config.js'; |
4 | 3 | import { runStagedCLI } from '../staged.js'; |
| 4 | +import { insertConfigArg, parseCliArgs } from './args.js'; |
5 | 5 |
|
6 | 6 | declare global { |
7 | 7 | const RSTACK_VERSION: string; |
@@ -29,73 +29,6 @@ Options: |
29 | 29 | -h, --help Display this help message |
30 | 30 | -v, --version Display version number`; |
31 | 31 |
|
32 | | -type ParsedRstackArgs = { |
33 | | - args: string[]; |
34 | | - configPath?: string; |
35 | | -}; |
36 | | - |
37 | | -function parseCliArgs(args: string[]): ParsedRstackArgs { |
38 | | - const { tokens } = parseArgs({ |
39 | | - args, |
40 | | - options: { |
41 | | - config: { type: 'string', short: 'c' }, |
42 | | - }, |
43 | | - strict: false, |
44 | | - allowPositionals: true, |
45 | | - tokens: true, |
46 | | - }); |
47 | | - |
48 | | - let removedIndexes: number[] | undefined; |
49 | | - let configPath: string | undefined; |
50 | | - |
51 | | - for (const token of tokens) { |
52 | | - if (token.kind === 'option-terminator') { |
53 | | - break; |
54 | | - } |
55 | | - |
56 | | - // parseArgs expands short option groups like `-abc`; only consume `-c` when it starts the raw arg. |
57 | | - if ( |
58 | | - token.kind !== 'option' || |
59 | | - token.name !== 'config' || |
60 | | - (token.rawName === '-c' && !args[token.index].startsWith('-c')) |
61 | | - ) { |
62 | | - continue; |
63 | | - } |
64 | | - |
65 | | - if (token.value === undefined || token.value.length === 0) { |
66 | | - throw new Error(`Missing value for ${token.rawName}.`); |
67 | | - } |
68 | | - |
69 | | - configPath = token.value; |
70 | | - const indexes = (removedIndexes ??= []); |
71 | | - indexes.push(token.index); |
72 | | - |
73 | | - if (!token.inlineValue) { |
74 | | - indexes.push(token.index + 1); |
75 | | - } |
76 | | - } |
77 | | - |
78 | | - if (!removedIndexes) { |
79 | | - return { args }; |
80 | | - } |
81 | | - |
82 | | - return { |
83 | | - args: args.filter((_, index) => !removedIndexes.includes(index)), |
84 | | - configPath, |
85 | | - }; |
86 | | -} |
87 | | - |
88 | | -function insertConfigArg(args: string[], option: string, configPath: string): string[] { |
89 | | - // Keep the injected config before `--`; arguments after it must remain positional for the child CLI. |
90 | | - const terminatorIndex = args.indexOf('--'); |
91 | | - |
92 | | - if (terminatorIndex === -1) { |
93 | | - return [...args, option, configPath]; |
94 | | - } |
95 | | - |
96 | | - return [...args.slice(0, terminatorIndex), option, configPath, ...args.slice(terminatorIndex)]; |
97 | | -} |
98 | | - |
99 | 32 | async function runRsbuildCLI(args: string[]): Promise<void> { |
100 | 33 | const argv = [ |
101 | 34 | process.execPath, |
@@ -142,9 +75,10 @@ async function runRslintCLI(args: string[]): Promise<void> { |
142 | 75 |
|
143 | 76 | export async function setupCommands(): Promise<void> { |
144 | 77 | const { args, configPath } = parseCliArgs(process.argv.slice(2)); |
145 | | - getConfigState().configPath = configPath; |
146 | 78 | const command = args[0]; |
147 | 79 |
|
| 80 | + getConfigState().configPath = configPath; |
| 81 | + |
148 | 82 | if (!command || command === '-h' || command === '--help') { |
149 | 83 | console.log(helpMessage); |
150 | 84 | return; |
|
0 commit comments