-
Notifications
You must be signed in to change notification settings - Fork 23
/
index.js
137 lines (128 loc) · 3.71 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
"use strict";
var exec = require("child_process").exec;
var semver = require("semver");
var parallel = require("run-parallel");
var mapValues = require("map-values");
var filterObject = require("object-filter");
var assign = require("object.assign");
var PROGRAMS = {
node: {
getVersion: runVersionCommand.bind(null, "node --version"),
getInstallInstructions: function(v) {
return "To install node, run `nvm install " + v +
"` or see https://nodejs.org/";
}
},
npm: {
getVersion: runVersionCommand.bind(null, "npm --version"),
getInstallInstructions: function(v) {
return "To install npm, run `npm install -g npm@" + v + "`";
}
},
yarn: {
getVersion: runVersionCommand.bind(null, "yarn --version"),
getInstallInstructions: function(v) {
return "To install yarn, see https://yarnpkg.com/lang/en/docs/install/";
}
},
};
function runVersionCommand(command, callback) {
exec(command, function(execError, stdin, stderr) {
var commandDescription = JSON.stringify(command);
if (execError && execError.code === 127) {
return callback(null, {
notfound: true,
});
}
else if (execError) {
var runError = new Error("Command failed: " + commandDescription);
if (stderr) {
runError.stderr = stderr.trim();
}
if (execError) {
runError.execError = execError;
}
return callback(null, {
error: runError,
});
}
else {
return callback(null, {
version: stdin.toString().split('\n')[0].trim(),
});
}
});
}
// Return object containing only keys that a program exists for and
// something valid was given.
function normalizeWanted(wanted) {
wanted = wanted || {};
// Validate keys
wanted = filterObject(wanted, Boolean);
// Normalize to strings
wanted = mapValues(wanted, String);
// Filter existing programs
wanted = filterObject(wanted, function(version, key) {
return PROGRAMS[key];
});
return wanted;
}
function normalizeOptions(options) {
return assign({
getVersion: defaultGetVersion,
}, options);
}
function defaultGetVersion(name, callback) {
PROGRAMS[name].getVersion(callback);
}
module.exports = function check(wanted, options, callback) {
// Normalize arguments
if (typeof wanted === "function") {
callback = wanted;
wanted = null;
}
if (typeof options === "function") {
callback = options;
options = null;
}
wanted = normalizeWanted(wanted);
options = normalizeOptions(options);
var commands = mapValues(PROGRAMS, function(program, name) {
return options.getVersion.bind(null, name);
});
parallel(commands, function(err, versionsResult) {
if (err) {
callback(err);
}
else {
var versions = mapValues(PROGRAMS, function(program, name) {
var programInfo = {};
if (versionsResult[name].error) {
programInfo.error = versionsResult[name].error;
}
if (versionsResult[name].version) {
programInfo.version = semver(versionsResult[name].version);
}
if (versionsResult[name].notfound) {
programInfo.notfound = versionsResult[name].notfound;
}
programInfo.isSatisfied = true;
if (wanted[name]) {
programInfo.wanted = new semver.Range(wanted[name]);
programInfo.isSatisfied = !! (
programInfo.version &&
semver.satisfies(programInfo.version, programInfo.wanted)
);
}
return programInfo;
});
callback(null, {
versions: versions,
isSatisfied: Object.keys(wanted).every(function(name) {
return versions[name].isSatisfied;
}),
});
}
});
};
module.exports.PROGRAMS = PROGRAMS;