forked from angular/dev-infra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
yargs.ts
39 lines (34 loc) · 1.2 KB
/
yargs.ts
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
import yargs, {Arguments, Argv} from 'yargs';
import {Log} from './logging.js';
// A function to be called when the command completes.
type CompletedFn = (err: Error | null) => Promise<void> | void;
/** List of functions to be called upon command completion. */
const completedFunctions: CompletedFn[] = [];
/** Register a function to be called when the command completes. */
export function registerCompletedFunction(fn: CompletedFn) {
completedFunctions.push(fn);
}
/** Error to be thrown when yargs completes without running a command. */
export class YargsError extends Error {
constructor() {
super('Error is parse or validation of command');
}
}
/**
* Run the yargs process, as configured by the supplied function, calling a set of completion
* functions after the command completes.
*/
export function runParserWithCompletedFunctions(applyConfiguration: (argv: Argv) => Argv) {
applyConfiguration(yargs([])).parse(
process.argv.slice(2),
async (err: Error | null, _: Arguments, output: string) => {
if (output) {
err = new YargsError();
Log.log(output);
}
for (const completedFunc of completedFunctions) {
await completedFunc(err);
}
},
);
}