-
Notifications
You must be signed in to change notification settings - Fork 42
/
version
executable file
·178 lines (156 loc) · 4.42 KB
/
version
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
#!/usr/bin/env node
"use strict";
const { readFileSync, writeFileSync } = require("fs");
const { spawnSync } = require("child_process");
// Use minify-html as source of truth for current version value.
const currentVersion = /^version = "(\d+)\.(\d+)\.(\d+)"\s*$/m
.exec(readFileSync(`${__dirname}/minify-html/Cargo.toml`, "utf8"))
.slice(1)
.map((n) => Number.parseInt(n, 10));
const assertBetween = (n, min, max) => {
if (n < min || n > max) {
throw new Error("Invalid argument");
}
return n;
};
const newVersion = currentVersion.slice();
let versionPart = assertBetween(
["major", "minor", "patch"].indexOf(process.argv[2].toLowerCase()),
0,
2
);
newVersion[versionPart++]++;
while (versionPart < 3) {
newVersion[versionPart++] = 0;
}
console.log(`${currentVersion.join(".")} => ${newVersion.join(".")}`);
const NEW_VERSION = newVersion.join(".");
const cmd = (...cfg) => {
const command = cfg[0];
const args = cfg.slice(1);
const {
workingDir,
throwOnBadStatus = true,
throwOnSignal = true,
captureStdio = false,
throwOnStdErr = false,
} = typeof args[args.length - 1] == "object" ? args.pop() : {};
const throwErr = (msg) => {
throw new Error(`${msg}\n ${command} ${args.join(" ")}`);
};
const { status, signal, error, stdout, stderr } = spawnSync(
command,
args.map(String),
{
cwd: workingDir,
stdio: [
"ignore",
captureStdio ? "pipe" : "inherit",
captureStdio || throwOnStdErr ? "pipe" : "inherit",
],
encoding: "utf8",
}
);
if (error) {
throwErr(error.message);
}
if (throwOnSignal && signal) {
throwErr(`Command exited with signal ${signal}`);
}
if (throwOnBadStatus && status !== 0) {
throwErr(`Command exited with status ${status}`);
}
if (throwOnStdErr && stderr) {
throwErr(`stderr: ${stderr}`);
}
return { status, signal, stdout, stderr };
};
const replaceInFile = (path, pattern, replacement) =>
writeFileSync(
path,
readFileSync(`${__dirname}/${path}`, "utf8").replace(pattern, replacement)
);
if (
cmd("git", "status", "--porcelain", {
throwOnStderr: true,
captureStdio: true,
}).stdout
) {
throw new Error("Working directory not clean");
}
cmd("git", "pull");
cmd("cargo", "test");
replaceInFile("CHANGELOG.md", /^## Pending$/m, `## ${NEW_VERSION}`);
for (const f of [
"minhtml/Cargo.toml",
"minify-html-java/Cargo.toml",
"minify-html-nodejs/Cargo.toml",
"minify-html-onepass-python/Cargo.toml",
"minify-html-onepass/Cargo.toml",
"minify-html-python/Cargo.toml",
"minify-html-ruby/ext/minify_html/Cargo.toml",
"minify-html-wasm/Cargo.toml",
"minify-html/Cargo.toml",
]) {
replaceInFile(
f,
/^version = "\d+\.\d+\.\d+"\s*$/m,
`version = "${NEW_VERSION}"`
);
}
for (const f of ["minhtml/Cargo.toml"]) {
replaceInFile(
f,
/^minify-html = \{ version = "\d+\.\d+\.\d+"/m,
`minify-html = { version = "${NEW_VERSION}"`
);
}
for (const f of ["README.md", "minify-html-ruby/ext/minify_html/Cargo.toml"]) {
replaceInFile(f, /^(minify-html = )"\d+\.\d+\.\d+"/m, `$1"${NEW_VERSION}"`);
}
for (const f of ["README.md"]) {
replaceInFile(
f,
/(wilsonl\.in\/minify-html\/(?:bin|deno)\/)\d+\.\d+\.\d+/g,
`$1${NEW_VERSION}`
);
}
for (const f of [
"README.md",
"bench/README.md",
"minify-html-onepass/README.md",
]) {
replaceInFile(
f,
/(wilsonl\.in\/minify-html\/bench\/)\d+\.\d+\.\d+/g,
`$1${NEW_VERSION}`
);
}
for (const f of ["minify-html-java/pom.xml", "README.md"]) {
replaceInFile(
f,
/(<artifactId>minify-html<\/artifactId>\s*<version>)\d+\.\d+\.\d+(<\/version>)/,
`$1${NEW_VERSION}$2`
);
}
for (const f of ["minify-html-nodejs/package.json"]) {
replaceInFile(
f,
/(\s*"(?:version|@minify-html\/node-.*?)": )"\d+\.\d+\.\d+"(,?\s*)$/gm,
`$1"${NEW_VERSION}"$2`
);
}
for (const f of ["minify-html-ruby/minify_html.gemspec"]) {
replaceInFile(
f,
/^(\s*spec\.version\s*=\s*)"\d+\.\d+\.\d+"\s*$/m,
`$1"${NEW_VERSION}"`
);
}
cmd("git", "add", "-A");
cmd("git", "commit", "-m", NEW_VERSION);
cmd("git", "tag", "-a", `v${NEW_VERSION}`, "-m", "");
cmd("cargo", "publish", { workingDir: `${__dirname}/minify-html` });
cmd("cargo", "publish", { workingDir: `${__dirname}/minify-html-onepass` });
cmd("cargo", "publish", { workingDir: `${__dirname}/minhtml` }); // This must come after the above as it depends on the just-published version of those.
cmd("git", "push", "--follow-tags");