-
Notifications
You must be signed in to change notification settings - Fork 1
/
cmd.js
executable file
·208 lines (187 loc) · 5.78 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#!/usr/bin/env node
/**
* An executable command which will be added to $PATH.
*
* @copyright Copyright 2017 Kevin Locke <kevin@kevinlocke.name>
* @license MIT
*/
'use strict';
const Yargs = require('yargs/yargs');
const packageJson = require('../package.json');
const modulename = require('..');
/** Calls <code>yargs.parse</code> and passes any thrown errors to the callback.
* Workaround for https://github.com/yargs/yargs/issues/755
* @private
*/
function parseYargs(yargs, args, callback) {
// Since yargs doesn't nextTick its callback, this function must be careful
// that exceptions thrown from callback (which propagate through yargs.parse)
// are not caught and passed to a second invocation of callback.
let called = false;
try {
yargs.parse(args, function(...cbargs) {
called = true;
return callback.apply(this, cbargs);
});
} catch (err) {
if (called) {
// err was thrown after or by callback. Let it propagate.
throw err;
} else {
callback(err);
}
}
}
/** Options for command entry points.
*
* @typedef {{
* in: (stream.Readable|undefined),
* out: (stream.Writable|undefined),
* err: (stream.Writable|undefined)
* }} CommandOptions
* @property {stream.Readable=} in Stream from which input is read. (default:
* <code>process.stdin</code>)
* @property {stream.Writable=} out Stream to which output is written.
* (default: <code>process.stdout</code>)
* @property {stream.Writable=} err Stream to which errors (and non-output
* status messages) are written. (default: <code>process.stderr</code>)
*/
// const CommandOptions;
/** Entry point for this command.
*
* @param {!Array<string>} args Command-line arguments.
* @param {CommandOptions=} options Options.
* @param {?function(Error, number=)=} callback Optional callback for the exit
* code or an <code>Error</code>.
* @return {Promise<number>|undefined} If <code>callback</code> is not given,
* a <code>Promise</code> with the exit code or <code>Error</code>.
*/
function modulenameCmd(args, options, callback) {
if (!callback && typeof options === 'function') {
callback = options;
options = null;
}
if (!callback) {
return new Promise((resolve, reject) => {
modulenameCmd(args, options, (err, result) => {
if (err) { reject(err); } else { resolve(result); }
});
});
}
if (typeof callback !== 'function') {
throw new TypeError('callback must be a function');
}
try {
if (args === undefined || args === null) {
args = [];
} else if (typeof args !== 'object'
|| Math.floor(args.length) !== args.length) {
throw new TypeError('args must be Array-like');
} else if (args.length < 2) {
throw new RangeError('args must have at least 2 elements');
} else {
args = Array.prototype.slice.call(args, 2).map(String);
}
if (options !== undefined && typeof options !== 'object') {
throw new TypeError('options must be an object');
}
options = Object.assign(
{
in: process.stdin,
out: process.stdout,
err: process.stderr
},
options
);
if (!options.in || typeof options.in.on !== 'function') {
throw new TypeError('options.in must be a stream.Readable');
}
if (!options.out || typeof options.out.write !== 'function') {
throw new TypeError('options.out must be a stream.Writable');
}
if (!options.err || typeof options.err.write !== 'function') {
throw new TypeError('options.err must be a stream.Writable');
}
} catch (err) {
process.nextTick(() => {
callback(err);
});
return undefined;
}
// Workaround for https://github.com/yargs/yargs/issues/783
// Necessary because mocha package.json overrides .parserConfiguration()
require.main = module;
const yargs = new Yargs(null, null, require)
.parserConfiguration({
'parse-numbers': false,
'duplicate-arguments-array': false,
'flatten-duplicate-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();
parseYargs(yargs, args, (err, argOpts, output) => {
if (err) {
if (output) {
options.err.write(`${output}\n`);
} else {
options.err.write(`${err.name}: ${err.message}\n`);
}
callback(null, 1);
return;
}
if (output) {
options.out.write(`${output}\n`);
}
if (argOpts.help || argOpts.version) {
callback(null, 0);
return;
}
if (argOpts._.length !== 1) {
options.err.write('Error: Exactly one argument is required.\n');
callback(null, 1);
return;
}
// Parse arguments then call API function with parsed options
const cmdOpts = {
files: argOpts._,
verbosity: argOpts.verbose - argOpts.quiet
};
modulename(cmdOpts, callback);
});
return undefined;
}
modulenameCmd.default = modulenameCmd;
module.exports = modulenameCmd;
if (require.main === module) {
// This file was invoked directly.
/* eslint-disable no-process-exit */
const mainOptions = {
in: process.stdin,
out: process.stdout,
err: process.stderr
};
modulenameCmd(process.argv, mainOptions, (err, exitCode) => {
if (err) {
if (err.stdout) { process.stdout.write(err.stdout); }
if (err.stderr) { process.stderr.write(err.stderr); }
process.stderr.write(`${err.name}: ${err.message}\n`);
exitCode = typeof err.exitCode === 'number' ? err.exitCode : 1;
}
process.exit(exitCode);
});
}