This repository was archived by the owner on Aug 25, 2022. It is now read-only.
forked from JS-DevTools/npm-publish
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse-args.ts
73 lines (66 loc) · 1.96 KB
/
parse-args.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import * as commandLineArgs from "command-line-args";
import { Access, Options } from "../options";
import { ExitCode } from "./exit-code";
import { usageText } from "./help";
/**
* The parsed command-line arguments
* @internal
*/
export interface ParsedArgs {
help?: boolean;
version?: boolean;
options: Options;
}
/**
* Parses the command-line arguments
* @internal
*/
export function parseArgs(argv: string[]): ParsedArgs {
try {
let args = commandLineArgs(
[
{ name: "token", type: String },
{ name: "registry", type: String },
{ name: "package", type: String, defaultOption: true },
{ name: "tag", type: String },
{ name: "access", type: String },
{ name: "dry-run", type: Boolean },
{ name: "debug", alias: "d", type: Boolean },
{ name: "quiet", alias: "q", type: Boolean },
{ name: "version", alias: "v", type: Boolean },
{ name: "help", alias: "h", type: Boolean },
],
{ argv }
);
if (args.token === null) {
throw new SyntaxError("The --token argument requires a value");
}
if (args.registry === null) {
throw new SyntaxError("The --registry argument requires a value");
}
let parsedArgs: ParsedArgs = {
help: args.help as boolean,
version: args.version as boolean,
options: {
token: args.token as string,
registry: args.registry as string,
package: args.package as string,
tag: args.tag as string,
access: args.access as Access,
dryRun: args["dry-run"] as boolean,
debug: args.debug ? console.debug : undefined,
quiet: args.quiet as boolean,
}
};
return parsedArgs;
}
catch (error) {
// There was an error parsing the command-line args
return errorHandler(error as Error);
}
}
function errorHandler(error: Error): never {
console.error(error.message);
console.error(usageText);
return process.exit(ExitCode.InvalidArgument);
}