From 8a67b96fdb6ef40dec1ce47193217458de2490c1 Mon Sep 17 00:00:00 2001 From: Simon <10131203+gaomeng1900@users.noreply.github.com> Date: Mon, 1 Dec 2025 20:52:26 +0800 Subject: [PATCH] chore: add version sync script --- package.json | 1 + scripts/sync-version.js | 76 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+) create mode 100644 scripts/sync-version.js diff --git a/package.json b/package.json index 8125504..801f419 100644 --- a/package.json +++ b/package.json @@ -23,6 +23,7 @@ "start": "npm run dev --workspace=@page-agent/website", "build": "npm run build --workspaces --if-present", "build:lib": "npm run build --workspace=page-agent", + "version": "node scripts/sync-version.js", "lint": "eslint .", "prepare": "husky" }, diff --git a/scripts/sync-version.js b/scripts/sync-version.js new file mode 100644 index 0000000..c9a7fd1 --- /dev/null +++ b/scripts/sync-version.js @@ -0,0 +1,76 @@ +#!/usr/bin/env node +/** + * Sync version from root package.json to all packages + * + * Usage: + * node scripts/sync-version.js # Sync current version from root + * node scripts/sync-version.js 0.1.0 # Set and sync new version + */ +import chalk from 'chalk' +import { existsSync, readFileSync, readdirSync, writeFileSync } from 'fs' +import { dirname, join } from 'path' +import { fileURLToPath } from 'url' + +const __dirname = dirname(fileURLToPath(import.meta.url)) +const rootDir = join(__dirname, '..') + +// Parse arguments +const versionArg = process.argv[2] + +// Read root package.json +const rootPkgPath = join(rootDir, 'package.json') +const rootPkg = JSON.parse(readFileSync(rootPkgPath, 'utf-8')) +const newVersion = versionArg || rootPkg.version + +console.log(chalk.cyan.bold('\nšŸ“¦ Syncing version\n')) + +// Update root package.json if new version specified +if (versionArg) { + rootPkg.version = newVersion + writeFileSync(rootPkgPath, JSON.stringify(rootPkg, null, '\t') + '\n') + console.log(chalk.green('āœ“') + ` ${chalk.bold('root')} → ${chalk.yellow(newVersion)}`) +} else { + console.log(chalk.dim(' root:') + ` ${chalk.yellow(newVersion)} ${chalk.dim('(source)')}`) +} + +// Sync to all packages +const packagesDir = join(rootDir, 'packages') +const packages = readdirSync(packagesDir, { withFileTypes: true }) + .filter((d) => d.isDirectory()) + .map((d) => d.name) + +let hasChanges = !!versionArg + +for (const pkg of packages) { + const pkgPath = join(packagesDir, pkg, 'package.json') + if (!existsSync(pkgPath)) continue + + const pkgJson = JSON.parse(readFileSync(pkgPath, 'utf-8')) + const oldVersion = pkgJson.version + + if (oldVersion === newVersion) { + console.log(chalk.dim(` ${pkgJson.name}: ${newVersion} (unchanged)`)) + continue + } + + pkgJson.version = newVersion + writeFileSync(pkgPath, JSON.stringify(pkgJson, null, '\t') + '\n') + console.log( + chalk.green('āœ“') + + ` ${chalk.bold(pkgJson.name)}: ${chalk.dim(oldVersion)} → ${chalk.yellow(newVersion)}` + ) + hasChanges = true +} + +console.log(chalk.green.bold(`\nāœ“ Version synced: ${newVersion}\n`)) + +// Show git commands hint +if (hasChanges) { + const tagName = `v${newVersion}` + console.log(chalk.cyan.bold('šŸ“‹ Next steps:\n')) + console.log( + chalk.blueBright(`git add . && git commit -m "chore(version): bump version to ${newVersion}"`) + ) + console.log(chalk.blueBright(`git tag -a ${tagName} -m "${tagName}"`)) + console.log(chalk.blueBright(`git push && git push origin ${tagName}\n`)) +}