-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.ts
56 lines (49 loc) · 1.3 KB
/
index.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import { myLibrary } from "..";
import { ExitCode } from "./exit-code";
import { helpText } from "./help";
import { manifest } from "./manifest";
import { parseArgs } from "./parse-args";
/**
* The main entry point of the CLI
*
* @param args - The command-line arguments
*/
export function main(args: string[]): void {
try {
// Setup global error handlers
process.on("uncaughtException", errorHandler);
process.on("unhandledRejection", errorHandler);
// Parse the command-line arguments
let { help, version, quiet, options } = parseArgs(args);
if (help) {
// Show the help text and exit
console.log(helpText);
process.exit(ExitCode.Success);
}
else if (version) {
// Show the version number and exit
console.log(manifest.version);
process.exit(ExitCode.Success);
}
else {
let result = myLibrary(options);
if (!quiet) {
console.log(result);
}
}
}
catch (error) {
errorHandler(error as Error);
}
}
/**
* Prints errors to the console
*/
function errorHandler(error: Error): void {
let message = error.message || String(error);
if (process.env.DEBUG || process.env.NODE_ENV === "development") {
message = error.stack || message;
}
console.error(message);
process.exit(ExitCode.FatalError);
}