UNPKG

6.5 kBJavaScriptView Raw
1#!/usr/bin/env node
2var path = require('path');
3var fs = require('fs');
4var program = require('commander');
5var npm = require('npm');
6var ini = require('ini');
7var echo = require('node-echo');
8var extend = require('extend');
9var open = require('open');
10var async = require('async');
11var request = require('request');
12var only = require('only');
13
14var registries = require('./registries.json');
15var PKG = require('./package.json');
16var NRMRC = path.join(process.env.HOME, '.nrmrc');
17
18
19program
20 .version(PKG.version);
21
22program
23 .command('ls')
24 .description('List all the registries')
25 .action(onList);
26
27program
28 .command('current')
29 .description('Show current registry name')
30 .action(showCurrent);
31
32program
33 .command('use <registry>')
34 .description('Change registry to registry')
35 .action(onUse);
36
37program
38 .command('add <registry> <url> [home]')
39 .description('Add one custom registry')
40 .action(onAdd);
41
42program
43 .command('del <registry>')
44 .description('Delete one custom registry')
45 .action(onDel);
46
47program
48 .command('home <registry> [browser]')
49 .description('Open the homepage of registry with optional browser')
50 .action(onHome);
51
52program
53 .command('test [registry]')
54 .description('Show response time for specific or all registries')
55 .action(onTest);
56
57program
58 .command('help')
59 .description('Print this help')
60 .action(program.help);
61
62program
63 .parse(process.argv);
64
65
66if (process.argv.length === 2) {
67 program.outputHelp();
68}
69
70/*//////////////// cmd methods /////////////////*/
71
72function onList() {
73 getCurrentRegistry(function(cur) {
74 var info = [''];
75 var allRegistries = getAllRegistry();
76
77 Object.keys(allRegistries).forEach(function(key) {
78 var item = allRegistries[key];
79 var prefix = item.registry === cur ? '* ' : ' ';
80 info.push(prefix + key + line(key, 8) + item.registry);
81 });
82
83 info.push('');
84 printMsg(info);
85 });
86}
87
88function showCurrent() {
89 getCurrentRegistry(function(cur) {
90 var allRegistries = getAllRegistry();
91 Object.keys(allRegistries).forEach(function(key) {
92 var item = allRegistries[key];
93 if (item.registry === cur) {
94 printMsg([key]);
95 return;
96 }
97 });
98 });
99}
100
101function onUse(name) {
102 var allRegistries = getAllRegistry();
103 if (allRegistries.hasOwnProperty(name)) {
104 var registry = allRegistries[name];
105 npm.load(function (err) {
106 if (err) return exit(err);
107 npm.commands.config(['set', 'registry', registry.registry], function (err, data) {
108 if (err) return exit(err);
109 console.log(' ');
110 var newR = npm.config.get('registry');
111 printMsg([
112 '', ' Registry has been set to: ' + newR, ''
113 ]);
114 })
115 });
116 } else {
117 printMsg([
118 '', ' Not find registry: ' + name, ''
119 ]);
120 }
121}
122
123function onDel(name) {
124 var customRegistries = getCustomRegistry();
125 if (!customRegistries.hasOwnProperty(name)) return;
126 getCurrentRegistry(function(cur) {
127 if (cur === customRegistries[name].registry) {
128 onUse('npm');
129 }
130 delete customRegistries[name];
131 setCustomRegistry(customRegistries, function(err) {
132 if (err) return exit(err);
133 printMsg([
134 '', ' delete registry ' + name + ' success', ''
135 ]);
136 });
137 });
138}
139
140function onAdd(name, url, home) {
141 var customRegistries = getCustomRegistry();
142 if (customRegistries.hasOwnProperty(name)) return;
143 var config = customRegistries[name] = {};
144 if (url[url.length - 1] !== '/') url += '/'; // ensure url end with /
145 config.registry = url;
146 if (home) {
147 config.home = home;
148 }
149 setCustomRegistry(customRegistries, function(err) {
150 if (err) return exit(err);
151 printMsg([
152 '', ' add registry ' + name + ' success', ''
153 ]);
154 });
155}
156
157function onHome(name, browser) {
158 var allRegistries = getAllRegistry();
159 var home = allRegistries[name] && allRegistries[name].home;
160 if (home) {
161 var args = [home];
162 if (browser) args.push(browser);
163 open.apply(null, args);
164 }
165}
166
167function onTest(registry) {
168 var allRegistries = getAllRegistry();
169
170 var toTest;
171
172 if (registry) {
173 if (!allRegistries.hasOwnProperty(registry)) {
174 return;
175 }
176 toTest = only(allRegistries, registry);
177 } else {
178 toTest = allRegistries;
179 }
180
181 async.map(Object.keys(toTest), function(name, cbk) {
182 var registry = toTest[name];
183 var start = +new Date();
184 request(registry.registry + 'pedding', function(error) {
185 cbk(null, {
186 name: name,
187 registry: registry.registry,
188 time: (+new Date() - start),
189 error: error ? true : false
190 });
191 });
192 }, function(err, results) {
193 getCurrentRegistry(function(cur) {
194 var msg = [''];
195 results.forEach(function(result) {
196 var prefix = result.registry === cur ? '* ' : ' ';
197 var suffix = result.error ? 'Fetch Error' : result.time + 'ms';
198 msg.push(prefix + result.name + line(result.name, 8) + suffix);
199 });
200 msg.push('');
201 printMsg(msg);
202 });
203 });
204}
205
206
207
208/*//////////////// helper methods /////////////////*/
209
210/*
211 * get current registry
212 */
213function getCurrentRegistry(cbk) {
214 npm.load(function(err, conf) {
215 if (err) return exit(err);
216 cbk(npm.config.get('registry'));
217 });
218}
219
220function getCustomRegistry() {
221 return fs.existsSync(NRMRC) ? ini.parse(fs.readFileSync(NRMRC, 'utf-8')) : {};
222}
223
224function setCustomRegistry(config, cbk) {
225 echo(ini.stringify(config), '>', NRMRC, cbk);
226}
227
228function getAllRegistry() {
229 return extend({}, registries, getCustomRegistry());
230}
231
232function printErr(err) {
233 console.error('an error occured: ' + err);
234}
235
236function printMsg(infos) {
237 infos.forEach(function(info) {
238 console.log(info);
239 });
240}
241
242/*
243 * print message & exit
244 */
245function exit(err) {
246 printErr(err);
247 process.exit(1);
248}
249
250function line(str, len) {
251 var line = new Array(Math.max(1, len - str.length)).join('-');
252 return ' ' + line + ' ';
253}