|
| 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