@@ -36,36 +36,15 @@ program
3636 . version ( packageJSON . version )
3737 . showHelpAfterError ( ) ;
3838
39- const commandBuild = program . command ( 'build' )
39+ program . command ( 'build' )
4040 . description ( 'compile the project that was set up using Cpp.js' )
4141 . addOption ( new Option ( '-p, --platform <platform>' , 'target platform' ) . argParser ( createListParser ( platforms ) ) )
4242 . addOption ( new Option ( '-a, --arch <arch>' , 'target architecture' ) . argParser ( createListParser ( archs ) ) )
4343 . addOption ( new Option ( '-r, --runtime <runtime>' , 'target runtime' ) . argParser ( createListParser ( runtimes ) ) )
4444 . addOption ( new Option ( '-b, --build-type <buildType>' , 'target build type' ) . argParser ( createListParser ( buildTypes ) ) )
45- . addOption ( new Option ( '-e, --runtime-env <runtimeEnv>' , 'target runtime environment' ) . argParser ( createListParser ( runtimeEnvs ) ) ) ;
46-
47- const commandDocker = program . command ( 'docker' )
48- . description ( 'manage docker' ) ;
49- const commandRun = commandDocker . command ( 'run' ) . description ( 'run docker application' ) ;
50- commandDocker . command ( 'create' ) . description ( 'create docker container' ) ;
51- commandDocker . command ( 'start' ) . description ( 'start docker container' ) ;
52- commandDocker . command ( 'stop' ) . description ( 'stop docker container' ) ;
53- commandDocker . command ( 'delete' ) . description ( 'delete docker container' ) ;
54-
55- const commandConfig = program . command ( 'config' )
56- . description ( 'manage the Cpp.js configuration files' ) ;
57- commandConfig . command ( 'get' ) . description ( 'get the Cpp.js system configuration' ) ;
58- commandConfig . command ( 'set' ) . description ( 'set the Cpp.js system configuration' ) ;
59- commandConfig . command ( 'delete' ) . description ( 'delete the Cpp.js system configuration' ) ;
60- const commandConfigList = commandConfig . command ( 'list' ) . description ( 'list the Cpp.js configurations' )
61- . addOption ( new Option ( '-t, --type <type>' , 'config type' ) . default ( 'system' ) . choices ( [ 'all' , 'system' , 'project' ] ) ) ;
62- commandConfig . command ( 'keys' ) . description ( 'list all available system configuration keys for Cpp.js' ) ;
63-
64- program . parse ( process . argv ) ;
65-
66- switch ( program . args [ 0 ] ) {
67- case 'build' : {
68- const targetParams = commandBuild . opts ( ) ;
45+ . addOption ( new Option ( '-e, --runtime-env <runtimeEnv>' , 'target runtime environment' ) . argParser ( createListParser ( runtimeEnvs ) ) )
46+ . action ( ( options ) => {
47+ const targetParams = { ...options } ;
6948
7049 if ( ! targetParams . arch ) {
7150 targetParams . arch = archs . filter ( item => item !== 'wasm64' ) ;
@@ -85,101 +64,81 @@ switch (program.args[0]) {
8564 } else {
8665 build ( targetParams ) ;
8766 }
88- break ;
89- }
90- case 'docker' : {
91- switch ( program . args [ 1 ] ) {
92- case 'run' : {
93- const [ programName , ...params ] = commandRun . args ;
94- run ( programName , params ) ;
95- break ;
96- }
97- case 'create' : {
98- const args = [
99- 'run' ,
100- '-dit' ,
101- '--name' ,
102- `${ getDockerImage ( ) } -${ getContentHash ( state . config . paths . base ) } ` . replace ( '/' , '-' ) . replace ( ':' , '-' ) ,
103- '-v' , `${ state . config . paths . base } :/tmp/cppjs/live` ,
104- getDockerImage ( ) ,
105- 'bash' ,
106- ] ;
107- try {
108- execFileSync ( 'docker' , args , { stdio : 'inherit' } ) ;
109- } catch ( e ) {
110- console . error ( 'An error occurred while running the application. Please check the logs for more details.' ) ;
111- process . exit ( ) ;
112- }
113- break ;
114- }
115- case 'start' : {
116- const args = [
117- 'start' ,
118- `${ getDockerImage ( ) } -${ getContentHash ( state . config . paths . base ) } ` . replace ( '/' , '-' ) . replace ( ':' , '-' ) ,
119- ] ;
120- try {
121- execFileSync ( 'docker' , args , { stdio : 'inherit' } ) ;
122- } catch ( e ) {
123- console . error ( 'An error occurred while running the application. Please check the logs for more details.' ) ;
124- process . exit ( ) ;
125- }
126- break ;
127- }
128- case 'stop' : {
129- const args = [
130- 'stop' ,
131- `${ getDockerImage ( ) } -${ getContentHash ( state . config . paths . base ) } ` . replace ( '/' , '-' ) . replace ( ':' , '-' ) ,
132- ] ;
133- try {
134- execFileSync ( 'docker' , args , { stdio : 'inherit' } ) ;
135- } catch ( e ) {
136- console . error ( 'An error occurred while running the application. Please check the logs for more details.' ) ;
137- process . exit ( ) ;
138- }
139- break ;
140- }
141- case 'delete' : {
142- const args = [
143- 'rm' ,
144- `${ getDockerImage ( ) } -${ getContentHash ( state . config . paths . base ) } ` . replace ( '/' , '-' ) . replace ( ':' , '-' ) ,
145- ] ;
146- try {
147- execFileSync ( 'docker' , args , { stdio : 'inherit' } ) ;
148- } catch ( e ) {
149- console . error ( 'An error occurred while running the application. Please check the logs for more details.' ) ;
150- process . exit ( ) ;
151- }
152- break ;
153- }
154- default :
155- }
156- break ;
157- }
158- case 'config' : {
159- switch ( program . args [ 1 ] ) {
160- case 'get' :
161- getSystemConfig ( program . args [ 2 ] ) ;
162- break ;
163- case 'set' :
164- setSystemConfig ( program . args [ 2 ] , program . args [ 3 ] ) ;
165- break ;
166- case 'delete' :
167- deleteSystemConfig ( program . args [ 2 ] ) ;
168- break ;
169- case 'list' :
170- listSystemConfig ( commandConfigList . opts ( ) . type ) ;
171- break ;
172- case 'keys' :
173- listSystemKeys ( ) ;
174- break ;
175- default :
176- break ;
177- }
178- break ;
67+ } ) ;
68+
69+ const dockerContainerName = ( ) =>
70+ `${ getDockerImage ( ) } -${ getContentHash ( state . config . paths . base ) } `
71+ . replace ( '/' , '-' )
72+ . replace ( ':' , '-' ) ;
73+
74+ const dockerExec = ( args ) => {
75+ try {
76+ execFileSync ( 'docker' , args , { stdio : 'inherit' } ) ;
77+ } catch ( e ) {
78+ console . error ( 'An error occurred while running the application. Please check the logs for more details.' ) ;
79+ process . exit ( ) ;
17980 }
180- default :
181- break ;
182- }
81+ } ;
82+
83+ const commandDocker = program . command ( 'docker' ) . description ( 'manage docker' ) ;
84+
85+ commandDocker . command ( 'run' )
86+ . description ( 'run docker application' )
87+ . argument ( '<program>' , 'program to execute inside the container' )
88+ . argument ( '[params...]' , 'arguments passed to the program' )
89+ . action ( ( programName , params ) => run ( programName , params ) ) ;
90+
91+ commandDocker . command ( 'create' )
92+ . description ( 'create docker container' )
93+ . action ( ( ) => dockerExec ( [
94+ 'run' , '-dit' ,
95+ '--name' , dockerContainerName ( ) ,
96+ '-v' , `${ state . config . paths . base } :/tmp/cppjs/live` ,
97+ getDockerImage ( ) ,
98+ 'bash' ,
99+ ] ) ) ;
100+
101+ commandDocker . command ( 'start' )
102+ . description ( 'start docker container' )
103+ . action ( ( ) => dockerExec ( [ 'start' , dockerContainerName ( ) ] ) ) ;
104+
105+ commandDocker . command ( 'stop' )
106+ . description ( 'stop docker container' )
107+ . action ( ( ) => dockerExec ( [ 'stop' , dockerContainerName ( ) ] ) ) ;
108+
109+ commandDocker . command ( 'delete' )
110+ . description ( 'delete docker container' )
111+ . action ( ( ) => dockerExec ( [ 'rm' , dockerContainerName ( ) ] ) ) ;
112+
113+ const commandConfig = program . command ( 'config' )
114+ . description ( 'manage the Cpp.js configuration files' ) ;
115+
116+ commandConfig . command ( 'get' )
117+ . description ( 'get the Cpp.js system configuration' )
118+ . argument ( '<key>' , 'configuration key to read' )
119+ . action ( ( key ) => getSystemConfig ( key ) ) ;
120+
121+ commandConfig . command ( 'set' )
122+ . description ( 'set the Cpp.js system configuration' )
123+ . argument ( '<key>' , 'configuration key to set' )
124+ . argument ( '<value>' , 'value to assign' )
125+ . action ( ( key , value ) => setSystemConfig ( key , value ) ) ;
126+
127+ commandConfig . command ( 'delete' )
128+ . description ( 'delete the Cpp.js system configuration' )
129+ . argument ( '<key>' , 'configuration key to remove' )
130+ . action ( ( key ) => deleteSystemConfig ( key ) ) ;
131+
132+ commandConfig . command ( 'list' )
133+ . description ( 'list the Cpp.js configurations' )
134+ . addOption ( new Option ( '-t, --type <type>' , 'config type' ) . default ( 'system' ) . choices ( [ 'all' , 'system' , 'project' ] ) )
135+ . action ( ( options ) => listSystemConfig ( options . type ) ) ;
136+
137+ commandConfig . command ( 'keys' )
138+ . description ( 'list all available system configuration keys for Cpp.js' )
139+ . action ( ( ) => listSystemKeys ( ) ) ;
140+
141+ program . parse ( process . argv ) ;
183142
184143function listSystemKeys ( ) {
185144 console . info ( 'Available configurations:' ) ;
0 commit comments