This repository has been archived by the owner on Dec 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
231 lines (208 loc) · 5.29 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
'use strict';
var util = require('util');
var semver = require('semver');
var child = require('child_process');
var concat = require('concat-stream');
var Promise = require('bluebird');
var name = require('./package.json').name;
var VERSION_REGEX = /(\w+)-(.+)/;
/**
* Get the system version of Node, if present.
* Otherwise, return a rejected Promise.
*/
var getSystemNode = function() {
return new Promise(function(resolve, reject) {
var stdout, stderr;
var cmd = child.spawn(process.env.SHELL,
['-c', 'source $NVM_DIR/nvm.sh; nvm run --silent system --version;']);
cmd.stdout.pipe(concat(function(data) {
stdout = data;
}));
cmd.stderr.pipe(concat(function(data) {
stderr = data;
}));
cmd.on('close', function(code) {
if (code === 0) { resolve({ stdout: stdout, stderr: stderr }); }
else {
reject(new Error('Could not find system version of node.'));
}
});
});
};
/**
* Run an nvm command.
*
* @private
* @function
* @param {String} command
* @return {Promise}
*/
var nvmCommand = function(command) {
return new Promise(function(resolve, reject) {
var stdout, stderr;
var cmd = child.spawn(process.env.SHELL,
['-c', 'source $NVM_DIR/nvm.sh; nvm ' + command]);
cmd.stdout.pipe(concat(function(data) {
stdout = data;
}));
cmd.stderr.pipe(concat(function(data) {
stderr = data;
}));
cmd.on('close', function(code) {
if (code === 0) { resolve({ stdout: stdout, stderr: stderr }); }
else {
reject(new Error(util.format('nvm exited with status: %d\n%s',
code, String(stdout).trim() + String(stderr).trim())));
}
});
});
};
/**
* Prase versions
*
* @private
* @function
* @param {String} output
* @return {Array.<String>}
*/
var parseVersions = function(output) {
var string = String(output.stdout)
.replace(/\x1b[^m]*m/g, '')
.replace(/^->/gm, '');
return string.split('\n')
.map(function(line) { return line.trim(); })
.filter(function(line) { return line && !line.match(/current|system|->/); });
};
/**
* List versions.
*
* @private
* @function
* @return {Promise}
*/
var listVersions = function() {
// find all of the versions of node installed by nvm.
return Promise.resolve()
.then(function() { return nvmCommand('list'); })
.then(parseVersions);
};
/**
* Extract a name from a version (to support iojs)
*
* @private
* @function
* @return {Promise}
*/
var versionName = function(version) {
var match = version.match(VERSION_REGEX);
return match ? match[1] : null;
};
/**
* Extract just the version number from a version.
*
* @private
* @function
* @param {String} version
* @return {String}
*/
var versionNumber = function(version) {
var match = version.match(VERSION_REGEX);
return match ? match[2] : version;
};
/**
* Find a version.
*
* @param {Array.<String>} versions
* @param {String} matching
* @return {String}
*/
var findVersion = function(versions, matching) {
if (matching === 'system') {
return matching;
}
var highestMatch = null;
var mName = versionName(matching);
var mNumber = versionNumber(matching);
versions.forEach(function(v) {
var vName = versionName(v);
var vNumber = versionNumber(v);
if (vName === mName && semver.satisfies(vNumber, mNumber)) {
if (!highestMatch) { highestMatch = v; }
else if (semver.gt(vNumber, versionNumber(highestMatch))) {
highestMatch = v;
}
}
});
return highestMatch;
};
/**
* Parse the results of the nvm version call.
*
* @param {Promise} matching
*/
var parseMatching = function(matching) {
return String(matching.stdout).trim();
};
/**
* Use nvm to resolve a version string (which could be a version number or an
alias) to an installed version number (or N/A).
*
* @param {String} matching
*/
var resolveVersion = function(matching) {
return Promise.resolve()
.then(function() { return nvmCommand('version "' + matching + '"'); })
.then(parseMatching);
};
/**
* Get installed version matching a given version.
*
* @param {String} matching
* @return {Promise}
*/
var installedVersion = function(matching) {
return Promise.resolve()
.then(function() {
return Promise.all([
resolveVersion(matching),
listVersions(),
]);
})
.spread(function(parsedVersion, versions) {
var parsedMatching = parsedVersion !== 'N/A' ? parsedVersion : matching;
return findVersion(versions, parsedMatching);
});
};
/**
* Match a specific version.
*
* @param {String} version
* @return {Promise}
*/
var match = function(version) {
return Promise.resolve()
.then(function() { return installedVersion(version); })
.then(function(useVersion) {
return useVersion === 'system' ?
getSystemNode()
.then(parseMatching)
.then(function(use) {
return 'system: ' + use;
}) :
useVersion;
})
.then(function(useVersion) {
var use = (useVersion && useVersion.indexOf('system') === 0) ?
'system' : useVersion;
var command = util.format('nvm use %s > /dev/null;', use);
var result = { version: useVersion, command: command };
return use ? result :
Promise.reject(new Error('no version matching ' + version));
});
};
module.exports = {
name: name,
match: match,
_parseVersions: parseVersions,
_findVersion: findVersion,
};