-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
61 lines (50 loc) · 1.35 KB
/
index.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
/**
* Created by axetroy on 17-3-10.
*/
const path = require('path');
const spawn = require('child_process').spawn;
const Promise = require('bluebird');
const co = require('co');
const fs = Promise.promisifyAll(require('fs-extra'));
const which$0 = require('which');
function isYarnEnv() {
return co(function *() {
yield [
fs.readFileAsync(path.join('yarn.lock')),
which$1('yarn')
];
return true;
}).catch((err) => {
console.error(err);
return Promise.resolve(false);
});
}
function which$1(cmd, options = {}) {
return new Promise(function (resolve, reject) {
which$0.call(this, cmd, options, function (err, resolvePath) {
if (err) return reject(err);
resolve(resolvePath);
});
});
}
function nodeModuleInstall(done) {
co(function*() {
try {
yield fs.readJsonAsync(path.join('package.json'));
} catch (err) {
throw new Error('Invalid package.json');
}
const __isYarnEnv__ = yield isYarnEnv();
let cmd = [];
if (__isYarnEnv__) {
cmd = ['yarn']
} else {
cmd = ['npm', 'install']
}
const ls = spawn(cmd.shift(), cmd, {stdio: 'inherit'});
ls.on('close', (code, signal) => {
code === 0 ? done() : done(new Error(`Error Code: ${code}, Exist Signal: ${signal}`));
});
}).catch((err) => done(err));
}
exports.add = nodeModuleInstall;