-
Notifications
You must be signed in to change notification settings - Fork 156
/
Copy pathinstall.js
66 lines (54 loc) · 1.37 KB
/
install.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import which from 'which';
function runCmd(cmd, args, fn) {
args = args || [];
var runner = require('child_process').spawn(cmd, args, {
// keep color
stdio: "inherit"
});
runner.on('close', function (code) {
if (fn) {
fn(code);
}
});
}
function findExe(exes) {
for (var i = 0; i < exes.length; i++) {
try {
which.sync(exes[i]);
console.log('use pkg manager: ' + exes[i]);
return exes[i];
} catch (e) {
}
}
return null;
}
function findYarn() {
var yarnExes = process.platform === 'win32' ? ['yarn.cmd', 'yarnpkg.cmd'] : ['yarn', 'yarnpkg'];
return findExe(yarnExes);
}
function findNpm() {
var npmExes = process.platform === 'win32' ? ['tnpm.cmd', 'cnpm.cmd', 'npm.cmd'] : ['tnpm', 'cnpm', 'npm'];
return findExe(npmExes);
}
export default function (done) {
const yarn = findYarn();
if (yarn) {
runCmd(which.sync(yarn), ['install'], function () {
runCmd(which.sync(yarn), ['add', 'dva'], function () {
console.log(yarn + ' install end');
done();
});
});
} else {
const npm = findNpm();
if (!npm) {
throw new Error('please install yarn or npm');
}
runCmd(which.sync(npm), ['install'], function () {
runCmd(which.sync(npm), ['install', 'dva', '--save'], function () {
console.log(npm + ' install end');
done();
});
});
}
};