From 4f3b2c3d949d2bed9668c2a9a9b640c9f4521b56 Mon Sep 17 00:00:00 2001 From: #FF0000 Date: Fri, 20 Dec 2024 21:45:05 +0800 Subject: [PATCH] chore: add update-deps (#27) --- package.json | 4 +- pnpm-lock.yaml | 3 + scripts/update-dependencies.ts | 102 +++++++++++++++++++++++++++++++++ 3 files changed, 108 insertions(+), 1 deletion(-) create mode 100644 scripts/update-dependencies.ts diff --git a/package.json b/package.json index f743de0..f449114 100644 --- a/package.json +++ b/package.json @@ -13,12 +13,14 @@ "build:node-demo": "pnpm --filter examples-node build:demo", "lint": "eslint .", "lint:fix": "eslint . --fix", - "release": "release-it" + "release": "release-it", + "update-deps": "tsx scripts/update-dependencies.ts" }, "devDependencies": { "@antfu/eslint-config": "^3.11.2", "@release-it-plugins/workspaces": "^4.2.0", "@release-it/conventional-changelog": "^9.0.3", + "@types/fs-extra": "^11.0.4", "@univerjs/core": "0.5.1", "eslint": "^9.16.0", "eslint-plugin-format": "^0.1.3", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index f958253..522d02a 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -17,6 +17,9 @@ importers: '@release-it/conventional-changelog': specifier: ^9.0.3 version: 9.0.3(conventional-commits-filter@5.0.0)(conventional-commits-parser@6.0.0)(release-it@17.10.0(typescript@5.7.2)) + '@types/fs-extra': + specifier: ^11.0.4 + version: 11.0.4 '@univerjs/core': specifier: 0.5.1 version: 0.5.1(@grpc/grpc-js@1.9.14)(react@18.3.1)(rxjs@7.8.1) diff --git a/scripts/update-dependencies.ts b/scripts/update-dependencies.ts new file mode 100644 index 0000000..c8b0c65 --- /dev/null +++ b/scripts/update-dependencies.ts @@ -0,0 +1,102 @@ +/** + * Dependencies Version Update Script + * + * Purpose: Automatically updates the version of all @univerjs and @univerjs-pro + * related dependencies across all packages. + * + * Usage: + * 1. Basic usage (will use latest version from @univerjs/core latest tag): + * pnpm tsx scripts/update-dependencies.ts + * + * 2. Specify release channel: + * RELEASE_CHANNEL=beta pnpm tsx scripts/update-dependencies.ts + * + * 3. Specify exact version: + * NEW_VERSION=1.0.0 pnpm tsx scripts/update-dependencies.ts + * + * Environment Variables: + * - RELEASE_CHANNEL: Release channel to use, defaults to 'latest'. + * Can be 'beta', 'alpha', etc. + * - NEW_VERSION: Target version to update to. If not specified, + * will fetch latest version from specified RELEASE_CHANNEL + * + * Notes: + * - Excluded packages: ${EXCLUDED_PACKAGES} + * - Excluded versions: ${EXCLUDED_VERSIONS} + */ + +import { execSync } from 'node:child_process'; +import path from 'node:path'; +import process from 'node:process'; +import fs from 'fs-extra'; + +const RELEASE_CHANNEL = process.env.RELEASE_CHANNEL || 'latest'; +const NEW_VERSION = process.env.NEW_VERSION || getLatestTagVersion('@univerjs/core', RELEASE_CHANNEL); + +if (!NEW_VERSION) { + console.error('Failed to get version'); + process.exit(1); +} + +const PACKAGES_DIR = path.resolve(__dirname, '../packages'); + +const EXCLUDED_PACKAGES = ['@univerjs/protocol']; +const EXCLUDED_VERSIONS = ['workspace:*']; + +function getLatestTagVersion(packageName: string, tag: string = 'latest') { + try { + const version = execSync(`npm view ${packageName}@${tag} version`, { encoding: 'utf-8' }).trim(); + return version; + } + catch (error) { + console.error(`Failed to get version for ${packageName}@${tag}`); + throw error; + } +} + +function updateDependencies() { + const packages = fs.readdirSync(PACKAGES_DIR).filter(file => + fs.statSync(path.join(PACKAGES_DIR, file)).isDirectory(), + ); + + packages.forEach((packageDir) => { + const packageJsonPath = path.join(PACKAGES_DIR, packageDir, 'package.json'); + + if (!fs.existsSync(packageJsonPath)) { + return; + } + + const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8')); + let hasUpdates = false; + + const dependencyTypes = ['dependencies', 'devDependencies', 'peerDependencies']; + + dependencyTypes.forEach((depType) => { + if (!packageJson[depType]) + return; + + Object.entries(packageJson[depType] as [string, string]).forEach(([pkg, version]) => { + if ( + (pkg.startsWith('@univerjs/') || pkg.startsWith('@univerjs-pro/')) + && !EXCLUDED_PACKAGES.includes(pkg) + && !EXCLUDED_VERSIONS.includes(version) + ) { + packageJson[depType][pkg] = NEW_VERSION; + hasUpdates = true; + console.log(`Updated ${pkg} to ${NEW_VERSION} in ${packageDir}`); + } + }); + }); + + if (hasUpdates) { + fs.writeJSONSync(packageJsonPath, packageJson, { spaces: 4, EOL: '\n' }); + } + }); + + console.log('Running pnpm install to update lock file...'); + execSync('pnpm install', { stdio: 'inherit' }); + + console.log('Dependencies update completed!'); +} + +updateDependencies();