-
Notifications
You must be signed in to change notification settings - Fork 20
/
cli.js
executable file
·61 lines (53 loc) · 1.7 KB
/
cli.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
#!/usr/bin/env node
'use strict';
import {spawn} from 'cross-spawn';
import nodeNightly from './index.js';
import gracefulFs from 'graceful-fs';
import isOnline from 'is-online';
import path from 'path';
import {fileURLToPath} from 'url';
const {existsSync} = gracefulFs;
const os = process.platform === 'win32' ? 'win' : process.platform;
let args = process.argv.slice(2);
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
// Check for upgrade.
let upgradeIndex = args.indexOf('--upgrade'),
versionIndex = args.indexOf('--version'),
version = versionIndex < 0 ? null : args[versionIndex + 1];
if (version) {
nodeNightly.install(version).catch(console.error);
} else if (!!~upgradeIndex) {
reportIfOffline();
nodeNightly.update().then(res => {
if (res !== 'Installed') {
console.log(res);
}
process.exit(0);
}).catch(console.error);
} else if (!existsSync(`${__dirname}/node-nightly`)) {
console.log('Downloading the nightly version, hang on...');
reportIfOffline();
//First install
nodeNightly.install().catch(console.error);
} else {
nodeNightly.check().then(updatedVersion => {
if (updatedVersion) {
console.log('\x1b[36m', 'New nightly available. To upgrade: `node-nightly --upgrade`' ,'\x1b[0m');
}
if (os === 'win') {
spawn(`${__dirname}/node-nightly/node`, args, {stdio: 'inherit', env: process.env});
} else {
spawn(`${__dirname}/node-nightly/bin/node`, args, {stdio: 'inherit', env: process.env});
}
}).catch(console.error);
}
function reportIfOffline() {
isOnline((err, online) => {
if (!online) {
//offline
console.info('\x1b[31m', 'Please check your internet connectivity.','\x1b[0m');
process.exit(0);
}
});
}