version.js (1440B)
1 #!/usr/bin/env node 2 3 const { dirname, join } = require('node:path'); 4 const { writeFileSync, readFileSync } = require('node:fs'); 5 6 const packageDir = join(__dirname, '..'); 7 const packageFile = join(packageDir, 'package.json'); 8 9 function syncVersion() { 10 const pkg = JSON.parse(readFileSync(packageFile, 'utf-8')); 11 const version = pkg.version; 12 for(const workspace of pkg.workspaces) { 13 const workspaceDir = join(packageDir, workspace); 14 const workspacePackageFile = join(workspaceDir, 'package.json'); 15 const workspacePackage = JSON.parse(readFileSync(workspacePackageFile, 'utf-8')); 16 workspacePackage.version = version; 17 writeFileSync(workspacePackageFile, JSON.stringify(workspacePackage, null, 2)); 18 } 19 } 20 21 function bumpVersion(oldVersion, idx) { 22 const tokens = oldVersion.split('.').map(n => parseInt(n)); 23 tokens[idx]++; 24 for(let i=idx+1; i < tokens.length; i++) tokens[i]=0; 25 return tokens.join('.'); 26 } 27 28 const types = { 29 major: 0, 30 minor: 1, 31 patch: 2, 32 }; 33 34 if (!(process.argv[2] in types)) { 35 process.stdout.write(` 36 Bump the version across all workspaces 37 38 Usage: version.js <type> 39 40 Types: 41 major 1.1.1 => 2.0.0 42 minor 1.1.1 => 1.2.0 43 patch 1.1.1 => 1.1.2 44 45 `); 46 process.exit(1); 47 } 48 49 const pkg = JSON.parse(readFileSync(packageFile, 'utf-8')); 50 pkg.version = bumpVersion(pkg.version, types[process.argv[2]]); 51 writeFileSync(__dirname+'/../package.json', JSON.stringify(pkg, null, 2)); 52 syncVersion();