-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.ts
executable file
·425 lines (368 loc) · 12.5 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
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
#!/usr/bin/env -S node --experimental-strip-types --no-warnings
import {execa} from "execa";
import minimist from "minimist";
import {basename, dirname, join, relative} from "node:path";
import {cwd, exit as doExit} from "node:process";
import {platform} from "node:os";
import {readFileSync, writeFileSync, accessSync, truncateSync, statSync} from "node:fs";
import pkg from "./package.json" with {type: "json"};
import type {Opts as MinimistOpts} from "minimist";
export type SemverLevel = "patch" | "minor" | "major";
const packageVersion = pkg.version || "0.0.0";
const esc = (str: string) => str.replace(/[|\\{}()[\]^$+*?.-]/g, "\\$&");
const semverRe = /^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$/;
const isSemver = (str: string) => semverRe.test(str.replace(/^v/, ""));
const pwd = cwd();
function uniq<T extends any[]>(arr: T): T {
return Array.from(new Set(arr)) as T;
}
const minOpts: MinimistOpts = {
boolean: [
"a", "all",
"D", "dry",
"g", "gitless",
"h", "help",
"P", "packageless",
"p", "prefix",
"v", "version",
],
string: [
"b", "base",
"c", "command",
"d", "date",
"r", "replace",
"m", "message",
"_",
],
alias: {
a: "all",
b: "base",
c: "command",
d: "date",
D: "dry",
g: "gitless",
h: "help",
m: "message",
p: "prefix",
r: "replace",
v: "version",
}
};
function replaceTokens(str: string, newVersion: string) {
const [major, minor, patch] = newVersion.split(".");
return str
.replace(/_VER_/g, newVersion)
.replace(/_MAJOR_/g, major)
.replace(/_MINOR_/g, minor)
.replace(/_PATCH_/g, patch);
}
function incrementSemver(str: string, level: SemverLevel) {
if (!isSemver(str)) throw new Error(`Invalid semver: ${str}`);
if (level === "major") return str.replace(/([0-9]+)\.[0-9]+\.[0-9]+(.*)/, (_, m1, m2) => {
return `${Number(m1) + 1}.0.0${m2}`;
});
if (level === "minor") return str.replace(/([0-9]+\.)([0-9]+)\.[0-9]+(.*)/, (_, m1, m2, m3) => {
return `${m1}${Number(m2) + 1}.0${m3}`;
});
return str.replace(/([0-9]+\.[0-9]+\.)([0-9]+)(.*)/, (_, m1, m2, m3) => {
return `${m1}${Number(m2) + 1}${m3}`;
});
}
function find(filename: string, dir: string, stopDir?: string) {
const path = join(dir, filename);
try {
accessSync(path);
return path;
} catch {}
const parent = dirname(dir);
if ((stopDir && path === stopDir) || parent === dir) {
return null;
} else {
return find(filename, parent, stopDir);
}
}
async function run(cmd: string | string[], {silent = false, input}: {silent?: boolean, input?: any} = {}) {
let child;
if (Array.isArray(cmd)) {
const [c, ...args] = cmd;
child = execa(c, args, {input});
} else {
child = execa(cmd, {shell: true, input});
}
if (!silent) child.stdout.pipe(process.stdout);
if (!silent) child.stderr.pipe(process.stderr);
return await child;
}
async function removeIgnoredFiles(files: string[]) {
let stdout;
try {
({stdout} = await run(["git", "check-ignore", "--", ...files], {silent: true}));
} catch {
return files;
}
const ignoredFiles = new Set(stdout.split(/\r?\n/));
return files.filter(file => !ignoredFiles.has(file));
}
type GetFileChangesOpts = {
file: string,
baseVersion: string,
newVersion: string,
replacements?: {re: RegExp | string, replacement: string}[],
date?: string,
}
function getFileChanges({file, baseVersion, newVersion, replacements, date}: GetFileChangesOpts) {
const oldData = readFileSync(file, "utf8");
const fileName = basename(file);
let newData;
if (fileName === "package.json") {
const re = new RegExp(`("version":[^]*?")${esc(baseVersion)}(")`);
newData = oldData.replace(re, (_, p1, p2) => `${p1}${newVersion}${p2}`);
} else if (fileName === "package-lock.json") {
// special case for package-lock.json which contains a lot of version
// strings which make regexp replacement risky.
newData = JSON.parse(oldData);
if (newData.version) newData.version = newVersion; // v1 and v2
if (newData?.packages?.[""]?.version) newData.packages[""].version = newVersion; // v2 and v3
newData = `${JSON.stringify(newData, null, 2)}\n`;
} else if (fileName === "pyproject.toml") {
const re = new RegExp(`(^version ?= ?["'])${esc(baseVersion)}(["'].*)`, "gm");
newData = oldData.replace(re, (_, p1, p2) => `${p1}${newVersion}${p2}`);
} else {
const re = new RegExp(esc(baseVersion), "g");
newData = oldData.replace(re, newVersion);
}
if (date) {
const re = /([^0-9]|^)[0-9]{4}-[0-9]{2}-[0-9]{2}([^0-9]|$)/g;
newData = newData.replace(re, (_, p1, p2) => `${p1}${date}${p2}`);
}
if (replacements?.length) {
for (const replacement of replacements) {
newData = newData.replace(replacement.re, replacement.replacement);
}
}
if (oldData === newData) {
throw new Error(`No replacement made in ${file} for base version ${baseVersion}`);
} else {
return [file, newData];
}
}
function write(file: string, content: string) {
if (platform() === "win32") {
try {
truncateSync(file);
writeFileSync(file, content, {flag: "r+"});
} catch {
writeFileSync(file, content);
}
} else {
writeFileSync(file, content);
}
}
function parseMixedArg(arg: any) {
if (arg === "") {
return true;
} else if (typeof arg === "string") {
return arg.includes(",") ? arg.split(",") : [arg];
} else if (Array.isArray(arg)) {
return arg;
} else {
return Boolean(arg);
}
}
// handle minimist parsing issues like '-d patch'
function fixArgs(commands: Set<string>, args: any, minOpts: MinimistOpts) {
for (const key of Object.keys(minOpts.alias as object)) {
delete args[key];
}
if (commands.has(args.date)) {
args._ = [args.date, ...args._];
args.date = true;
}
if (commands.has(args.base)) {
args._ = [args.base, ...args._];
args.base = true;
}
if (commands.has(args.command)) {
args._ = [args.command, ...args._];
args.command = "";
}
if (commands.has(args.replace)) {
args._ = [args.replace, ...args._];
args.replace = "";
}
return args;
}
// join strings, ignoring falsy values and trimming the result
function joinStrings(strings: (string | undefined)[], separator: string) {
const arr = [];
for (const string of strings) {
if (!string) continue;
arr.push(string);
}
return arr.join(separator).trim();
}
function exit(err?: Error | string | void) {
if (err instanceof Error) {
console.info(String(err.stack || err.message || err).trim());
} else if (err) {
console.info(err);
}
doExit(err ? 1 : 0);
}
async function main() {
const commands = new Set(["patch", "minor", "major"]);
const args = fixArgs(commands, minimist(process.argv.slice(2), minOpts), minOpts);
let [level, ...files]: [SemverLevel, ...string[]] = args._;
files = uniq(files);
if (args.version) {
console.info(packageVersion);
exit();
}
if (!commands.has(level) || args.help) {
console.info(`usage: versions [options] patch|minor|major [files...]
Options:
-a, --all Add all changed files to the commit
-b, --base <version> Base version. Default is from latest git tag or 0.0.0
-p, --prefix Prefix version string with a "v" character. Default is none
-c, --command <cmd> Run command after files are updated but before git commit and tag
-d, --date [<date>] Replace dates in format YYYY-MM-DD with current or given date
-m, --message <str> Custom tag and commit message
-r, --replace <str> Additional replacements in the format "s#regexp#replacement#flags"
-g, --gitless Do not perform any git action like creating commit and tag
-D, --dry Do not create a tag or commit, just print what would be done
-v, --version Print the version
-h, --help Print this help
The message and replacement strings accept tokens _VER_, _MAJOR_, _MINOR_, _PATCH_.
Examples:
$ versions patch
$ versions -c 'npm run build' -m 'Release _VER_' minor file.css`);
exit();
}
let date: any = parseMixedArg(args.date);
if (date) {
if (date === true) {
date = (new Date()).toISOString().substring(0, 10);
} else if (Array.isArray(date)) {
date = date[date.length - 1];
}
if (typeof date !== "string" || !/^[0-9]{4}-[0-9]{2}-[0-9]{2}$/.test(date)) {
exit(`Invalid date argument: ${date}`);
}
}
const gitDir = find(".git", pwd);
let projectRoot = gitDir ? dirname(gitDir) : null;
if (!projectRoot) projectRoot = pwd;
// obtain old version
let baseVersion;
if (!args.base) {
if (args.gitless) return exit(new Error(`--gitless requires --base to be set`));
let stdout, exitCode;
try {
({stdout, exitCode} = await run(["git", "tag", "--list", "--sort=-creatordate"], {silent: true}));
} catch {}
if (exitCode === 0) {
for (const tag of String(stdout).split(/\r?\n/).map(v => v.trim()).filter(Boolean)) {
if (isSemver(tag)) {
baseVersion = tag.replace(/^v/, "");
break;
}
}
}
if (!baseVersion) {
baseVersion = "0.0.0";
}
} else {
baseVersion = args.base;
}
// chop off "v"
if (baseVersion.startsWith("v")) baseVersion = baseVersion.substring(1);
// validate old version
if (!isSemver(baseVersion)) {
throw new Error(`Invalid base version: ${baseVersion}`);
}
// convert paths to relative
files = await Promise.all(files.map(file => relative(pwd, file)));
// set new version
const newVersion = incrementSemver(baseVersion, level);
const replacements = [];
if (args.replace) {
args.replace = Array.isArray(args.replace) ? args.replace : [args.replace];
for (const replaceStr of args.replace) {
let [_, re, replacement, flags] = (/^s#(.+?)#(.+?)#(.*)$/.exec(replaceStr) || []);
if (!re || !replacement) {
exit(new Error(`Invalid replace string: ${replaceStr}`));
}
replacement = replaceTokens(replacement, newVersion);
replacements.push({re: new RegExp(re, flags || undefined), replacement});
}
}
if (files.length) {
// verify files exist
for (const file of files) {
const stats = statSync(file);
if (!stats.isFile() && !stats.isSymbolicLink()) {
throw new Error(`${file} is not a file`);
}
}
// update files
const todo = [];
for (const file of files) {
todo.push(getFileChanges({file, baseVersion, newVersion, replacements, date}));
}
for (const [file, newData] of todo) {
write(file, newData);
}
}
if (args.command) await run(args.command);
if (args.gitless) return; // nothing else to do
const messages: string[] = parseMixedArg(args.message) as string[];
const tagName = args["prefix"] ? `v${newVersion}` : newVersion;
const msgs: string[] = [];
if (messages) {
msgs.push(...messages.map(message => replaceTokens(message, newVersion)));
}
// check if base tag exists
let range;
try {
await run(["git", "show", tagName], {silent: true});
range = `${tagName}..HEAD`;
} catch {}
// check if we have any previous tag
if (!range) {
try {
const {stdout} = await run(["git", "describe", "--abbrev=0"], {silent: true});
range = `${stdout}..HEAD`;
} catch {}
}
// use the whole log (for cases where it's the first release)
if (!range) range = "";
let changelog: string | undefined;
try {
const args = ["git", "log"];
if (range) args.push(range);
// https://git-scm.com/docs/pretty-formats
const {stdout} = await run([...args, `--pretty=format:* %s (%aN)`], {silent: true});
if (stdout?.length) changelog = stdout;
} catch {}
if (args.dry) {
return console.info(`Would create new tag and commit: ${tagName}`);
}
// create commit
const commitMsg = joinStrings([tagName, ...msgs, changelog], "\n\n");
if (args.all) {
await run(["git", "commit", "-a", "--allow-empty", "-F", "-"], {input: commitMsg});
} else {
const filesToAdd = await removeIgnoredFiles(files);
if (filesToAdd.length) {
await run(["git", "add", ...filesToAdd]);
await run(["git", "commit", "-F", "-"], {input: commitMsg});
} else {
await run(["git", "commit", "--allow-empty", "-F", "-"], {input: commitMsg});
}
}
// create tag
const tagMsg = joinStrings([...msgs, changelog], "\n\n");
// adding explicit -a here seems to make git no longer sign the tag
await run(["git", "tag", "-f", "-F", "-", tagName], {input: tagMsg});
}
main().then(exit).catch(exit);