-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmd.js
executable file
·139 lines (124 loc) · 3.84 KB
/
cmd.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
#!/usr/bin/env node
/**
* An executable command which will be added to $PATH.
*
* @copyright Copyright 2017-2019 Kevin Locke <kevin@kevinlocke.name>
* @license MIT
*/
'use strict';
const Yargs = require('yargs/yargs');
const packageJson = require('../package.json');
const modulename = require('..');
/** Options for command entry points.
*
* @typedef {{
* stdin: !module:stream.Readable,
* stdout: !module:stream.Writable,
* stderr: !module:stream.Writable
* }} CommandOptions
* @property {!module:stream.Readable} stdin Stream from which input is read.
* @property {!module:stream.Writable} stdout Stream to which output is
* written.
* @property {!module:stream.Writable} stderr Stream to which errors and
* non-output status messages are written.
*/
// const CommandOptions;
/** Entry point for this command.
*
* @param {Array<string>} args Command-line arguments.
* @param {!CommandOptions} options Options.
* @param {function(number)} callback Callback with exit code.
*/
function modulenameCmd(args, options, callback) {
if (typeof callback !== 'function') {
throw new TypeError('callback must be a function');
}
if (args !== undefined
&& args !== null
&& Math.floor(args.length) !== args.length) {
throw new TypeError('args must be Array-like');
}
if (!options || typeof options !== 'object') {
throw new TypeError('options must be an object');
}
if (!options.stdin || typeof options.stdin.on !== 'function') {
throw new TypeError('options.stdin must be a stream.Readable');
}
if (!options.stdout || typeof options.stdout.write !== 'function') {
throw new TypeError('options.stdout must be a stream.Writable');
}
if (!options.stderr || typeof options.stderr.write !== 'function') {
throw new TypeError('options.stderr must be a stream.Writable');
}
if (args.length >= 2) {
// Strip "node" and script name, ensure args are strings
args = Array.prototype.slice.call(args, 2).map(String);
} else {
args = [];
}
// Workaround for https://github.com/yargs/yargs/issues/783
// Necessary because mocha package.json overrides .parserConfiguration()
require.main = module;
const yargs = new Yargs(undefined, undefined, require)
.parserConfiguration({
'parse-numbers': false,
'duplicate-arguments-array': false,
'flatten-duplicate-arrays': false,
'greedy-arrays': false,
})
.usage('Usage: $0 [options] [args...]')
.help()
.alias('help', 'h')
.alias('help', '?')
.option('quiet', {
alias: 'q',
describe: 'Print less output',
count: true,
})
.option('verbose', {
alias: 'v',
describe: 'Print more output',
count: true,
})
.version(`${packageJson.name} ${packageJson.version}`)
.alias('version', 'V')
.strict();
yargs.parse(args, (err, argOpts, output) => {
if (err) {
if (output) {
options.stderr.write(`${output}\n`);
} else {
options.stderr.write(`${err.name}: ${err.message}\n`);
}
callback(undefined, 1);
return;
}
if (output) {
options.stdout.write(`${output}\n`);
}
if (argOpts.help || argOpts.version) {
callback(0);
return;
}
if (argOpts._.length !== 1) {
options.stderr.write('Error: Exactly one argument is required.\n');
callback(1);
return;
}
// Parse arguments then call API function with parsed options
const cmdOpts = {
files: argOpts._,
verbosity: argOpts.verbose - argOpts.quiet,
};
modulename(cmdOpts, callback);
});
}
modulenameCmd.default = modulenameCmd;
module.exports = modulenameCmd;
if (require.main === module) {
// This file was invoked directly.
// Note: Could pass process.exit as callback to force immediate exit.
modulenameCmd(process.argv, process, (exitCode) => {
process.exitCode = exitCode;
});
}