From 1551a160b1ddc1e5c7303ec858e97759a1d40989 Mon Sep 17 00:00:00 2001 From: xiange Date: Mon, 5 Aug 2024 22:40:32 +0800 Subject: [PATCH] feat: Add release script --- package.json | 2 +- scripts/{version.mjs => release.mjs} | 45 ++++++++++++++++++++++++++-- 2 files changed, 44 insertions(+), 3 deletions(-) rename scripts/{version.mjs => release.mjs} (61%) diff --git a/package.json b/package.json index 81f3d9c3..7aacd1c1 100644 --- a/package.json +++ b/package.json @@ -9,7 +9,7 @@ "test": "node --experimental-vm-modules node_modules/jest/bin/jest.js", "testc": "node --experimental-vm-modules node_modules/jest/bin/jest.js --coverage", "prettier": "npx prettier --write .", - "version": "node scripts/version.mjs" + "release": "node scripts/release.mjs" }, "simple-git-hooks": { "pre-commit": "npx lint-staged" diff --git a/scripts/version.mjs b/scripts/release.mjs similarity index 61% rename from scripts/version.mjs rename to scripts/release.mjs index 5a748c55..d0b30b7b 100644 --- a/scripts/version.mjs +++ b/scripts/release.mjs @@ -1,6 +1,7 @@ import fs from 'fs'; import path from 'path'; import { fileURLToPath } from 'url'; +import { exec } from 'child_process'; /** * @typedef {{ @@ -32,6 +33,7 @@ function updateVersions(version) { packages.forEach(p => updatePackage(getPkgRoot(p), version), ) + console.log(`Updated all packages to version ${version}`); } /** @@ -54,6 +56,35 @@ function updatePackage(pkgRoot, version) { fs.writeFileSync(pkgPath, JSON.stringify(pkg, null, 2) + '\n') } +/** + * + * @param {string} cmd + */ +async function run(cmd) { + console.log(`> ${cmd}`) + return new Promise((resolve, reject) => { + exec(cmd, (err, stdout) => { + if (err) { + reject(err) + process.exit(1) + } + resolve(stdout) + }) + }) +} + +/** + * + * @param {string} version + */ +async function tag(version) { + await run(`git add -A`) + await run(`git commit -m '🐎 ci: release ${version}'`) + await run(`git tag ${version}`) + await run(`git push origin refs/tags/${version}`) + await run(`git push`) +} + async function main() { const version = process.argv[2] if (!version) { @@ -61,8 +92,18 @@ async function main() { process.exit(1) } - updateVersions(version) - console.log('Releasing ' + version) + console.log(`Releasing ${version}. Confirm?`) + + process.stdin.on('data', async (data) => { + if (data.toString().trim() === 'y') { + updateVersions(version); + await tag(version); + console.log('Released ' + version) + } else { + console.log('Aborting...') + } + process.exit(1) + }) } await main()