|
1 | 1 | import cac from 'cac'; |
2 | | -import { logger } from '@rsbuild/core'; |
3 | | -import type { BuildOptions, DevOptions, PreviewOptions } from './types.js'; |
4 | 2 |
|
5 | 3 | declare global { |
6 | 4 | const RSTACK_VERSION: string; |
7 | 5 | } |
8 | 6 |
|
| 7 | +async function runRsbuildCLI(): Promise<void> { |
| 8 | + const args = process.argv.slice(2); |
| 9 | + process.argv = [process.execPath, 'rsbuild', ...args]; |
| 10 | + |
| 11 | + const { runCLI } = await import('@rsbuild/core'); |
| 12 | + runCLI(); |
| 13 | +} |
| 14 | + |
9 | 15 | export function setupCommands(): void { |
10 | 16 | const cli = cac('rs'); |
11 | 17 | const { argv } = process; |
12 | 18 |
|
13 | 19 | cli.version(RSTACK_VERSION); |
14 | 20 |
|
15 | | - cli.command('dev', 'Start the Rsbuild dev server').action(async (options: DevOptions) => { |
16 | | - try { |
17 | | - const { initRsbuild } = await import('./rsbuild.js'); |
18 | | - const rsbuild = await initRsbuild({ |
19 | | - options, |
20 | | - }); |
21 | | - |
22 | | - if (!rsbuild) { |
23 | | - return; |
24 | | - } |
25 | | - |
26 | | - await rsbuild.startDevServer(); |
27 | | - } catch (err) { |
28 | | - logger.error('Failed to start dev server.'); |
29 | | - logger.error(err); |
30 | | - process.exit(1); |
31 | | - } |
32 | | - }); |
| 21 | + cli |
| 22 | + .command('dev [...args]', 'Start the Rsbuild dev server') |
| 23 | + .allowUnknownOptions() |
| 24 | + .action(runRsbuildCLI); |
33 | 25 |
|
34 | 26 | cli |
35 | | - .command('build', 'Run Rsbuild to build the app for production') |
| 27 | + .command('build [...args]', 'Run Rsbuild to build the app for production') |
| 28 | + .allowUnknownOptions() |
36 | 29 | .option('-w, --watch', 'Enable watch mode to automatically rebuild on file changes') |
37 | | - .action(async (options: BuildOptions) => { |
38 | | - try { |
39 | | - if (!options.watch) { |
40 | | - process.env.RSPACK_UNSAFE_FAST_DROP = 'true'; |
41 | | - } |
42 | | - |
43 | | - const { initRsbuild } = await import('./rsbuild.js'); |
44 | | - const rsbuild = await initRsbuild({ |
45 | | - options, |
46 | | - }); |
47 | | - |
48 | | - if (!rsbuild) { |
49 | | - return; |
50 | | - } |
51 | | - |
52 | | - const buildResult = await rsbuild.build({ |
53 | | - watch: options.watch, |
54 | | - }); |
55 | | - |
56 | | - if (buildResult) { |
57 | | - if (options.watch) { |
58 | | - // TODO |
59 | | - } else { |
60 | | - await buildResult.close(); |
61 | | - } |
62 | | - } |
63 | | - } catch (err) { |
64 | | - const RSPACK_BUILD_ERROR = 'Rspack build failed.'; |
65 | | - const isRspackError = err instanceof Error && err.message === RSPACK_BUILD_ERROR; |
66 | | - if (!isRspackError) { |
67 | | - logger.error('Failed to build.'); |
68 | | - } |
69 | | - |
70 | | - logger.error(err); |
71 | | - process.exit(1); |
72 | | - } |
73 | | - }); |
| 30 | + .action(runRsbuildCLI); |
74 | 31 |
|
75 | 32 | cli |
76 | | - .command('preview', 'Preview the production build locally via Rsbuild') |
77 | | - .action(async (options: PreviewOptions) => { |
78 | | - try { |
79 | | - const { initRsbuild } = await import('./rsbuild.js'); |
80 | | - const rsbuild = await initRsbuild({ |
81 | | - options, |
82 | | - }); |
83 | | - |
84 | | - if (!rsbuild) { |
85 | | - return; |
86 | | - } |
87 | | - |
88 | | - await rsbuild.preview(); |
89 | | - } catch (err) { |
90 | | - logger.error('Failed to start preview server.'); |
91 | | - logger.error(err); |
92 | | - process.exit(1); |
93 | | - } |
94 | | - }); |
| 33 | + .command('preview [...args]', 'Preview the production build locally via Rsbuild') |
| 34 | + .allowUnknownOptions() |
| 35 | + .action(runRsbuildCLI); |
95 | 36 |
|
96 | 37 | cli.command('test [...args]', 'Run tests with Rstest').action(async () => { |
97 | 38 | const { runCLI } = await import('@rstest/core'); |
|
0 commit comments