import fs from "node:fs"; import path from "node:path"; const root = process.cwd(); const pkgPath = path.join(root, "package.json"); const lockPath = path.join(root, "package-lock.json"); const target = String(process.argv[2] || "patch").toLowerCase(); if (!["patch", "minor", "major"].includes(target)) { console.error(`[version] unsupported target: ${target}`); process.exit(1); } function bump(version, mode) { const parts = String(version || "0.0.0").split(".").map((v) => Number(v) || 0); const [major, minor, patch] = [parts[0] ?? 0, parts[1] ?? 0, parts[2] ?? 0]; if (mode === "major") return `${major + 1}.0.0`; if (mode === "minor") return `${major}.${minor + 1}.0`; return `${major}.${minor}.${patch + 1}`; } function readJson(filePath) { return JSON.parse(fs.readFileSync(filePath, "utf8")); } function writeJson(filePath, obj) { fs.writeFileSync(filePath, `${JSON.stringify(obj, null, 2)}\n`, "utf8"); } const pkg = readJson(pkgPath); const current = String(pkg.version || "0.0.0"); const next = bump(current, target); pkg.version = next; writeJson(pkgPath, pkg); if (fs.existsSync(lockPath)) { const lock = readJson(lockPath); lock.version = next; if (lock.packages && lock.packages[""]) { lock.packages[""].version = next; } writeJson(lockPath, lock); } console.log(`[version] ${current} -> ${next} (${target})`);