-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathbin.ts
executable file
·204 lines (165 loc) · 4.9 KB
/
bin.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#!/usr/bin/env deno
import { join } from "jsr:@std/[email protected]";
import { Changelog, parser, Release } from "./mod.ts";
import { parseArgs } from "jsr:@std/[email protected]/parse-args";
import { parse as parseIni } from "jsr:@std/[email protected]";
import getSettingsForURL from "./src/settings.ts";
const argv = parseArgs(Deno.args, {
default: {
file: "CHANGELOG.md",
format: "compact",
release: null,
create: null,
url: null,
https: true,
quiet: false,
head: null,
"bullet-style": "-",
},
string: ["file", "format", "url", "head", "bullet-style"],
boolean: ["https", "init", "latest-release", "quiet", "help"],
alias: {
h: "help",
},
});
const file = join(Deno.cwd(), argv.file);
try {
if (argv.help) {
showHelp();
Deno.exit(0);
}
if (argv.init) {
const changelog = new Changelog("Changelog").addRelease(
new Release("0.1.0", new Date(), "First version"),
);
changelog.format = argv.format as "compact" | "markdownlint";
changelog.bulletStyle = argv['bullet-style'] as "-" | "*" | "+";
save(file, changelog, true);
Deno.exit(0);
}
const changelog = parser(Deno.readTextFileSync(file));
changelog.format = argv.format as "compact" | "markdownlint";
changelog.bulletStyle = argv['bullet-style'] as "-" | "*" | "+";
if (argv["no-v-prefix"]) {
changelog.tagNameBuilder = (release) => String(release.version);
}
if (argv["latest-release"]) {
const release = changelog.releases.find((release) =>
release.date && release.version
);
if (release) {
console.log(release.version?.toString());
}
Deno.exit(0);
}
if (argv.release) {
const release = changelog.releases.find((release) => {
if (release.date) {
return false;
}
if (typeof argv.release === "string") {
return !release.version || argv.release === release.version.toString();
}
return !!release.version;
});
if (release) {
release.date = new Date();
if (typeof argv.release === "string") {
release.setVersion(argv.release);
}
} else {
console.error("Not found any valid unreleased version");
Deno.exit(1);
}
}
if (argv.create) {
const version = typeof argv.create === "string" ? argv.create : undefined;
changelog.addRelease(new Release(version));
}
save(file, changelog);
} catch (err) {
console.error(red((err as Error).message));
if (!argv.quiet) {
Deno.exit(1);
}
}
function save(file: string, changelog: Changelog, isNew = false) {
changelog.url = argv.url || changelog.url || getRemoteUrl(argv.https);
if (!changelog.url) {
console.error(
red(
'Please, set the repository url with --url="https://github.com/username/repository"',
),
);
changelog.url = "https://example.com";
}
if (changelog.url) {
const settings = getSettingsForURL(changelog.url);
if (settings) {
changelog.head = settings.head;
changelog.tagLinkBuilder = settings.tagLink;
}
}
if (argv.head) {
changelog.head = argv.head;
}
Deno.writeTextFileSync(file, changelog.toString());
if (isNew) {
console.log(green("Generated new file"), file);
} else {
console.log(green("Updated file"), file);
}
}
function red(message: string) {
return "\u001b[" + 31 + "m" + message + "\u001b[" + 39 + "m";
}
function green(message: string) {
return "\u001b[" + 32 + "m" + message + "\u001b[" + 39 + "m";
}
function normalizeUrl(url: string, https: boolean) {
// remove .git suffix
url = url.replace(/\.git$/, "");
// normalize git@host urls
if (url.startsWith("git@")) {
url = url.replace(
/^git@([^:]+):(.*)$/,
(https ? "https" : "http") + "://$1/$2",
);
}
// remove trailing slashes
url = url.replace(/\/+$/, "");
return new URL(url);
}
function getRemoteUrl(https = true) {
try {
const file = join(Deno.cwd(), ".git", "config");
const content = Deno.readTextFileSync(file);
const data = parseIni(content);
const origin = data['remote "origin"'] as { url?: string };
if (!origin?.url) {
return;
}
return normalizeUrl(origin.url, https).href;
} catch (err) {
console.error(red((err as Error).message));
// Ignore
}
}
function showHelp() {
console.log(`keep-a-changelog
Usage: keep-a-changelog [options]
Options:
--file, -f Changelog file (default: CHANGELOG.md)
--format Output format (default: compact)
--bullet-style Bullet point style (default: -)
--url Repository URL
--init Initialize a new changelog file
--latest-release Print the latest release version
--release Set the date of the specified release
--create Create a new release
--no-v-prefix Do not add a "v" prefix to the version
--head Set the HEAD link
--quiet Do not print errors
--help, -h Show this help message
`);
}