forked from cnpm/cnpm
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathparse_argv.js
97 lines (86 loc) · 2.78 KB
/
parse_argv.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
/**!
* cnpm - parse_argv.js
*
* Copyright(c) cnpmjs.org and other contributors.
* MIT Licensed
*
* Authors:
* fengmk2 <[email protected]> (http://fengmk2.github.com)
* dead_horse <[email protected]> (http://deadhorse.me)
*/
'use strict';
/**
* Module dependencies.
*/
var fs = require('fs');
var program = require('commander');
var config = require('./config');
var pkg = require('./package.json');
var help = require('./help');
var argv = null;
module.exports = function (cmd) {
if (!argv) {
argv = program.version(pkg.version, '-v, --version')
.option('-r, --registry [registry]', 'registry url, default is ' + config.cnpmRegistry)
.option('-w, --registryweb [registryweb]', 'web url, default is ' + config.cnpmHost)
.option('--disturl [disturl]', 'dist url for node-gyp, default is ' + config.disturl)
.option('-c, --cache [cache]', 'cache folder, default is ' + config.cache)
.option('-u, --userconfig [userconfig]', 'userconfig file, default is ' + config.userconfig)
.option('-y, --yes', 'yes all confirm');
}
if (cmd === 'doc') {
argv.option('-g, --git', '[doc options] open git url');
} else if (cmd === 'sync') {
argv.option('--sync-publish', '[sync options] sync as publish')
.option('--no-deps', '[sync options] do not sync dependencies and devDependencies');
}
// commander's bug, fix here
// https://github.com/visionmedia/commander.js/pull/189
var cacheInfo;
argv.on('cache', function (cache) {
if (typeof cache === 'string') {
cacheInfo = cache;
return;
}
argv.args = ['cache'].concat(cache || []);
});
// custom help message
// output command help, default options help info will output by default
argv.on('--help', function () {
if (!argv.registry) {
argv.userconfig = argv.userconfig || config.userconfig;
argv.registry = getDefaultRegistry(argv.userconfig);
}
help(argv);
});
argv.parse(process.argv.slice());
argv.userconfig = argv.userconfig || config.userconfig;
if (!argv.registry) {
// try to use registry in uerconfig
argv.registry = getDefaultRegistry(argv.userconfig);
}
if (!argv.disturl) {
var isIOJS = process.execPath.indexOf('iojs') >= 0;
argv.disturl = isIOJS ? config.iojsDisturl : config.disturl;
}
if (argv.disturl === 'none') {
delete argv.disturl;
}
argv.registryweb = argv.registryweb || config.cnpmHost;
argv.cache = cacheInfo || config.cache;
if (!argv.args.length) {
help(argv);
}
return argv;
};
function getDefaultRegistry(userconfig) {
if (fs.existsSync(argv.userconfig)) {
var content = fs.readFileSync(argv.userconfig, 'utf8');
// registry = {registry-url}
var m = /^registry\s*=\s*(.+)$/m.exec(content);
if (m) {
return m[1];
}
}
return config.cnpmRegistry;
}