-
-
Notifications
You must be signed in to change notification settings - Fork 151
/
index.js
49 lines (41 loc) · 1.18 KB
/
index.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
'use strict';
var path = require('path');
var minimist = require('minimist');
var indentString = require('indent-string');
var objectAssign = require('object-assign');
var camelcaseKeys = require('camelcase-keys');
// needed to get the uncached parent
delete require.cache[__filename];
var parentDir = path.dirname(module.parent.filename);
module.exports = function (opts, minimistOpts) {
opts = objectAssign({
pkg: './package.json',
argv: process.argv.slice(2)
}, opts);
if (Array.isArray(opts.help)) {
opts.help = opts.help.join('\n');
}
var pkg = typeof opts.pkg === 'string' ? require(path.join(parentDir, opts.pkg)) : opts.pkg;
var argv = minimist(opts.argv, minimistOpts);
var help = '\n' + indentString(pkg.description + (opts.help ? '\n\n' + opts.help : '\n'), ' ');
var showHelp = function () {
console.log(help);
process.exit();
};
if (argv.version && opts.version !== false) {
console.log(typeof opts.version === 'string' ? opts.version : pkg.version);
process.exit();
}
if (argv.help && opts.help !== false) {
showHelp();
}
var _ = argv._;
delete argv._;
return {
input: _,
flags: camelcaseKeys(argv),
pkg: pkg,
help: help,
showHelp: showHelp
};
};