#!/usr/bin/env node
/**
* scripts/bump-version.mjs — bump เวอร์ชันแอปให้ตรงกันสองที่
*
* node scripts/bump-version.mjs # bump เป็นวันนี้ +1 (เช่น 2026.11.23.2)
* node scripts/bump-version.mjs --set 2026.12.01.1
* node scripts/bump-version.mjs --check # ตรวจว่า sw.js VERSION === (exit 1 ถ้าไม่ตรง)
*
* สองที่ที่ต้องตรงกัน: `VERSION` ใน sw.js (ชื่อ cache) และ `` ใน index.html
* bump เฉพาะเมื่อแก้ logic/PRECACHE ของ sw.js — แก้เนื้อหา (HTML/CSS/JS/JSON) ใช้ network-first ไม่ต้อง bump
*/
import { readFile, writeFile } from "node:fs/promises";
import path from "node:path";
import { fileURLToPath } from "node:url";
const ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "..");
const SW_FILE = path.join(ROOT, "sw.js");
const HTML_FILE = path.join(ROOT, "index.html");
const SW_RE = /const VERSION = "([^"]+)";/;
const META_RE = //;
const SEMVER_RE = /^\d{4}\.\d{2}\.\d{2}\.\d+$/;
function today() {
const now = new Date();
const pad = (n) => String(n).padStart(2, "0");
return `${now.getFullYear()}.${pad(now.getMonth() + 1)}.${pad(now.getDate())}`;
}
function nextVersion(current) {
const base = today();
const match = String(current || "").match(/^(\d{4}\.\d{2}\.\d{2})\.(\d+)$/);
if (match && match[1] === base) return `${base}.${Number(match[2]) + 1}`;
return `${base}.1`;
}
async function readPair() {
const [sw, html] = await Promise.all([readFile(SW_FILE, "utf8"), readFile(HTML_FILE, "utf8")]);
const swMatch = sw.match(SW_RE);
const metaMatch = html.match(META_RE);
return {
sw,
html,
swVersion: swMatch ? swMatch[1] : null,
metaVersion: metaMatch ? metaMatch[1] : null,
};
}
async function check() {
const { swVersion, metaVersion } = await readPair();
if (!swVersion) {
console.error("[version] หา `const VERSION = \"…\"` ใน sw.js ไม่เจอ");
process.exit(1);
}
if (!metaVersion) {
console.error('[version] หา ใน index.html ไม่เจอ');
process.exit(1);
}
if (swVersion !== metaVersion) {
console.error(`[version] ไม่ตรงกัน: sw.js = ${swVersion} · index.html = ${metaVersion}`);
console.error("[version] แก้ด้วย `node scripts/bump-version.mjs` หรือ `--set <เวอร์ชัน>`");
process.exit(1);
}
console.log(`[version] ตรงกันที่ ${swVersion}`);
}
async function setVersion(target) {
if (!SEMVER_RE.test(target)) {
console.error(`[version] รูปแบบไม่ถูกต้อง: ${target} (ต้องเป็น YYYY.MM.DD.N)`);
process.exit(1);
}
const { sw, html } = await readPair();
const nextSw = sw.replace(SW_RE, `const VERSION = "${target}";`);
const nextHtml = html.replace(META_RE, ``);
if (nextSw === sw && nextHtml === html) {
console.log(`[version] เป็น ${target} อยู่แล้ว ไม่ต้องแก้`);
return;
}
await Promise.all([
writeFile(SW_FILE, nextSw, "utf8"),
writeFile(HTML_FILE, nextHtml, "utf8"),
]);
console.log(`[version] ตั้งเป็น ${target} แล้ว (sw.js + index.html)`);
}
const args = process.argv.slice(2);
if (args.includes("--check")) {
await check();
} else if (args.includes("--set")) {
const value = args[args.indexOf("--set") + 1];
if (!value) {
console.error("[version] ต้องระบุเวอร์ชันหลัง --set");
process.exit(1);
}
await setVersion(value);
} else {
const { swVersion } = await readPair();
await setVersion(nextVersion(swVersion));
}