-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·65 lines (56 loc) · 1.64 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
#!/usr/bin/env node
const https = require('https');
const semver = require('semver');
const process = require('process');
const childProcess = require('child_process');
const fs = require('fs');
const os = require('os');
const isWindows = os.platform() === 'win32';
const errorExit = (err) => {
console.error(err.message);
process.exit(1);
};
const executeNvm = (command, version) => {
return childProcess.spawnSync(isWindows ? 'nvm' : '. $NVM_DIR/nvm.sh; nvm', [command, version], {
shell: true,
stdio: 'inherit',
});
};
const cwd = process.cwd();
if (fs.existsSync(cwd + '/package.json')) {
const pkg = require(cwd + '/package.json');
if (!pkg.engines || !pkg.engines.node) {
errorExit(new Error("Cannot find supported version of node in 'engines' field of package.json"));
} else {
https
.get('https://nodejs.org/dist/index.json', (response) => {
let data = '';
response.on('data', (chunk) => {
data += chunk;
});
response.on('end', () => {
try {
const versions = JSON.parse(data).map(({ version }) =>
version.startsWith('v') ? version.substring(1) : version
);
const version = semver.maxSatisfying(versions, pkg.engines.node);
if (version) {
executeNvm('install', version.toString());
if (isWindows) {
executeNvm('use', version.toString());
}
} else {
throw new Error('Cannot find supported version of node@' + pkg.engines.node);
}
} catch (err) {
errorExit(err);
}
});
})
.on('error', (err) => {
errorExit(err);
});
}
} else {
errorExit(new Error('No package.json file found in current working directory'));
}