33import { loadSession } from './api' ;
44import { appCommands } from './app' ;
55import { bundleCommands } from './bundle' ;
6+ import { moduleManager } from './module-manager' ;
7+ import { builtinModules } from './modules' ;
68import { packageCommands } from './package' ;
9+ import type { CommandContext } from './types' ;
710import { userCommands } from './user' ;
811import { printVersionCommand } from './utils' ;
912import { t } from './utils/i18n' ;
1013import { versionCommands } from './versions' ;
1114
15+ function registerBuiltinModules ( ) {
16+ for ( const module of builtinModules ) {
17+ try {
18+ moduleManager . registerModule ( module ) ;
19+ } catch ( error ) {
20+ console . error ( `Failed to register module ${ module . name } :` , error ) ;
21+ }
22+ }
23+ }
24+
1225function printUsage ( ) {
26+ console . log ( 'React Native Update CLI' ) ;
27+ console . log ( '' ) ;
28+ console . log ( 'Traditional commands:' ) ;
29+
30+ const legacyCommands = {
31+ ...userCommands ,
32+ ...bundleCommands ,
33+ ...appCommands ,
34+ ...packageCommands ,
35+ ...versionCommands ,
36+ } ;
37+
38+ for ( const [ name , handler ] of Object . entries ( legacyCommands ) ) {
39+ console . log ( ` ${ name } : Legacy command` ) ;
40+ }
41+
42+ console . log ( '' ) ;
43+ console . log ( 'Modular commands:' ) ;
44+ const commands = moduleManager . getRegisteredCommands ( ) ;
45+ for ( const command of commands ) {
46+ console . log (
47+ ` ${ command . name } : ${ command . description || 'No description' } ` ,
48+ ) ;
49+ }
50+
51+ console . log ( '' ) ;
52+ console . log ( 'Available workflows:' ) ;
53+ const workflows = moduleManager . getRegisteredWorkflows ( ) ;
54+ for ( const workflow of workflows ) {
55+ console . log (
56+ ` ${ workflow . name } : ${ workflow . description || 'No description' } ` ,
57+ ) ;
58+ }
59+
60+ console . log ( '' ) ;
61+ console . log ( 'Special commands:' ) ;
62+ console . log ( ' list: List all available commands and workflows' ) ;
63+ console . log ( ' workflow <name>: Execute a specific workflow' ) ;
64+ console . log ( ' help: Show this help message' ) ;
65+
66+ console . log ( '' ) ;
1367 console . log (
1468 'Visit `https://github.com/reactnativecn/react-native-update` for document.' ,
1569 ) ;
1670 process . exit ( 1 ) ;
1771}
1872
19- const commands = {
73+ const legacyCommands = {
2074 ...userCommands ,
2175 ...bundleCommands ,
2276 ...appCommands ,
@@ -31,20 +85,71 @@ async function run() {
3185 process . exit ( ) ;
3286 }
3387
88+ // Register builtin modules for modular functionality
89+ registerBuiltinModules ( ) ;
90+
3491 const argv = require ( 'cli-arguments' ) . parse ( require ( '../cli.json' ) ) ;
3592 global . NO_INTERACTIVE = argv . options [ 'no-interactive' ] ;
3693 global . USE_ACC_OSS = argv . options . acc ;
3794
38- loadSession ( )
39- . then ( ( ) => commands [ argv . command ] ( argv ) )
40- . catch ( ( err ) => {
41- if ( err . status === 401 ) {
42- console . log ( t ( 'loginFirst' ) ) ;
43- return ;
95+ const context : CommandContext = {
96+ args : argv . args || [ ] ,
97+ options : argv . options || { } ,
98+ } ;
99+
100+ try {
101+ await loadSession ( ) ;
102+ context . session = require ( './api' ) . getSession ( ) ;
103+
104+ // Handle special modular commands first
105+ if ( argv . command === 'help' ) {
106+ printUsage ( ) ;
107+ } else if ( argv . command === 'list' ) {
108+ moduleManager . listAll ( ) ;
109+ } else if ( argv . command === 'workflow' ) {
110+ const workflowName = argv . args [ 0 ] ;
111+ if ( ! workflowName ) {
112+ console . error ( 'Workflow name is required' ) ;
113+ process . exit ( 1 ) ;
44114 }
45- console . error ( err . stack ) ;
46- process . exit ( - 1 ) ;
47- } ) ;
115+ const result = await moduleManager . executeWorkflow ( workflowName , context ) ;
116+ if ( ! result . success ) {
117+ console . error ( 'Workflow execution failed:' , result . error ) ;
118+ process . exit ( 1 ) ;
119+ }
120+ console . log ( 'Workflow completed successfully:' , result . data ) ;
121+ }
122+ // Try legacy commands first for backward compatibility
123+ else if ( legacyCommands [ argv . command ] ) {
124+ await legacyCommands [ argv . command ] ( argv ) ;
125+ }
126+ // Fall back to modular commands
127+ else {
128+ const result = await moduleManager . executeCommand ( argv . command , context ) ;
129+ if ( ! result . success ) {
130+ console . error ( 'Command execution failed:' , result . error ) ;
131+ process . exit ( 1 ) ;
132+ }
133+ console . log ( 'Command completed successfully:' , result . data ) ;
134+ }
135+ } catch ( err : any ) {
136+ if ( err . status === 401 ) {
137+ console . log ( t ( 'loginFirst' ) ) ;
138+ return ;
139+ }
140+ console . error ( err . stack ) ;
141+ process . exit ( - 1 ) ;
142+ }
48143}
49144
145+ export { moduleManager } ;
146+ export { CLIProviderImpl } from './provider' ;
147+ export type {
148+ CLIProvider ,
149+ CLIModule ,
150+ CommandDefinition ,
151+ CustomWorkflow ,
152+ WorkflowStep ,
153+ } from './types' ;
154+
50155run ( ) ;
0 commit comments