-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathversion.ts
117 lines (107 loc) · 3.41 KB
/
version.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
import { parseArgs } from "jsr:@std/cli/parse-args";
type VersionString = `${number}.${number}.${number}`;
type Version = "major" | "minor" | "patch" | VersionString;
const versionRegex = /\d+\.\d+\.\d+/gi;
const paths = {
README: "./README.md",
packageJson: "./app/package.json",
};
function getCurrentVersion(): VersionString {
const fileText = Deno.readTextFileSync(paths.README);
const lines = fileText.split("\n");
for (const line of lines) {
if (line.includes(`id="version"`)) {
const matches = line.match(versionRegex);
if (!matches) {
throw new Error("Could not find the existing version in the readme");
}
for (const match of matches) {
return match as VersionString;
}
}
}
throw new Error("Could not find the existing version in the readme");
}
function parseVersionString(version: VersionString): [number, number, number] {
const split = version.split(".").map(Number);
if (split.length !== 3) {
throw new Error(`Failed to parse version ${version}`);
}
return split as [number, number, number];
}
const transformers = {
[paths.README]: (version: VersionString): string => {
const fileText = Deno.readTextFileSync(paths.README);
const lines = fileText.split("\n");
for (const lineIndex in lines) {
if (lines[lineIndex].includes(`id="version"`)) {
lines[lineIndex] = lines[lineIndex].replace(versionRegex, version);
}
}
return lines.join("\n");
},
[paths.packageJson]: (version: VersionString) => {
const fileText = Deno.readTextFileSync(paths.packageJson);
const parsed = JSON.parse(fileText);
parsed.version = version;
const stringified = JSON.stringify(parsed, undefined, 2);
return stringified;
},
};
function getVersionArg(): Version {
const flags = parseArgs(Deno.args, {
string: ["version"],
});
if (!flags.version) {
throw new Error("Version was not supplied");
}
if (flags.version === "major") return "major";
if (flags.version === "minor") return "minor";
if (flags.version === "patch") return "patch";
const versionRegex = /\d+\.\d+\.\d+/gi;
if (!versionRegex.test(flags.version)) {
throw new Error("Version must be of the form #.#.#");
}
return flags.version as VersionString;
}
function getModifiedVersion(): VersionString {
const versionArg = getVersionArg();
const currentVersion = getCurrentVersion();
let modifiedVersion = currentVersion;
switch (versionArg) {
case "major": {
const parsed = parseVersionString(currentVersion);
parsed[0] += 1;
parsed[1] = 0;
parsed[2] = 0;
modifiedVersion = parsed.join(".") as VersionString;
break;
}
case "minor": {
const parsed = parseVersionString(currentVersion);
parsed[1] += 1;
parsed[2] = 0;
modifiedVersion = parsed.join(".") as VersionString;
break;
}
case "patch": {
const parsed = parseVersionString(currentVersion);
parsed[2] += 1;
modifiedVersion = parsed.join(".") as VersionString;
break;
}
default:
modifiedVersion = versionArg as VersionString;
break;
}
return modifiedVersion;
}
function main() {
const newVersion = getModifiedVersion();
for (const [path, transform] of Object.entries(transformers)) {
console.log(`Updating ${path} to version ${newVersion}`);
const transformed = transform(newVersion);
Deno.writeTextFileSync(path, transformed);
}
}
main();