版本 )(\d+\.\d+\.\d+)( ·)/, }, { file: 'website/download.html', label: '(版本清单的兜底值)', pattern: /()/, }, ]; const ALL_TARGETS = [...PRODUCT_TARGETS, ...PLUGIN_TARGETS, ...SITE_TARGETS]; const PRODUCT_RE = /^\d+\.\d+\.\d+$/; const PLUGIN_RE = /^\d+\.\d+\.\d+\.\d+$/; /** 输出用的名字:同一个文件里有多个落点时带上括号里的说明,否则三行看起来一模一样 */ function name(target) { return target.label ? `${target.file} ${target.label}` : target.file; } function readTarget(target) { const source = fs.readFileSync(path.join(root, target.file), 'utf8'); const flags = target.pattern.flags.includes('g') ? target.pattern.flags : `${target.pattern.flags}g`; const hits = [...source.matchAll(new RegExp(target.pattern.source, flags))]; return { source, hits, global: new RegExp(target.pattern.source, flags) }; } /** 取出某个文件当前声明的版本号;没匹配到或匹配到多处都返回 null。 */ function readVersion(target) { const { hits } = readTarget(target); return hits.length === 1 ? hits[0][2] : null; } /** 改写一处。缺失或重复命中都算失败 —— 静默漏改比直接报错危险得多。 */ function writeVersion(target, next, { dry }) { const { source, hits, global } = readTarget(target); if (hits.length !== 1) { throw new Error( `${target.file} 里匹配到 ${hits.length} 处版本号(应为 1 处)。` + ' 模板被改过?请同步 scripts/version.mjs 里的 pattern 表。', ); } if (!dry) { const replaced = source.replace(global, (_m, prefix, _old, suffix) => `${prefix}${next}${suffix}`); fs.writeFileSync(path.join(root, target.file), replaced, 'utf8'); } return `${name(target)} ${hits[0][2]} → ${next}`; } function bumpProduct(current, level) { const [major, minor, patch] = current.split('.').map(Number); if (level === 'major') return `${major + 1}.0.0`; if (level === 'minor') return `${major}.${minor + 1}.0`; if (level === 'patch') return `${major}.${minor}.${patch + 1}`; throw new Error(`未知的级别 ${level}(可选:major / minor / patch,或用 --set x.y.z)`); } /** 检查模式:逐处列出,并给出不一致清单。 */ function check(product) { console.log(`产品版本(单一来源:根 package.json):${product}\n`); const width = Math.max(...ALL_TARGETS.map((target) => target.file.length)); const problems = []; for (const target of ALL_TARGETS) { const version = readVersion(target); if (!version) { console.log(`?? ${name(target).padEnd(width)} (未匹配到版本号)`); problems.push(`${target.file}:匹配不到版本号`); continue; } // 插件版本是四段,只校验前三段;第 4 段允许不为 0(插件专属补丁号) const expected = PLUGIN_RE.test(version) ? `${product}.0` : product; const ok = PLUGIN_RE.test(version) ? version.split('.').slice(0, 3).join('.') === product : version === expected; console.log(`${ok ? ' ' : '!!'} ${name(target).padEnd(width)} ${version}`); if (!ok) problems.push(`${target.file}:${version} ≠ ${expected}`); } if (problems.length) { console.error('\n版本号不一致:'); for (const problem of problems) console.error(` - ${problem}`); console.error('\n对齐方式:pnpm version:check 之后用 pnpm version:bump patch 统一改写。'); process.exit(1); } console.log('\n全部一致 ✅(插件第 4 段是插件专属补丁号,允许不为 0)'); } function apply(next, { dry, product }) { const pluginNext = `${next}.0`; const lines = []; for (const target of PRODUCT_TARGETS) lines.push(writeVersion(target, next, { dry })); for (const target of PLUGIN_TARGETS) lines.push(writeVersion(target, pluginNext, { dry })); for (const target of SITE_TARGETS) lines.push(writeVersion(target, next, { dry })); console.log( `${dry ? '[dry] 将改写' : '已改写'} ${product} → ${next}(插件 ${product}.0 → ${pluginNext}):\n`, ); for (const line of lines) console.log(` ${line}`); console.log('\n接着按 docs/dev/release.md 走发布流程;GitHub Release 的 tag(v' + next + ')也要对齐。'); } function main() { const args = process.argv.slice(2); const dry = args.includes('--dry'); const bumpIndex = args.indexOf('--bump'); const setIndex = args.indexOf('--set'); const product = readVersion(PRODUCT_TARGETS[0]); // 根 package.json = 单一来源 if (!product || !PRODUCT_RE.test(product)) { throw new Error('读不到根 package.json 的版本号(或它不是 x.y.z 形式)'); } let next = null; if (bumpIndex >= 0) { const level = args[bumpIndex + 1]; if (!level) throw new Error('--bump 后面要跟 patch / minor / major'); next = bumpProduct(product, level); } else if (setIndex >= 0) { const value = args[setIndex + 1]; if (!value) throw new Error('--set 后面要跟 x.y.z'); if (!PRODUCT_RE.test(value)) throw new Error(`--set 的取值 ${value} 不是 x.y.z 形式`); next = value; } if (!next) check(product); else apply(next, { dry, product }); } try { main(); } catch (error) { console.error(`[version] ${error.message}`); process.exit(1); }