|
| 1 | +/** |
| 2 | + * @fileoverview Script to bump versions of packages in the monorepo. |
| 3 | + * Usage: `node path/to/version.mjs <semver> [preid]` |
| 4 | + */ |
| 5 | + |
| 6 | +/* eslint-disable no-console -- CLI */ |
| 7 | +// @ts-check |
| 8 | + |
| 9 | +// -------------------------------------------------------------------------------- |
| 10 | +// Import |
| 11 | +// -------------------------------------------------------------------------------- |
| 12 | + |
| 13 | +import { execSync } from 'node:child_process'; |
| 14 | +import { styleText } from 'node:util'; |
| 15 | + |
| 16 | +// -------------------------------------------------------------------------------- |
| 17 | +// Helper |
| 18 | +// -------------------------------------------------------------------------------- |
| 19 | + |
| 20 | +const semver = process.argv[2]; |
| 21 | +const preid = process.argv[3] ?? ''; |
| 22 | + |
| 23 | +/** @param {Buffer<ArrayBufferLike>} buffer @returns {Record<string, any>} */ |
| 24 | +function stringifyBuffer(buffer) { |
| 25 | + return JSON.parse(buffer.toString()); |
| 26 | +} |
| 27 | + |
| 28 | +/** @param {string} text */ |
| 29 | +function bgCyan(text) { |
| 30 | + return styleText('bgCyan', text); |
| 31 | +} |
| 32 | + |
| 33 | +/** @param {string} text */ |
| 34 | +function cyan(text) { |
| 35 | + return styleText('cyan', text); |
| 36 | +} |
| 37 | + |
| 38 | +/** @param {string} text */ |
| 39 | +function green(text) { |
| 40 | + return styleText('green', text); |
| 41 | +} |
| 42 | + |
| 43 | +/** @param {string} text */ |
| 44 | +function magenta(text) { |
| 45 | + return styleText('magenta', text); |
| 46 | +} |
| 47 | + |
| 48 | +// -------------------------------------------------------------------------------- |
| 49 | +// Script: Bump workspace root and package versions |
| 50 | +// -------------------------------------------------------------------------------- |
| 51 | + |
| 52 | +console.log(` |
| 53 | +${bgCyan('Bump workspace root and package versions')} |
| 54 | +
|
| 55 | +> semver: ${cyan(semver)} |
| 56 | +> preid: ${cyan(preid)} |
| 57 | +`); |
| 58 | + |
| 59 | +execSync( |
| 60 | + `npm version ${semver} --preid ${preid} -w packages --no-workspaces-update --include-workspace-root --no-git-tag-version`, |
| 61 | + { |
| 62 | + stdio: 'inherit', |
| 63 | + }, |
| 64 | +); |
| 65 | + |
| 66 | +console.log(` |
| 67 | +${green('Successfully bumped workspace root and package versions')} |
| 68 | +`); |
| 69 | + |
| 70 | +// -------------------------------------------------------------------------------- |
| 71 | +// Script: Bump transitive dependency and dev-dependency versions |
| 72 | +// -------------------------------------------------------------------------------- |
| 73 | + |
| 74 | +console.log(` |
| 75 | +${bgCyan('Bump transitive dependency and dev-dependency versions')} |
| 76 | +`); |
| 77 | + |
| 78 | +const packages = stringifyBuffer(execSync('npm pkg get -ws')); |
| 79 | +const bumpedPackages = stringifyBuffer(execSync('npm pkg get -w packages')); |
| 80 | + |
| 81 | +/** @type {Map<string, string>} */ |
| 82 | +const bumpedPackagesMap = new Map( |
| 83 | + Object.entries(bumpedPackages).map(([packageName, packageJson]) => [ |
| 84 | + packageName, |
| 85 | + `^${packageJson.version}`, |
| 86 | + ]), |
| 87 | +); |
| 88 | + |
| 89 | +for (const [packageName, packageJson] of Object.entries(packages)) { |
| 90 | + console.log('Workspace:', magenta(packageName)); |
| 91 | + |
| 92 | + // Step 1: Check dependencies. |
| 93 | + if (packageJson.dependencies) { |
| 94 | + for (const [depName, oldDepVersion] of Object.entries(packageJson.dependencies)) { |
| 95 | + if (!bumpedPackagesMap.has(depName)) continue; |
| 96 | + |
| 97 | + const newDepVersion = String(bumpedPackagesMap.get(depName)); |
| 98 | + |
| 99 | + console.log( |
| 100 | + '> Bump transitive dependency:', |
| 101 | + cyan(depName), |
| 102 | + 'from', |
| 103 | + cyan(oldDepVersion), |
| 104 | + 'to', |
| 105 | + cyan(newDepVersion), |
| 106 | + ); |
| 107 | + |
| 108 | + execSync( |
| 109 | + `npm pkg set dependencies.${depName}="${newDepVersion}" -w ${packageName}`, |
| 110 | + ); |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + // Step 2: Check dev-dependencies. |
| 115 | + if (packageJson.devDependencies) { |
| 116 | + for (const [depName, oldDepVersion] of Object.entries(packageJson.devDependencies)) { |
| 117 | + if (!bumpedPackagesMap.has(depName)) continue; |
| 118 | + |
| 119 | + const newDepVersion = String(bumpedPackagesMap.get(depName)); |
| 120 | + |
| 121 | + console.log( |
| 122 | + '> Bump transitive dev-dependency:', |
| 123 | + cyan(depName), |
| 124 | + 'from', |
| 125 | + cyan(oldDepVersion), |
| 126 | + 'to', |
| 127 | + cyan(newDepVersion), |
| 128 | + ); |
| 129 | + |
| 130 | + execSync( |
| 131 | + `npm pkg set devDependencies.${depName}="${newDepVersion}" -w ${packageName}`, |
| 132 | + ); |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + console.log(); // New line. |
| 137 | +} |
| 138 | + |
| 139 | +console.log(` |
| 140 | +${green('Successfully bumped transitive dependency and dev-dependency versions')} |
| 141 | +`); |
| 142 | + |
| 143 | +// -------------------------------------------------------------------------------- |
| 144 | +// Script: Run `npm install` to update lockfile |
| 145 | +// -------------------------------------------------------------------------------- |
| 146 | + |
| 147 | +console.log(` |
| 148 | +${bgCyan('Run `npm install` to update lockfile')} |
| 149 | +`); |
| 150 | + |
| 151 | +execSync('npm install', { stdio: 'inherit' }); |
| 152 | + |
| 153 | +console.log(` |
| 154 | +${green('Successfully ran `npm install` to update lockfile')} |
| 155 | +`); |
0 commit comments