Skip to content

Commit d6863d1

Browse files
committed
feat: mars-cli update
1 parent d4d933d commit d6863d1

File tree

2 files changed

+107
-0
lines changed

2 files changed

+107
-0
lines changed

packages/mars-cli/bin/mars.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,22 @@ program
178178
start(options);
179179
});
180180

181+
program
182+
.command('update')
183+
.description('update all mars dependences')
184+
.option('-r, --registry <url>', 'Use specified npm registry when installing dependencies (only for npm)')
185+
.option('-f, --force', 'Force update all denpenences to latest version')
186+
.action(cmd => {
187+
const update = require('../lib/update');
188+
const options = cleanArgs(cmd);
189+
190+
if (!options.registry) {
191+
options.registry = defaultConfig.registry;
192+
}
193+
194+
update(options);
195+
});
196+
181197
// output help information on unknown commands
182198
program
183199
.arguments('<command>')

packages/mars-cli/lib/update.js

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/**
2+
* @file 升级依赖
3+
* @author meixuguang
4+
*/
5+
6+
const {error, stopSpinner} = require('@vue/cli-shared-utils');
7+
const execa = require('execa');
8+
const fs = require('fs-extra');
9+
10+
function executeCommand(command, args, targetDir) {
11+
return new Promise((resolve, reject) => {
12+
13+
const child = execa(command, args, {
14+
cwd: targetDir,
15+
stdio: ['inherit', 'inherit', 'inherit']
16+
});
17+
18+
child.on('close', code => {
19+
if (code !== 0) {
20+
reject(`command failed: ${command} ${args.join(' ')}`);
21+
return;
22+
}
23+
resolve();
24+
});
25+
});
26+
}
27+
28+
const dependencyList = [
29+
'@marsjs/build',
30+
'@marsjs/core'
31+
];
32+
const dependencyListH5 = [
33+
'@marsjs/components',
34+
'@marsjs/api',
35+
'atom-web-compiler',
36+
'atom2vue-loader',
37+
'@marsjs/vue-cli-plugin-mars-web'
38+
];
39+
40+
function getDependencyList() {
41+
if (fs.existsSync(process.cwd() + '/vue.config.js')) {
42+
return [...dependencyList, ...dependencyListH5];
43+
}
44+
return dependencyList;
45+
}
46+
47+
async function update(cmd) {
48+
if (cmd.force) {
49+
forceUpdate(cmd.registry);
50+
}
51+
else {
52+
compatibleUpdate(cmd.registry);
53+
}
54+
}
55+
56+
async function compatibleUpdate(registry) {
57+
const dep = getDependencyList();
58+
const args = [
59+
'update',
60+
...dep,
61+
`--registry=${registry}`
62+
];
63+
64+
return executeCommand('npm', args, process.cwd());
65+
}
66+
67+
async function forceUpdate(registry) {
68+
const args = [
69+
'install'
70+
];
71+
const dep = getDependencyList();
72+
73+
for (let i = 0; i < dep.length; i++) {
74+
const id = dep[i];
75+
args.push(`${id}@latest`);
76+
}
77+
args.push(`--registry=${registry}`);
78+
79+
return executeCommand('npm', args, process.cwd());
80+
}
81+
82+
83+
84+
module.exports = (...args) =>
85+
update(...args).catch(err => {
86+
stopSpinner(false); // do not persist
87+
error(err);
88+
if (!process.env.VUE_CLI_TEST) {
89+
process.exit(1);
90+
}
91+
});

0 commit comments

Comments
 (0)