-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
65 lines (51 loc) · 1.58 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
'use strict';
var assert = require('assert');
var braces = require('braces');
var npmCliPath = require('npm-cli-path');
var path = require('path');
var pify = require('pify');
var Promise = require('pinkie-promise');
var readPackageJson = pify(require('read-package-json'), Promise, { include: [] });
var spawn = require('child_process').spawn;
module.exports = function (args) {
var NPM_MAJOR_MIN = 2;
var NPM_MAJOR_MAX = 6;
var npmPackage = 'npm';
var npmVersion = parseInt(args[0]);
if (npmVersion >= NPM_MAJOR_MIN && npmVersion <= NPM_MAJOR_MAX) {
args.shift();
npmPackage = '@ex-machine/npm' + npmVersion;
}
var expandedArgs = args
.map(function (arg) {
return braces.expand(arg);
})
.reduce(function (args, arg) {
return args.concat(arg);
}, []);
var npmPathPromise;
if (npmPackage === 'npm') {
npmPathPromise = npmCliPath();
} else {
npmPathPromise = readPackageJson(require.resolve(npmPackage + '/package.json'), console.error, false)
.then(function (data) {
assert.ok(data.bin, 'data.bin');
var npmBin = Object.keys(data.bin)
.filter(function (bin) {
return /^npm-?\d$/.test(bin);
})[0];
assert.ok(npmBin, 'npmBin');
var npmBinPath = data.bin[npmBin];
assert.ok(npmBinPath, 'npmBinPath');
return require.resolve(path.join(npmPackage, npmBinPath));
});
}
npmPathPromise
.then(function (npmPath) {
spawn(process.execPath, [npmPath].concat(expandedArgs), { stdio: 'inherit' })
.on('exit', function (code) {
process.exit(code);
});
})
.catch(console.error);
};