11import type { RsbuildMode } from '@rsbuild/core' ;
2- import { type Command , program } from 'commander ' ;
2+ import cac , { type CAC } from 'cac ' ;
33import { logger } from '../utils/logger' ;
44import { build } from './build' ;
55import { init } from './init' ;
@@ -25,14 +25,14 @@ export type InspectOptions = CommonOptions & {
2525 verbose ?: boolean ;
2626} ;
2727
28- const applyCommonOptions = ( command : Command ) => {
29- command
28+ const applyCommonOptions = ( cli : CAC ) => {
29+ cli
3030 . option (
31- '-c --config <config>' ,
31+ '-c, --config <config>' ,
3232 'specify the configuration file, can be a relative or absolute path' ,
3333 )
3434 . option (
35- '-r --root <root>' ,
35+ '-r, --root <root>' ,
3636 'specify the project root directory, can be an absolute path or a path relative to cwd' ,
3737 )
3838 . option (
@@ -43,26 +43,33 @@ const applyCommonOptions = (command: Command) => {
4343 . option (
4444 '--lib <id>' ,
4545 'specify the library (repeatable, e.g. --lib esm --lib cjs)' ,
46- repeatableOption ,
46+ {
47+ type : [ String ] ,
48+ default : [ ] ,
49+ } ,
4750 ) ;
4851} ;
4952
50- const repeatableOption = ( value : string , previous : string [ ] ) => {
51- return ( previous ?? [ ] ) . concat ( [ value ] ) ;
52- } ;
53-
5453export function runCli ( ) : void {
55- program . name ( 'rslib' ) . usage ( '<command> [options]' ) . version ( RSLIB_VERSION ) ;
54+ const cli = cac ( 'rslib' ) ;
55+
56+ cli . help ( ) ;
57+ cli . version ( RSLIB_VERSION ) ;
5658
57- const buildCommand = program . command ( 'build' ) ;
58- const inspectCommand = program . command ( 'inspect' ) ;
59- const mfDevCommand = program . command ( 'mf-dev' ) ;
59+ applyCommonOptions ( cli ) ;
6060
61- [ buildCommand , inspectCommand , mfDevCommand ] . forEach ( applyCommonOptions ) ;
61+ const buildCommand = cli . command ( 'build' , 'build the library for production' ) ;
62+ const inspectCommand = cli . command (
63+ 'inspect' ,
64+ 'inspect the Rsbuild / Rspack configs of Rslib projects' ,
65+ ) ;
66+ const mfDevCommand = cli . command (
67+ 'mf-dev' ,
68+ 'start Rsbuild dev server of Module Federation format' ,
69+ ) ;
6270
6371 buildCommand
64- . option ( '-w --watch' , 'turn on watch mode, watch for changes and rebuild' )
65- . description ( 'build the library for production' )
72+ . option ( '-w, --watch' , 'turn on watch mode, watch for changes and rebuild' )
6673 . action ( async ( options : BuildOptions ) => {
6774 try {
6875 const cliBuild = async ( ) => {
@@ -92,12 +99,9 @@ export function runCli(): void {
9299 } ) ;
93100
94101 inspectCommand
95- . description ( 'inspect the Rsbuild / Rspack configs of Rslib projects' )
96- . option (
97- '--output <output>' ,
98- 'specify inspect content output path' ,
99- '.rsbuild' ,
100- )
102+ . option ( '--output <output>' , 'specify inspect content output path' , {
103+ default : '.rsbuild' ,
104+ } )
101105 . option ( '--verbose' , 'show full function definitions in output' )
102106 . action ( async ( options : InspectOptions ) => {
103107 try {
@@ -116,27 +120,26 @@ export function runCli(): void {
116120 }
117121 } ) ;
118122
119- mfDevCommand
120- . description ( 'start Rsbuild dev server of Module Federation format' )
121- . action ( async ( options : CommonOptions ) => {
122- try {
123- const cliMfDev = async ( ) => {
124- const { config, watchFiles } = await init ( options ) ;
125- await startMFDevServer ( config , {
126- lib : options . lib ,
127- } ) ;
123+ mfDevCommand . action ( async ( options : CommonOptions ) => {
124+ try {
125+ const cliMfDev = async ( ) => {
126+ const { config, watchFiles } = await init ( options ) ;
127+ await startMFDevServer ( config , {
128+ lib : options . lib ,
129+ } ) ;
128130
129- watchFilesForRestart ( watchFiles , async ( ) => {
130- await cliMfDev ( ) ;
131- } ) ;
132- } ;
131+ watchFilesForRestart ( watchFiles , async ( ) => {
132+ await cliMfDev ( ) ;
133+ } ) ;
134+ } ;
133135
134- await cliMfDev ( ) ;
135- } catch ( err ) {
136- logger . error ( 'Failed to start mf-dev.' ) ;
137- logger . error ( err ) ;
138- process . exit ( 1 ) ;
139- }
140- } ) ;
141- program . parse ( ) ;
136+ await cliMfDev ( ) ;
137+ } catch ( err ) {
138+ logger . error ( 'Failed to start mf-dev.' ) ;
139+ logger . error ( err ) ;
140+ process . exit ( 1 ) ;
141+ }
142+ } ) ;
143+
144+ cli . parse ( ) ;
142145}
0 commit comments