Files
Genarrative/apps/ai-game-creator-shell/scripts/bump-version.mjs
suzmii 2043f8c966
Project CI / Repository checks (pull_request) Successful in 2m54s
Project CI / Frontend tests (pull_request) Successful in 3m14s
Project CI / Backend tests (pull_request) Successful in 6m0s
Project CI / Native shell tests (pull_request) Successful in 20m42s
修复 bump-version 脚本导入排序以通过 lint:eslint
- bump-version.mjs 按 simple-import-sort 调整 node:child_process / node:path / node:url 导入顺序
2026-09-07 14:59:30 +08:00

122 lines
3.5 KiB
JavaScript

import { spawnSync } from 'node:child_process';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
const appRoot = fileURLToPath(new URL('..', import.meta.url));
const repoRoot = path.resolve(appRoot, '../..');
const {
bumpVersion,
readLocalVersion,
writeVersionFiles,
} = await import('./build-release.mjs');
const versionFiles = [
'apps/ai-game-creator-shell/package.json',
'package-lock.json',
'apps/ai-game-creator-shell/src-tauri/tauri.conf.json',
'apps/ai-game-creator-shell/src-tauri/Cargo.toml',
'apps/ai-game-creator-shell/src-tauri/Cargo.lock',
];
function parseArgs(argv) {
const args = argv.slice(2);
const explicitIndex = args.indexOf('--version');
const explicit = explicitIndex >= 0 ? args[explicitIndex + 1] : null;
const target =
explicit ??
args.find((arg) => arg === 'patch' || arg === 'minor' || arg === 'major') ??
'patch';
return { target, commit: args.includes('--commit') };
}
function runGit(args) {
const result = spawnSync('git', args, { cwd: repoRoot, stdio: 'inherit' });
if (result.error) throw result.error;
return result.status ?? 1;
}
function hasUncommittedVersionChanges() {
return (
spawnSync('git', ['diff', '--quiet', '--', ...versionFiles], {
cwd: repoRoot,
}).status === 1
);
}
function otherStagedFiles() {
const result = spawnSync(
'git',
['diff', '--cached', '--name-only'],
{ cwd: repoRoot, encoding: 'utf8' },
);
if (result.status !== 0) {
throw new Error('无法读取已暂存文件列表');
}
const versionSet = new Set(versionFiles);
return result.stdout
.split('\n')
.map((file) => file.trim())
.filter(Boolean)
.filter((file) => !versionSet.has(file));
}
const { target, commit } = parseArgs(process.argv);
// 提交前先确认没有其他已暂存改动,避免把无关改动一并提交;若存在则在改动任何文件前中止。
if (commit) {
const others = otherStagedFiles();
if (others.length > 0) {
console.error(
`[bump-version] 检测到其他已暂存改动,为避免误提交,请先单独处理后再运行 --commit:\n ${others.join('\n ')}`,
);
console.error(
'提示:用 git restore --staged <file> 取消暂存,或先提交这些改动。',
);
process.exit(1);
}
}
const current = readLocalVersion();
// 若版本文件已带着一次未提交的提升(例如先默认跑过一次),再 --commit 时不再重复递增,直接提交现有改动。
const next =
commit && hasUncommittedVersionChanges()
? current
: bumpVersion(current, target);
writeVersionFiles(next);
if (!commit) {
console.log(
`[bump-version] 版本 ${current} -> ${next}(已写入版本文件,未创建提交;如需提交加 --commit)`,
);
process.exit(0);
}
const addStatus = runGit(['add', ...versionFiles]);
if (addStatus !== 0) process.exit(addStatus);
const hasDiff =
spawnSync('git', ['diff', '--cached', '--quiet'], {
cwd: repoRoot,
}).status === 1;
if (!hasDiff) {
console.log(`[bump-version] 版本未变化(已是 ${current}),未创建提交`);
process.exit(0);
}
const commitStatus = runGit([
'commit',
'-m',
`提升 AGC 版本至 ${next}`,
'-m',
`- AGC 客户端版本提升至 ${next}`,
'-m',
'- 同步更新 package.json、根 package-lock.json、tauri.conf.json、Cargo.toml、Cargo.lock 中的 AGC 包条目',
]);
if (commitStatus !== 0) process.exit(commitStatus);
console.log(`[bump-version] 已提交版本 ${next}(本地提交,未推送)`);