@@ -18,7 +18,8 @@ const fs = require('fs-extra');
18
18
const path = require ( 'path' ) ;
19
19
const slash = require ( 'slash' ) ;
20
20
const minimist = require ( 'minimist' ) ;
21
- const { getCliVersion} = require ( '../lib/helper/utils' ) ;
21
+ const { getCliVersion, cleanArgs, defaultConfig} = require ( '../lib/helper/utils' ) ;
22
+ const execa = require ( 'execa' ) ;
22
23
23
24
function checkNodeVersion ( wanted , id ) {
24
25
if ( ! semver . satisfies ( process . version , wanted ) ) {
@@ -30,10 +31,6 @@ function checkNodeVersion(wanted, id) {
30
31
}
31
32
}
32
33
33
- const defaultConfig = {
34
- registry : 'https://registry.npm.taobao.org'
35
- } ;
36
-
37
34
checkNodeVersion ( requiredVersion , 'mars-cli' ) ;
38
35
39
36
if ( semver . satisfies ( process . version , '9.x' ) ) {
@@ -152,31 +149,58 @@ program
152
149
. option ( '-r, --registry <url>' , 'Use specified npm registry when installing dependencies (only for npm)' )
153
150
. option ( '-t, --target <target>' , 'Build target (swan | h5 | wx, default: swan)' )
154
151
. action ( cmd => {
155
- const build = require ( '../lib/build' ) ;
156
152
const options = cleanArgs ( cmd ) ;
153
+ const buildPath = path . resolve ( __dirname , './mars-build.js' ) ;
154
+
155
+ const targets = ( options . target || 'swan' ) . split ( ',' ) ;
156
+ targets . forEach ( t => {
157
+ const args = [ buildPath , '-t' , t ] ;
158
+ Object . keys ( options ) . forEach ( op => {
159
+ if ( op === 'target' ) {
160
+ return ;
161
+ }
157
162
158
- if ( ! options . registry ) {
159
- options . registry = defaultConfig . registry ;
160
- }
161
-
162
- build ( options ) ;
163
+ if ( options [ op ] !== false ) {
164
+ args . push ( '--' + op ) ;
165
+ if ( options [ op ] !== true ) {
166
+ args . push ( options . op ) ;
167
+ }
168
+ }
169
+ } ) ;
170
+ execa ( 'node' , args , {
171
+ stdout : 'inherit'
172
+ } ) ;
173
+ } ) ;
163
174
} ) ;
164
175
165
176
program
166
177
. command ( 'serve' )
167
178
. description ( 'serve project in development mode' )
168
179
. option ( '-r, --registry <url>' , 'Use specified npm registry when installing dependencies (only for npm)' )
169
180
. option ( '-t, --target <target>' , 'Build target (swan | h5 | wx, default: swan)' )
170
- // .option('-d, --dest <dir>', 'output directory (default: dist)')
171
181
. action ( cmd => {
172
- const start = require ( '../lib/serve' ) ;
173
182
const options = cleanArgs ( cmd ) ;
183
+ const buildPath = path . resolve ( __dirname , './mars-serve.js' ) ;
184
+
185
+ const targets = ( options . target || 'swan' ) . split ( ',' ) ;
186
+ targets . forEach ( t => {
187
+ const args = [ buildPath , '-t' , t ] ;
188
+ Object . keys ( options ) . forEach ( op => {
189
+ if ( op === 'target' ) {
190
+ return ;
191
+ }
174
192
175
- if ( ! options . registry ) {
176
- options . registry = defaultConfig . registry ;
177
- }
178
-
179
- start ( options ) ;
193
+ if ( options [ op ] !== false ) {
194
+ args . push ( '--' + op ) ;
195
+ if ( options [ op ] !== true ) {
196
+ args . push ( options . op ) ;
197
+ }
198
+ }
199
+ } ) ;
200
+ execa ( 'node' , args , {
201
+ stdout : 'inherit'
202
+ } ) ;
203
+ } ) ;
180
204
} ) ;
181
205
182
206
program
@@ -227,22 +251,3 @@ program.parse(process.argv);
227
251
if ( ! process . argv . slice ( 2 ) . length ) {
228
252
program . outputHelp ( ) ;
229
253
}
230
-
231
- function camelize ( str ) {
232
- return str . replace ( / - ( \w ) / g, ( _ , c ) => c ? c . toUpperCase ( ) : '' ) ;
233
- }
234
-
235
- // commander passes the Command object itself as options,
236
- // extract only actual options into a fresh object.
237
- function cleanArgs ( cmd ) {
238
- const args = { } ;
239
- cmd . options . forEach ( o => {
240
- const key = camelize ( o . long . replace ( / ^ - - / , '' ) ) ;
241
- // if an option is not present and Command has a method with the same name
242
- // it should not be copied
243
- if ( typeof cmd [ key ] !== 'function' && typeof cmd [ key ] !== 'undefined' ) {
244
- args [ key ] = cmd [ key ] ;
245
- }
246
- } ) ;
247
- return args ;
248
- }
0 commit comments