Skip to content

Commit 1d7edfb

Browse files
committed
feat: cli build and serve support multi target
1 parent 3bcd412 commit 1d7edfb

File tree

4 files changed

+120
-38
lines changed

4 files changed

+120
-38
lines changed

packages/mars-cli/bin/mars-build.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* @file mars build
3+
* @author meixuguang
4+
*/
5+
6+
/* eslint-disable fecs-no-require */
7+
/* eslint-disable no-console */
8+
9+
const program = require('commander');
10+
const {defaultConfig, cleanArgs} = require('../lib/helper/utils');
11+
12+
program
13+
.description('build project in production mode')
14+
.option('-r, --registry <url>', 'Use specified npm registry when installing dependencies (only for npm)')
15+
.option('-t, --target <target>', 'Build target (swan | h5 | wx, default: swan)')
16+
.action(cmd => {
17+
const build = require('../lib/build');
18+
const options = cleanArgs(cmd);
19+
20+
if (!options.registry) {
21+
options.registry = defaultConfig.registry;
22+
}
23+
24+
build(options);
25+
})
26+
.parse(process.argv);

packages/mars-cli/bin/mars-serve.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* @file mars build
3+
* @author meixuguang
4+
*/
5+
6+
/* eslint-disable fecs-no-require */
7+
/* eslint-disable no-console */
8+
9+
const program = require('commander');
10+
const {defaultConfig, cleanArgs} = require('../lib/helper/utils');
11+
12+
program
13+
.description('serve project in development mode')
14+
.option('-r, --registry <url>', 'Use specified npm registry when installing dependencies (only for npm)')
15+
.option('-t, --target <target>', 'Build target (swan | h5 | wx, default: swan)')
16+
.action(cmd => {
17+
const start = require('../lib/serve');
18+
const options = cleanArgs(cmd);
19+
20+
if (!options.registry) {
21+
options.registry = defaultConfig.registry;
22+
}
23+
24+
start(options);
25+
})
26+
.parse(process.argv);

packages/mars-cli/bin/mars.js

Lines changed: 42 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ const fs = require('fs-extra');
1818
const path = require('path');
1919
const slash = require('slash');
2020
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');
2223

2324
function checkNodeVersion(wanted, id) {
2425
if (!semver.satisfies(process.version, wanted)) {
@@ -30,10 +31,6 @@ function checkNodeVersion(wanted, id) {
3031
}
3132
}
3233

33-
const defaultConfig = {
34-
registry: 'https://registry.npm.taobao.org'
35-
};
36-
3734
checkNodeVersion(requiredVersion, 'mars-cli');
3835

3936
if (semver.satisfies(process.version, '9.x')) {
@@ -152,31 +149,58 @@ program
152149
.option('-r, --registry <url>', 'Use specified npm registry when installing dependencies (only for npm)')
153150
.option('-t, --target <target>', 'Build target (swan | h5 | wx, default: swan)')
154151
.action(cmd => {
155-
const build = require('../lib/build');
156152
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+
}
157162

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+
});
163174
});
164175

165176
program
166177
.command('serve')
167178
.description('serve project in development mode')
168179
.option('-r, --registry <url>', 'Use specified npm registry when installing dependencies (only for npm)')
169180
.option('-t, --target <target>', 'Build target (swan | h5 | wx, default: swan)')
170-
// .option('-d, --dest <dir>', 'output directory (default: dist)')
171181
.action(cmd => {
172-
const start = require('../lib/serve');
173182
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+
}
174192

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+
});
180204
});
181205

182206
program
@@ -227,22 +251,3 @@ program.parse(process.argv);
227251
if (!process.argv.slice(2).length) {
228252
program.outputHelp();
229253
}
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-
}

packages/mars-cli/lib/helper/utils.js

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ const dependencyListH5 = [
2121
'@marsjs/vue-cli-plugin-mars-web'
2222
];
2323

24+
const defaultConfig = {
25+
registry: 'https://registry.npm.taobao.org'
26+
};
27+
2428
function getCliVersion() {
2529
return version !== undefined ? version : version = require('../../package').version;
2630
}
@@ -29,9 +33,30 @@ function isH5Project() {
2933
return isH5 !== undefined ? isH5 : isH5 = fs.existsSync(process.cwd() + '/vue.config.js');
3034
}
3135

36+
function camelize(str) {
37+
return str.replace(/-(\w)/g, (_, c) => (c ? c.toUpperCase() : ''));
38+
}
39+
40+
// commander passes the Command object itself as options,
41+
// extract only actual options into a fresh object.
42+
function cleanArgs(cmd) {
43+
const args = {};
44+
cmd.options.forEach(o => {
45+
const key = camelize(o.long.replace(/^--/, ''));
46+
// if an option is not present and Command has a method with the same name
47+
// it should not be copied
48+
if (typeof cmd[key] !== 'function' && typeof cmd[key] !== 'undefined') {
49+
args[key] = cmd[key];
50+
}
51+
});
52+
return args;
53+
}
54+
3255
module.exports = {
3356
dependencyList,
3457
dependencyListH5,
3558
getCliVersion,
36-
isH5Project
59+
isH5Project,
60+
defaultConfig,
61+
cleanArgs
3762
};

0 commit comments

Comments
 (0)