minify-html/version

171 lines
4.3 KiB
Plaintext
Raw Normal View History

#!/usr/bin/env node
"use strict";
2021-08-09 06:00:19 -04:00
const { readFileSync, writeFileSync } = require("fs");
const { spawnSync } = require("child_process");
2021-08-08 04:06:17 -04:00
const RUST_MAIN_DIR = `${__dirname}/rust/main`;
2021-08-09 06:00:19 -04:00
const RUST_ONEPASS_DIR = `${__dirname}/rust/onepass`;
2021-08-08 04:06:17 -04:00
2021-08-09 06:00:19 -04:00
// Use minify-html as source of truth for current version value.
const currentVersion = /^version = "(\d+)\.(\d+)\.(\d+)"\s*$/m
.exec(readFileSync(`${RUST_MAIN_DIR}/Cargo.toml`, "utf8"))
.slice(1)
.map((n) => Number.parseInt(n, 10));
2020-01-20 08:50:06 -05:00
const assertBetween = (n, min, max) => {
if (n < min || n > max) {
2021-08-09 06:00:19 -04:00
throw new Error("Invalid argument");
2020-01-20 08:50:06 -05:00
}
return n;
};
const newVersion = currentVersion.slice();
2021-08-09 06:00:19 -04:00
let versionPart = assertBetween(
["major", "minor", "patch"].indexOf(process.argv[2].toLowerCase()),
0,
2
);
2020-05-17 18:42:23 -04:00
newVersion[versionPart++]++;
while (versionPart < 3) {
newVersion[versionPart++] = 0;
}
2020-01-20 08:50:06 -05:00
2021-08-09 06:00:19 -04:00
console.log(`${currentVersion.join(".")} => ${newVersion.join(".")}`);
2020-01-20 08:50:06 -05:00
2021-08-09 06:00:19 -04:00
const NEW_VERSION = newVersion.join(".");
2020-01-03 01:45:28 -05:00
const cmd = (...cfg) => {
const command = cfg[0];
const args = cfg.slice(1);
const {
workingDir,
throwOnBadStatus = true,
throwOnSignal = true,
captureStdio = false,
throwOnStdErr = false,
2021-08-09 06:00:19 -04:00
} = typeof args[args.length - 1] == "object" ? args.pop() : {};
2020-01-03 01:45:28 -05:00
2021-08-09 06:00:19 -04:00
const throwErr = (msg) => {
throw new Error(`${msg}\n ${command} ${args.join(" ")}`);
2020-01-03 01:45:28 -05:00
};
2021-08-09 06:00:19 -04:00
const { status, signal, error, stdout, stderr } = spawnSync(
command,
args.map(String),
{
cwd: workingDir,
stdio: [
"ignore",
captureStdio ? "pipe" : "inherit",
captureStdio || throwOnStdErr ? "pipe" : "inherit",
],
encoding: "utf8",
}
);
2020-01-01 05:01:55 -05:00
if (error) {
2020-01-03 01:45:28 -05:00
throwErr(error.message);
}
if (throwOnSignal && signal) {
throwErr(`Command exited with signal ${signal}`);
2020-01-01 05:01:55 -05:00
}
2020-01-03 01:45:28 -05:00
if (throwOnBadStatus && status !== 0) {
throwErr(`Command exited with status ${status}`);
2020-01-01 05:01:55 -05:00
}
2020-01-03 01:45:28 -05:00
if (throwOnStdErr && stderr) {
throwErr(`stderr: ${stderr}`);
}
2021-08-09 06:00:19 -04:00
return { status, signal, stdout, stderr };
2020-01-01 05:01:55 -05:00
};
2021-08-09 06:00:19 -04:00
const replaceInFile = (path, pattern, replacement) =>
writeFileSync(path, readFileSync(path, "utf8").replace(pattern, replacement));
2021-08-09 06:00:19 -04:00
if (
cmd("git", "status", "--porcelain", {
throwOnStderr: true,
captureStdio: true,
}).stdout
) {
throw new Error("Working directory not clean");
2020-01-03 01:45:28 -05:00
}
2021-08-09 06:00:19 -04:00
cmd("git", "pull");
cmd("bash", "./prebuild.sh");
2022-06-21 03:29:12 -04:00
cmd("cargo", "test", { workingDir: RUST_MAIN_DIR });
2022-06-21 11:36:27 -04:00
cmd("cargo", "test", { workingDir: RUST_ONEPASS_DIR });
2021-08-09 06:00:19 -04:00
2022-10-27 20:15:16 -04:00
replaceInFile("CHANGELOG.md", /^## Pending$/m, `## ${NEW_VERSION}`);
2021-08-09 06:00:19 -04:00
for (const f of [
`${RUST_MAIN_DIR}/Cargo.toml`,
`${RUST_ONEPASS_DIR}/Cargo.toml`,
"cli/Cargo.toml",
2022-06-21 09:03:35 -04:00
"nodejs/Cargo.toml",
2021-08-09 06:00:19 -04:00
"java/Cargo.toml",
2022-06-21 22:24:15 -04:00
"python/main/Cargo.toml",
"python/onepass/Cargo.toml",
2021-08-09 06:00:19 -04:00
"ruby/Cargo.toml",
2022-06-21 10:43:09 -04:00
"wasm/Cargo.toml",
2021-08-09 06:00:19 -04:00
]) {
replaceInFile(
f,
/^version = "\d+\.\d+\.\d+"\s*$/m,
`version = "${NEW_VERSION}"`
);
}
2022-06-21 22:06:15 -04:00
for (const f of ["README.md", "nodejs/Cargo.toml"]) {
2022-06-21 22:24:52 -04:00
replaceInFile(f, /^(minify-html = )"\d+\.\d+\.\d+"/m, `$1"${NEW_VERSION}"`);
}
2021-08-09 06:00:19 -04:00
for (const f of ["README.md"]) {
replaceInFile(
f,
2022-06-21 10:43:09 -04:00
/(wilsonl\.in\/minify-html\/(?:bin|deno)\/)\d+\.\d+\.\d+/g,
2021-08-09 06:00:19 -04:00
`$1${NEW_VERSION}`
);
}
2021-08-09 06:00:19 -04:00
for (const f of ["README.md", "bench/README.md", "rust/onepass/README.md"]) {
replaceInFile(
f,
/(wilsonl\.in\/minify-html\/bench\/)\d+\.\d+\.\d+/g,
`$1${NEW_VERSION}`
);
2020-01-19 03:19:33 -05:00
}
2021-08-09 06:00:19 -04:00
for (const f of ["java/pom.xml", "README.md"]) {
replaceInFile(
f,
/(<artifactId>minify-html<\/artifactId>\s*<version>)\d+\.\d+\.\d+(<\/version>)/,
`$1${NEW_VERSION}$2`
);
}
2022-06-21 07:52:28 -04:00
for (const f of ["nodejs/package.json"]) {
2021-08-09 06:00:19 -04:00
replaceInFile(
f,
2022-06-21 07:52:28 -04:00
/^(\s*"version": )"\d+\.\d+\.\d+",\s*$/m,
2021-08-09 06:00:19 -04:00
`$1"${NEW_VERSION}",`
);
}
2020-01-01 05:01:55 -05:00
2021-08-09 06:00:19 -04:00
for (const f of ["ruby/minify_html.gemspec"]) {
replaceInFile(
f,
/^(\s*spec\.version\s*=\s*)"\d+\.\d+\.\d+"\s*$/m,
`$1"${NEW_VERSION}"`
);
2020-01-25 22:38:14 -05:00
}
2021-08-09 06:00:19 -04:00
cmd("cargo", "generate-lockfile", { workingDir: RUST_MAIN_DIR });
cmd("cargo", "generate-lockfile", { workingDir: RUST_ONEPASS_DIR });
cmd("git", "add", "-A");
cmd("git", "commit", "-m", NEW_VERSION);
cmd("git", "tag", "-a", `v${NEW_VERSION}`, "-m", "");
2021-08-08 03:40:42 -04:00
// We have generated but ignored in `rust/common/gen`.
2021-08-09 06:00:19 -04:00
cmd("cargo", "publish", "--allow-dirty", { workingDir: RUST_MAIN_DIR });
cmd("cargo", "publish", "--allow-dirty", { workingDir: RUST_ONEPASS_DIR });
cmd("git", "push", "--follow-tags");