Skip to content

Commit

Permalink
feat: Add version update script
Browse files Browse the repository at this point in the history
  • Loading branch information
xiangechen committed Aug 4, 2024
1 parent b1c39b2 commit b847d54
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 2 deletions.
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
{
"name": "chili3d",
"version": "0.1.0",
"version": "0.3.0-beta",
"description": "",
"author": "仙阁",
"scripts": {
"build": "npx rspack build",
"dev": "npx rspack dev",
"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 ."
"prettier": "npx prettier --write .",
"version": "node scripts/version.mjs"
},
"simple-git-hooks": {
"pre-commit": "npx lint-staged"
Expand Down
68 changes: 68 additions & 0 deletions scripts/version.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';

/**
* @typedef {{
* name: string
* version: string
* dependencies?: { [dependenciesPackageName: string]: string }
* peerDependencies?: { [peerDependenciesPackageName: string]: string }
* }} Package
*/

const __dirname = path.dirname(fileURLToPath(import.meta.url))

const packages = fs
.readdirSync(path.resolve(__dirname, '../packages'))
.filter(p => {
const pkgRoot = path.resolve(__dirname, '../packages', p)
if (fs.statSync(pkgRoot).isDirectory()) {
const pkg = JSON.parse(
fs.readFileSync(path.resolve(pkgRoot, 'package.json'), 'utf-8'),
)
return !pkg.private
}
})

function updateVersions(version) {
// 1. update root package.json
updatePackage(path.resolve(__dirname, '..'), version)
// 2. update all packages
packages.forEach(p =>
updatePackage(getPkgRoot(p), version),
)
}

/**
*
* @param {string} pkg
*/
function getPkgRoot(pkg) {
return path.resolve(__dirname, '../packages/' + pkg)
}

/**
* @param {string} pkgRoot
* @param {string} version
*/
function updatePackage(pkgRoot, version) {
const pkgPath = path.resolve(pkgRoot, 'package.json')
/** @type {Package} */
const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf-8'))
pkg.version = version
fs.writeFileSync(pkgPath, JSON.stringify(pkg, null, 2) + '\n')
}

async function main() {
const version = process.argv[2]
if (!version) {
console.error('Please provide a version, e.g. `npm run release 1.0.0`')
process.exit(1)
}

updateVersions(version)
console.log('Releasing ' + version)
}

await main()

0 comments on commit b847d54

Please sign in to comment.