xdc-npm
Version:
xnpm 命令行客户端
119 lines (108 loc) • 3.57 kB
JavaScript
/** !
* Copyright(c) cnpmjs.org and other contributors.
* MIT Licensed
*
* Authors:
* fengmk2 <fengmk2@gmail.com> (http://fengmk2.com)
* dead_horse <dead_horse@qq.com> (http://deadhorse.me)
*/
const fs = require('fs');
const program = require('commander');
const config = require('./config');
const pkg = require('./package.json');
const help = require('./help');
let argv = null;
// eslint-disable-next-line complexity
module.exports = function (cmd) {
if (!argv) {
argv = program.version(pkg.version, '-v, --version')
.option('-r, --registry [registry]', `registry url, default is ${config.xnpmRegistry}`)
.option('-w, --registryweb [registryweb]', `web url, default is ${config.xnpmHost}`)
.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')
.option('--proxy [proxy]', 'set a http proxy, no default');
}
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
let cacheInfo;
argv.on('cache', (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', () => {
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) {
const isIOJS = process.execPath.indexOf('iojs') >= 0;
argv.disturl = isIOJS ? config.iojsDisturl : config.disturl;
}
if (!argv.proxy) {
argv.proxy = config.proxy;
}
if (argv.disturl === 'none') {
delete argv.disturl;
}
if (argv.userconfig === 'none') {
delete argv.userconfig;
}
argv.registryweb = argv.registryweb || config.xnpmHost;
argv.cache = cacheInfo || config.cache;
if (!argv.args.length) {
help(argv);
}
// filter rawArgs
const { rawArgs } = argv;
const needs = [];
for (let i = 0; i < rawArgs.length; i++) {
const arg = rawArgs[i];
if (arg.indexOf('--userconfig=') === 0 || arg.indexOf('-u=') === 0) {
continue;
}
if (arg.indexOf('--disturl=') === 0) {
continue;
}
if (arg.indexOf('--registryweb=') === 0 || arg.indexOf('-w=') === 0) {
continue;
}
if (arg.indexOf('--registry=') === 0 || arg.indexOf('-r=') === 0) {
continue;
}
needs.push(arg);
}
argv.rawArgs = needs;
return argv;
};
function getDefaultRegistry(userconfig) {
if (argv.userconfig !== 'none' && fs.existsSync(argv.userconfig)) {
const content = fs.readFileSync(argv.userconfig, 'utf8');
// registry = {registry-url}
const m = /^registry\s*=\s*(.+)$/m.exec(content);
if (m) {
return m[1];
}
}
return config.xnpmRegistry;
}