88 lines
3.6 KiB
JavaScript
88 lines
3.6 KiB
JavaScript
#!/usr/bin/env node
|
|
/* scripts/bump-version.mjs — bump "build version" ให้ตรงกันทุกที่ในครั้งเดียว
|
|
*
|
|
* node scripts/bump-version.mjs 2026.11.16.2 # ตั้งเวอร์ชันใหม่
|
|
* node scripts/bump-version.mjs --check # ตรวจว่าทุกที่ตรงกัน
|
|
*
|
|
* ที่ที่ต้องตรงกัน (ถ้าไม่ตรง จะเกิดอาการ "แก้แล้วแต่เว็บยังโชว์ของเก่า"):
|
|
* 1. sw.js const VERSION = "…" (ชื่อ cache)
|
|
* 2. app/version.js window.FARM_VERSION = "…" (ค่าที่หน้าเว็บฝังไว้)
|
|
* 3. app/version.json { version, builtAt } (ตัวที่ client ใช้เทียบ)
|
|
* 4. index.html ?v=… ของ app/*.js และ app.css
|
|
* 5. package.json version
|
|
*/
|
|
|
|
import { readFileSync, writeFileSync } from "node:fs";
|
|
|
|
const CARRIERS = {
|
|
sw: "sw.js",
|
|
versionJs: "app/version.js",
|
|
versionJson: "app/version.json",
|
|
shell: "index.html",
|
|
packageJson: "package.json"
|
|
};
|
|
|
|
function read(path) {
|
|
return readFileSync(path, "utf8");
|
|
}
|
|
|
|
function write(path, text) {
|
|
writeFileSync(path, text, "utf8");
|
|
}
|
|
|
|
function currentVersions() {
|
|
const sw = /const VERSION = "([^"]+)"/.exec(read(CARRIERS.sw))?.[1];
|
|
const versionJs = /window\.FARM_VERSION = "([^"]+)"/.exec(read(CARRIERS.versionJs))?.[1];
|
|
const versionJson = JSON.parse(read(CARRIERS.versionJson)).version;
|
|
const shellMatches = [...read(CARRIERS.shell).matchAll(/\?v=([0-9][^"&]*)/g)].map((match) => match[1]);
|
|
const packageJson = JSON.parse(read(CARRIERS.packageJson)).version;
|
|
return { sw, versionJs, versionJson, shell: shellMatches, packageJson };
|
|
}
|
|
|
|
function check() {
|
|
const versions = currentVersions();
|
|
const shellSet = new Set(versions.shell);
|
|
const all = [versions.sw, versions.versionJs, versions.versionJson, versions.packageJson, ...shellSet];
|
|
const unique = new Set(all);
|
|
const ok = unique.size === 1 && shellSet.size <= 1;
|
|
|
|
console.log("sw.js :", versions.sw);
|
|
console.log("app/version.js :", versions.versionJs);
|
|
console.log("app/version.json :", versions.versionJson);
|
|
console.log("index.html (?v=) :", [...shellSet].join(", ") || "(ไม่มี)");
|
|
console.log("package.json :", versions.packageJson);
|
|
console.log(ok ? "OK — ตรงกันทุกที่" : "FAIL — มีเวอร์ชันไม่ตรงกัน");
|
|
return ok ? 0 : 1;
|
|
}
|
|
|
|
function bump(next) {
|
|
if (!/^\d{4}\.\d{2}\.\d{2}\.\d+$/.test(next)) {
|
|
console.error("รูปแบบเวอร์ชันต้องเป็น YYYY.MM.DD.N เช่น 2026.11.16.2");
|
|
return 1;
|
|
}
|
|
|
|
write(CARRIERS.sw, read(CARRIERS.sw).replace(/const VERSION = "[^"]+"/, `const VERSION = "${next}"`));
|
|
write(CARRIERS.versionJs,
|
|
read(CARRIERS.versionJs).replace(/window\.FARM_VERSION = "[^"]+"/, `window.FARM_VERSION = "${next}"`));
|
|
|
|
const json = JSON.parse(read(CARRIERS.versionJson));
|
|
json.version = next;
|
|
json.builtAt = new Date().toISOString();
|
|
write(CARRIERS.versionJson, JSON.stringify(json, null, 2) + "\n");
|
|
|
|
write(CARRIERS.shell, read(CARRIERS.shell).replace(/([?&]v=)[0-9][^"&]*/g, `$1${next}`));
|
|
|
|
const pkg = JSON.parse(read(CARRIERS.packageJson));
|
|
pkg.version = next;
|
|
write(CARRIERS.packageJson, JSON.stringify(pkg, null, 2) + "\n");
|
|
|
|
console.log("bump →", next);
|
|
return check();
|
|
}
|
|
|
|
const args = process.argv.slice(2);
|
|
if (args[0] === "--check" || args.length === 0) {
|
|
process.exit(check());
|
|
}
|
|
process.exit(bump(args[0]));
|