-
Notifications
You must be signed in to change notification settings - Fork 22
/
index.js
239 lines (185 loc) · 5.61 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
"use strict";
const { exec } = require("child_process");
const path = require("path");
const filterObject = require("object-filter");
const mapValues = require("map-values");
const parallel = require("run-parallel");
const semver = require("semver");
const tools = require("./tools");
const runningOnWindows = (process.platform === "win32");
const originalPath = process.env.PATH;
const pathSeparator = runningOnWindows ? ";" : ":";
const localBinPath = path.resolve("node_modules/.bin")
// ignore locally installed packages
const globalPath = originalPath
.split(pathSeparator)
.filter(p => path.resolve(p)!==localBinPath)
.join(pathSeparator)
;
module.exports = function check(wanted, callback) {
// Normalize arguments
if (typeof wanted === "function") {
callback = wanted;
wanted = null;
}
const options = { callback };
options.wanted = normalizeWanted(wanted);
options.commands = mapValues(
(
Object.keys(options.wanted).length
? filterObject(tools, (_, key) => options.wanted[key])
: tools
),
({ getVersion }) => ( runVersionCommand.bind(null, getVersion) )
);
if (runningOnWindows) {
runForWindows(options);
} else {
run(options);
}
}
function runForWindows(options) {
// See and understand https://github.com/parshap/check-node-version/issues/35
// before trying to optimize this function
//
// `chcp` is used instead of `where` on account of its more extensive availablity
// chcp: MS-DOS 6.22+, Windows 95+; where: Windows 7+
//
// Plus, in order to be absolutely certain, the error message of `where` would still need evaluation.
exec("chcp", (error, stdout) => {
const finalCallback = options.callback;
if (error) {
finalCallback(chcpError(error, 1));
return;
}
const codepage = stdout.match(/\d+/)[0];
if (codepage === "65001" || codepage === "437") {
// need not switch codepage
return run(options);
}
// reset codepage before exiting
options.callback = (...args) => exec(`chcp ${codepage}`, (error) => {
if (error) {
finalCallback(chcpError(error, 3));
return;
}
finalCallback(...args);
});
// switch to Unicode
exec("chcp 65001", (error) => {
if (error) {
finalCallback(chcpError(error, 2));
return;
}
run(options);
});
function chcpError(error, step) {
switch (step) {
case 1:
error.message = `[CHCP] error while getting current codepage:\n${error.message}`;
break;
case 2:
error.message = `[CHCP] error while switching to Unicode codepage:\n${error.message}`;
break;
case 3:
error.message = `
[CHCP] error while resetting current codepage:
${error.message}
Please note that your terminal is now using the Unicode codepage.
Therefore, codepage-dependent actions may work in an unusual manner.
You can run \`chcp ${codepage}\` yourself in order to reset your codepage,
or just close this terminal and work in another.
`.trim().replace(/^ +/gm,'') // strip indentation
break;
// no default
}
return error
}
});
}
function run({ commands, callback, wanted }) {
parallel(commands, (err, versionsResult) => {
if (err) {
callback(err);
return;
}
const versions = mapValues(versionsResult, ({ version, notfound, invalid }, name) => {
const programInfo = {
isSatisfied: true,
};
if (version) {
programInfo.version = semver(version);
}
if (invalid) {
programInfo.invalid = invalid;
}
if (notfound) {
programInfo.notfound = notfound;
}
if (wanted[name]) {
programInfo.wanted = new semver.Range(wanted[name]);
programInfo.isSatisfied = Boolean(
programInfo.version
&&
semver.satisfies(programInfo.version, programInfo.wanted)
);
}
return programInfo;
});
callback(null, {
versions: versions,
isSatisfied: Object.keys(wanted).every(name => versions[name].isSatisfied),
});
});
};
// Return object containing only keys that a program exists for and
// something valid was given.
function normalizeWanted(wanted) {
if (!wanted) {
return {};
}
// Validate keys
wanted = filterObject(wanted, Boolean);
// Normalize to strings
wanted = mapValues(wanted, String);
// Filter existing programs
wanted = filterObject(wanted, (_, key) => tools[key]);
return wanted;
}
function runVersionCommand(command, callback) {
process.env.PATH = globalPath;
exec(command, (execError, stdout, stderr) => {
const commandDescription = JSON.stringify(command);
if (!execError) {
const version = stdout.trim();
if (semver.valid(version)) {
return callback(null, {
version,
});
} else {
return callback(null, {
invalid: true,
})
}
}
if (toolNotFound(execError)) {
return callback(null, {
notfound: true,
});
}
// something went very wrong during execution
let errorMessage = `Command failed: ${commandDescription}`
if (stderr) {
errorMessage += `\n\nstderr:\n${stderr.toString().trim()}\n`;
}
errorMessage += `\n\noriginal error message:\n${execError.message}\n`;
return callback(new Error(errorMessage));
});
process.env.PATH = originalPath;
}
function toolNotFound(execError) {
if (runningOnWindows) {
return execError.message.includes("is not recognized");
}
return execError.code === 127;
}