forked from cnpm/cnpm
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathorigin_npm.js
131 lines (110 loc) · 2.95 KB
/
origin_npm.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
/**!
* cnpm - origin_npm.js
*
* Copyright(c) cnpmjs.org and other contributors.
* MIT Licensed
*
* Authors:
* fengmk2 <[email protected]> (http://fengmk2.com)
* dead_horse <[email protected]> (http://deadhorse.me)
*/
'use strict';
/**
* Module dependencies.
*/
require('colors');
var debug = require('debug')('cnpm:origin');
var match = require('auto-correct');
var spawn = require('cross-spawn');
var fs = require('fs');
var path = require('path');
var config = require('./config');
var parseArgv = require('./parse_argv');
var program = parseArgv();
var args = program.rawArgs.slice(2);
var hasCNPM = false;
for (var i = 0; i < args.length; i++) {
if (args[i][0] !== '-') {
args[i] = correct(args[i]);
if (args[i] === 'cnpm') {
hasCNPM = true;
}
}
}
var CWD = process.cwd();
if (program.userconfig && !fs.existsSync(program.userconfig)) {
// make sure userconfig exists
// or it will throw: npm ERR! Error: default value must be string or number
fs.writeFileSync(program.userconfig, 'email =\n');
}
var nodeModulesDir = path.join(__dirname, 'node_modules');
var pangyp = path.join(nodeModulesDir, 'pangyp', 'bin', 'node-gyp.js');
args.unshift('--node-gyp=' + pangyp);
args.unshift('--registry=' + program.registry);
args.unshift('--cache=' + program.cache);
args.unshift('--disturl=' + program.disturl);
args.unshift('--userconfig=' + program.userconfig);
var originalNpmBin = path.join(path.dirname(process.execPath), 'npm');
// node-pre-gyp will try to resolve node_modules/npm, so rename it
// see https://github.com/mapbox/node-pre-gyp/issues/144
function findNpmBin() {
var npmBin;
try {
npmBin = require.resolve('npm');
// $HOME/git/smart-npm/node_modules/npm/lib/npm.js
npmBin = path.join(npmBin, '..', '..', '..', '.bin', 'npm');
} catch (_) {
npmBin = originalNpmBin;
}
return npmBin;
}
var npmBin = findNpmBin();
// if local npm not exists, use npm. happen on `$ cnpm install cnpm`
if (hasCNPM) {
npmBin = originalNpmBin;
}
debug('%s %s', npmBin, args.join(' '));
var env = {
NVM_NODEJS_ORG_MIRROR: config.mirrorsUrl + '/node',
NVM_IOJS_ORG_MIRROR: config.mirrorsUrl + '/iojs',
PHANTOMJS_CDNURL: config.mirrorsUrl + '/phantomjs',
CHROMEDRIVER_CDNURL: config.mirrorsUrl + '/chromedriver',
SELENIUM_CDNURL: config.mirrorsUrl + '/selenium',
// set npm config: always-auth be true
// NPM_CONFIG_ALWAYS_AUTH: 'true',
};
for (var k in process.env) {
env[k] = process.env[k];
}
var npm = spawn(npmBin, args, {
env: env,
cwd: CWD,
stdio: [
process.stdin,
process.stdout,
process.stderr,
]
});
npm.on('exit', function (code, signal) {
process.exit(code);
});
/**
* correct command
* @return {[type]} [description]
*/
function correct(command) {
var cmds = [
'install',
'publish',
'adduser',
'author',
'config',
'unpublish',
];
for (var i = 0; i < cmds.length; i++) {
if (match(command, cmds[i])) {
return cmds[i];
}
}
return command;
}