Skip to content

Commit 2f6d1c7

Browse files
Cleaned-up error-handling code
1 parent 0b3b230 commit 2f6d1c7

3 files changed

Lines changed: 73 additions & 73 deletions

File tree

src/cli/exit-code.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/**
2+
* CLI exit codes.
3+
*
4+
* @see https://nodejs.org/api/process.html#process_exit_codes
5+
*/
6+
export enum ExitCode {
7+
Success = 0,
8+
FatalError = 1,
9+
InvalidArgument = 9
10+
}

src/cli/index.ts

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,11 @@
22
import { success } from "log-symbols";
33
import { manifest } from "../manifest";
44
import { versionBump } from "../version-bump";
5-
import { VersionBumpOptions } from "../version-bump-options";
65
import { ProgressEvent, VersionBumpProgress } from "../version-bump-progress";
6+
import { ExitCode } from "./exit-code";
77
import { helpText } from "./help";
88
import { parseArgs } from "./parse-args";
99

10-
/**
11-
* @see https://nodejs.org/api/process.html#process_exit_codes
12-
*/
13-
enum ExitCode {
14-
Success = 0,
15-
FatalError = 1,
16-
InvalidArgument = 9,
17-
}
18-
1910
/**
2011
* The main entry point of the CLI
2112
*
@@ -45,21 +36,9 @@ export async function main(args: string[]): Promise<void> {
4536
options.progress = progress;
4637
}
4738

48-
await bump(options);
39+
await versionBump(options);
4940
}
5041
}
51-
catch (error) {
52-
// There was an error parsing the command-line args
53-
console.error((error as Error).message);
54-
console.error(helpText);
55-
process.exit(ExitCode.InvalidArgument);
56-
}
57-
}
58-
59-
async function bump(options: VersionBumpOptions): Promise<void> {
60-
try {
61-
await versionBump(options);
62-
}
6342
catch (error) {
6443
errorHandler(error as Error);
6544
}

src/cli/parse-args.ts

Lines changed: 61 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1+
// tslint:disable: no-console
12
import * as commandLineArgs from "command-line-args";
23
import * as semver from "semver";
34
import { isReleaseType } from "../release-type";
45
import { VersionBumpOptions } from "../version-bump-options";
6+
import { ExitCode } from "./exit-code";
7+
import { helpText } from "./help";
58

69
/**
710
* The parsed command-line arguments
@@ -17,62 +20,70 @@ export interface ParsedArgs {
1720
* Parses the command-line arguments
1821
*/
1922
export function parseArgs(argv: string[]): ParsedArgs {
20-
let args = commandLineArgs(
21-
[
22-
{ name: "preid", type: String },
23-
{ name: "commit", alias: "c", type: String },
24-
{ name: "tag", alias: "t", type: String },
25-
{ name: "push", alias: "p", type: Boolean },
26-
{ name: "all", alias: "a", type: Boolean },
27-
{ name: "no-verify", type: Boolean },
28-
{ name: "quiet", alias: "q", type: Boolean },
29-
{ name: "version", alias: "v", type: Boolean },
30-
{ name: "help", alias: "h", type: Boolean },
31-
{ name: "files", type: String, multiple: true, defaultOption: true },
32-
],
33-
{ argv }
34-
);
23+
try {
24+
let args = commandLineArgs(
25+
[
26+
{ name: "preid", type: String },
27+
{ name: "commit", alias: "c", type: String },
28+
{ name: "tag", alias: "t", type: String },
29+
{ name: "push", alias: "p", type: Boolean },
30+
{ name: "all", alias: "a", type: Boolean },
31+
{ name: "no-verify", type: Boolean },
32+
{ name: "quiet", alias: "q", type: Boolean },
33+
{ name: "version", alias: "v", type: Boolean },
34+
{ name: "help", alias: "h", type: Boolean },
35+
{ name: "files", type: String, multiple: true, defaultOption: true },
36+
],
37+
{ argv }
38+
);
3539

36-
let parsedArgs: ParsedArgs = {
37-
help: args.help as boolean,
38-
version: args.version as boolean,
39-
quiet: args.quiet as boolean,
40-
options: {
41-
preid: args.preid as string,
42-
commit: args.commit as string | boolean,
43-
tag: args.tag as string | boolean,
44-
push: args.push as boolean,
45-
all: args.all as boolean,
46-
noVerify: args["no-verify"] as boolean,
47-
files: args.files as string[],
48-
}
49-
};
40+
let parsedArgs: ParsedArgs = {
41+
help: args.help as boolean,
42+
version: args.version as boolean,
43+
quiet: args.quiet as boolean,
44+
options: {
45+
preid: args.preid as string,
46+
commit: args.commit as string | boolean,
47+
tag: args.tag as string | boolean,
48+
push: args.push as boolean,
49+
all: args.all as boolean,
50+
noVerify: args["no-verify"] as boolean,
51+
files: args.files as string[],
52+
}
53+
};
5054

51-
// If --preid is used without an argument, then throw an error, since it's probably a mistake.
52-
// If they want to use the default value ("beta"), then they should not pass the argument at all
53-
if (args.preid === null) {
54-
throw new Error(`The --preid option requires a value, such as "alpha", "beta", etc.`);
55-
}
55+
// If --preid is used without an argument, then throw an error, since it's probably a mistake.
56+
// If they want to use the default value ("beta"), then they should not pass the argument at all
57+
if (args.preid === null) {
58+
throw new Error(`The --preid option requires a value, such as "alpha", "beta", etc.`);
59+
}
5660

57-
// If --commit is used without an argument, then treat it as a boolean flag
58-
if (args.commit === null) {
59-
parsedArgs.options.commit = true;
60-
}
61+
// If --commit is used without an argument, then treat it as a boolean flag
62+
if (args.commit === null) {
63+
parsedArgs.options.commit = true;
64+
}
6165

62-
// If --tag is used without an argument, then treat it as a boolean flag
63-
if (args.tag === null) {
64-
parsedArgs.options.tag = true;
65-
}
66+
// If --tag is used without an argument, then treat it as a boolean flag
67+
if (args.tag === null) {
68+
parsedArgs.options.tag = true;
69+
}
6670

67-
// If a version number or release type was specified, then it will mistakenly be added to the "files" array
68-
if (parsedArgs.options.files && parsedArgs.options.files.length > 0) {
69-
let firstArg = parsedArgs.options.files[0];
71+
// If a version number or release type was specified, then it will mistakenly be added to the "files" array
72+
if (parsedArgs.options.files && parsedArgs.options.files.length > 0) {
73+
let firstArg = parsedArgs.options.files[0];
7074

71-
if (firstArg === "prompt" || isReleaseType(firstArg) || semver.valid(firstArg)) {
72-
parsedArgs.options.release = firstArg;
73-
parsedArgs.options.files.shift();
75+
if (firstArg === "prompt" || isReleaseType(firstArg) || semver.valid(firstArg)) {
76+
parsedArgs.options.release = firstArg;
77+
parsedArgs.options.files.shift();
78+
}
7479
}
75-
}
7680

77-
return parsedArgs;
81+
return parsedArgs;
82+
}
83+
catch (error) {
84+
// There was an error parsing the command-line args
85+
console.error((error as Error).message);
86+
console.error(helpText);
87+
return process.exit(ExitCode.InvalidArgument);
88+
}
7889
}

0 commit comments

Comments
 (0)