-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathinstall.js
More file actions
72 lines (66 loc) · 2.05 KB
/
install.js
File metadata and controls
72 lines (66 loc) · 2.05 KB
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
67
68
69
70
71
// const { exec, spawn } = require('child_process');
const { exec: e, spawn: s } = require('child_process');
function exec (cmd, cb) {
e(cmd.replace('dc-api-cli', 'node /Volumes/DATA/Projects/Node/dc-api-cli/index.js'), cb);
}
function spawn (cmd, args, opts) {
if (cmd == 'dc-api-cli') {
return s('node', ['/Volumes/DATA/Projects/Node/dc-api-cli/index.js', ...args], opts);
} else {
return s(cmd, args, opts);
}
}
function prompt (question, list) {
return new Promise(resolve => {
process.stdout.write(question + ' ');
process.stdin.resume();
process.stdin.once('data', chunk => {
process.stdin.pause();
const answer = chunk.toString().replace(/\r?\n/, '').toLowerCase();
if (list && !~list.indexOf(answer)) prompt(question, list).then(resolve);
else resolve(answer);
});
});
}
function init () {
prompt('Init dc-api-core project for you? [Y/n]:', ['', 'y', 'n']).then(isInit => {
if (!isInit || isInit == 'y') {
console.log('\n$ dc-api-cli init');
spawn('dc-api-cli', ['init'], { stdio: 'inherit' });
}
});
}
exec('dc-api-cli --version', (err, stdout, stderr) => {
if (err || stderr) {
prompt('Install dc-api-cli globally (not required for production)? [Y/n]:', ['', 'y', 'n']).then(answer => {
if (!answer || answer == 'y') {
prompt('Select package manager\n1) npm\n2) Yarn\nAnswer:', ['1', '2']).then(pm => {
switch (pm) {
case '1':
pm = 'npm install --global dc-api-cli';
break;
case '2':
pm = 'yarn global add dc-api-cli';
break;
}
console.log('\n$ ' + pm);
pm = pm.split(' ');
const pmProcess = spawn(pm[0], pm.slice(1), { stdio: 'inherit' });
pmProcess.once('exit', code => {
if (code) {
prompt('Package manager exited with error, continue? [y/N]:', ['', 'y', 'n']).then(isContinue => {
if (isContinue == 'y') init();
});
} else {
init();
}
});
});
}
});
} else {
stdout = stdout.toString().replace(/\r?\n/, '');
console.log('Found dc-api-cli v' + stdout + ', skipping installation');
init();
}
});