-
Notifications
You must be signed in to change notification settings - Fork 2
/
cli.ts
140 lines (127 loc) · 2.88 KB
/
cli.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
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
import { parseArgs } from "@std/cli/parse-args";
import { red } from "@std/fmt/colors";
import { readConfig } from "./read_config.ts";
import { performCommit } from "./perform_commit.ts";
import { AppError, VersionInfo } from "./models.ts";
const NAME = "bmp";
const VERSION = "0.2.3";
function usage() {
console.log(`
Usage: bmp [options]
Options:
-i, --init Initializes the config file.
--info Shows the version information.
-c, --commit Commits changes with commit message (requires git command)
-p, --patch Bumps patch (0.0.1) level
-m, --minor Bumps minor (0.1.0) level
-j, --major Bumps major (1.0.0) level
--preid <label> Sets the pre-release version id (e.g. alpha, beta.1)
-r, --release Removes the pre-release version id
-h, --help Shows this help and exit
-v, --version Shows the version of this command and exit
`);
}
type CliArgs = {
info: boolean;
init: boolean;
commit: boolean;
patch: boolean;
minor: boolean;
major: boolean;
preid: string;
release: boolean;
help: boolean;
version: boolean;
};
export async function main(args: string[]): Promise<number> {
const {
init,
info: _isInfo,
commit,
patch,
minor,
major,
preid,
release,
help,
version,
} = parseArgs(args, {
boolean: [
"version",
"help",
"commit",
"major",
"minor",
"patch",
"release",
"info",
"init",
],
string: ["preid"],
alias: {
v: "version",
h: "help",
c: "commit",
j: "major",
m: "minor",
p: "patch",
i: "init",
},
});
if (version) {
console.log(`${NAME}@${VERSION}`);
return 0;
}
if (help) {
usage();
return 0;
}
let versionInfo: VersionInfo;
if (init) {
try {
const f = await Deno.open(".bmp.yml");
console.log(red("Error: .bmp.yml file already exists"));
f.close();
return 1;
} catch (e) {
if (e.name === "NotFound") {
console.log("Creating .bmp.yml file");
await VersionInfo.createDefault().save();
console.log("Done");
return 0;
}
throw e;
}
}
try {
versionInfo = await readConfig();
} catch (e) {
if (e instanceof AppError) {
console.log(red(e.message));
return 1;
}
throw e;
}
if (major) {
versionInfo.major();
} else if (minor) {
versionInfo.minor();
} else if (patch) {
versionInfo.patch();
} else if (preid) {
versionInfo.preid(preid);
} else if (release) {
versionInfo.release();
}
if (versionInfo.isUpdated()) {
console.log(versionInfo.bumpSummary());
await versionInfo.performUpdate();
}
if (commit) {
await performCommit(versionInfo);
} else if (!versionInfo.isUpdated()) {
console.log(versionInfo.toString());
}
return 0;
}
Deno.exit(await main(Deno.args));