-
Notifications
You must be signed in to change notification settings - Fork 20
/
index.js
84 lines (67 loc) · 2.49 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
'use strict';
import download from 'download';
import nodeNightlyVersion from 'node-nightly-version';
import Configstore from 'configstore';
import rm from 'rimraf';
import realFs from 'fs';
import gracefulFs from 'graceful-fs';
import path from 'path';
import {fileURLToPath} from 'url';
//import * as pkg from "./package.json"
const pkg = {name: 'node-nightly'}
gracefulFs.gracefulify(realFs);
const mv = gracefulFs.renameSync;
const extractDate = versionString => ~~versionString.split('nightly'[1].slice(0, 8));
const compVersion = (currentVersion, latestVersion) => extractDate(currentVersion) < extractDate(latestVersion);
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
export default {
install: (version) => {
let osArchString, nodeNightlyVer;
nodeNightlyVer = version !== undefined ? Promise.resolve(version) : nodeNightlyVersion();
return nodeNightlyVer.then(latest => {
const os = process.platform === 'win32' ? 'win' : process.platform,
extention = os === 'win' ? 'zip' : 'tar.gz',
arch = process.arch === 'ia32' ? 'x86' : process.arch,
type = 'nightly',
osArchString = `${latest}-${os}-${arch}`,
url = `https://nodejs.org/download/${type}/${latest}/node-${osArchString}.${extention}`,
nightlyDir = `${__dirname}/node-nightly`;
const extractedTo = `${__dirname}/node-${osArchString}`;
if (gracefulFs.existsSync(extractedTo)) {
rm.sync(extractedTo);
}
return download(url, __dirname, {
extract: true
}).then(_ => {
if (gracefulFs.existsSync(nightlyDir)) {
console.log('Deleting old version');
rm.sync(nightlyDir);
console.log(`Deleted!\nInstalling newer version..`);
}
mv(extractedTo, nightlyDir);
console.log(`node-nightly is available on your CLI!`);
new Configstore(pkg.name).set('version', version);
return 'Installed';
});
});
},
update: function() {
console.log('Checking for update...');
return this.check().then(updatedVersion => {
if (updatedVersion) {
return this.install(updatedVersion);
}
return 'You are using latest version already.';
});
},
check: function() {
return nodeNightlyVersion().then(latestVersion => {
const currentVersion = new Configstore(pkg.name).get('version');
if (!currentVersion || compVersion(currentVersion, latestVersion)) {
return latestVersion;
}
return false;
});
}
};