-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.js
executable file
·255 lines (217 loc) · 6.38 KB
/
cli.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
#!/usr/bin/env node
var path = require('path');
var fs = require('fs');
var program = require('commander');
var npm = require('npm');
var ini = require('ini');
var echo = require('node-echo');
var extend = require('extend');
var open = require('open');
var async = require('async');
var request = require('request');
var only = require('only');
var registries = require('./registries.json');
var PKG = require('./package.json');
var NRMRC = path.join(process.env.HOME, '.nrmrc');
program
.version(PKG.version);
program
.command('ls')
.description('List all the registries')
.action(onList);
program
.command('current')
.description('Show current registry name')
.action(showCurrent);
program
.command('use <registry>')
.description('Change registry to registry')
.action(onUse);
program
.command('add <registry> <url> [home]')
.description('Add one custom registry')
.action(onAdd);
program
.command('del <registry>')
.description('Delete one custom registry')
.action(onDel);
program
.command('home <registry> [browser]')
.description('Open the homepage of registry with optional browser')
.action(onHome);
program
.command('test [registry]')
.description('Show response time for specific or all registries')
.action(onTest);
program
.command('help')
.description('Print this help')
.action(function () {
program.outputHelp();
});
program
.parse(process.argv);
if (process.argv.length === 2) {
program.outputHelp();
}
/*//////////////// cmd methods /////////////////*/
function onList() {
getCurrentRegistry(function(cur) {
var info = [''];
var allRegistries = getAllRegistry();
Object.keys(allRegistries).forEach(function(key) {
var item = allRegistries[key];
var prefix = item.registry === cur ? '* ' : ' ';
info.push(prefix + key + line(key, 8) + item.registry);
});
info.push('');
printMsg(info);
});
}
function showCurrent() {
getCurrentRegistry(function(cur) {
var allRegistries = getAllRegistry();
Object.keys(allRegistries).forEach(function(key) {
var item = allRegistries[key];
if (item.registry === cur) {
printMsg([key]);
return;
}
});
});
}
function onUse(name) {
var allRegistries = getAllRegistry();
if (allRegistries.hasOwnProperty(name)) {
var registry = allRegistries[name];
npm.load(function (err) {
if (err) return exit(err);
npm.commands.config(['set', 'registry', registry.registry], function (err, data) {
if (err) return exit(err);
console.log(' ');
var newR = npm.config.get('registry');
printMsg([
'', ' Registry has been set to: ' + newR, ''
]);
})
});
} else {
printMsg([
'', ' Not find registry: ' + name, ''
]);
}
}
function onDel(name) {
var customRegistries = getCustomRegistry();
if (!customRegistries.hasOwnProperty(name)) return;
getCurrentRegistry(function(cur) {
if (cur === customRegistries[name].registry) {
onUse('npm');
}
delete customRegistries[name];
setCustomRegistry(customRegistries, function(err) {
if (err) return exit(err);
printMsg([
'', ' delete registry ' + name + ' success', ''
]);
});
});
}
function onAdd(name, url, home) {
var customRegistries = getCustomRegistry();
if (customRegistries.hasOwnProperty(name)) return;
var config = customRegistries[name] = {};
if (url[url.length - 1] !== '/') url += '/'; // ensure url end with /
config.registry = url;
if (home) {
config.home = home;
}
setCustomRegistry(customRegistries, function(err) {
if (err) return exit(err);
printMsg([
'', ' add registry ' + name + ' success', ''
]);
});
}
function onHome(name, browser) {
var allRegistries = getAllRegistry();
var home = allRegistries[name] && allRegistries[name].home;
if (home) {
var args = [home];
if (browser) args.push(browser);
open.apply(null, args);
}
}
function onTest(registry) {
var allRegistries = getAllRegistry();
var toTest;
if (registry) {
if (!allRegistries.hasOwnProperty(registry)) {
return;
}
toTest = only(allRegistries, registry);
} else {
toTest = allRegistries;
}
async.map(Object.keys(toTest), function(name, cbk) {
var registry = toTest[name];
var start = +new Date();
request(registry.registry + 'pedding', function(error) {
cbk(null, {
name: name,
registry: registry.registry,
time: (+new Date() - start),
error: error ? true : false
});
});
}, function(err, results) {
getCurrentRegistry(function(cur) {
var msg = [''];
results.forEach(function(result) {
var prefix = result.registry === cur ? '* ' : ' ';
var suffix = result.error ? 'Fetch Error' : result.time + 'ms';
msg.push(prefix + result.name + line(result.name, 8) + suffix);
});
msg.push('');
printMsg(msg);
});
});
}
/*//////////////// helper methods /////////////////*/
/*
* get current registry
*/
function getCurrentRegistry(cbk) {
npm.load(function(err, conf) {
if (err) return exit(err);
cbk(npm.config.get('registry'));
});
}
function getCustomRegistry() {
return fs.existsSync(NRMRC) ? ini.parse(fs.readFileSync(NRMRC, 'utf-8')) : {};
}
function setCustomRegistry(config, cbk) {
echo(ini.stringify(config), '>', NRMRC, cbk);
}
function getAllRegistry() {
return extend({}, registries, getCustomRegistry());
}
function printErr(err) {
console.error('an error occured: ' + err);
}
function printMsg(infos) {
infos.forEach(function(info) {
console.log(info);
});
}
/*
* print message & exit
*/
function exit(err) {
printErr(err);
process.exit(1);
}
function line(str, len) {
var line = new Array(Math.max(1, len - str.length)).join('-');
return ' ' + line + ' ';
}