diff --git a/package.json b/package.json index 2be0a157..81f3d9c3 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "chili3d", - "version": "0.1.0", + "version": "0.3.0-beta", "description": "", "author": "ไป™้˜", "scripts": { @@ -8,7 +8,8 @@ "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" diff --git a/scripts/version.mjs b/scripts/version.mjs new file mode 100644 index 00000000..5a748c55 --- /dev/null +++ b/scripts/version.mjs @@ -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()