|
| 1 | +/* |
| 2 | + * Copyright (c) 2024, Salesforce, Inc. |
| 3 | + * All rights reserved. |
| 4 | + * SPDX-License-Identifier: MIT |
| 5 | + * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT |
| 6 | + */ |
| 7 | +const fs = require('node:fs'); |
| 8 | +const path = require('node:path'); |
| 9 | +const readline = require('node:readline'); |
| 10 | +const { globSync } = require('glob'); |
| 11 | + |
| 12 | +(async () => { |
| 13 | + const newVersion = await promptVersion(); |
| 14 | + updatePackages(newVersion); |
| 15 | +})().catch(console.error); |
| 16 | + |
| 17 | +async function promptVersion() { |
| 18 | + const rl = readline.createInterface({ |
| 19 | + input: process.stdin, |
| 20 | + output: process.stdout, |
| 21 | + }); |
| 22 | + |
| 23 | + try { |
| 24 | + const answer = await new Promise((resolve) => |
| 25 | + rl.question('Enter a new LWC version: ', resolve) |
| 26 | + ); |
| 27 | + return answer; |
| 28 | + } catch (error) { |
| 29 | + console.error(error); |
| 30 | + process.exit(1); |
| 31 | + } finally { |
| 32 | + rl.close(); |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +function updatePackages(newVersion) { |
| 37 | + try { |
| 38 | + const packagesToUpdate = getPackagesToUpdate(); |
| 39 | + const workspacesPackageJson = new Set( |
| 40 | + packagesToUpdate.map(({ packageJson }) => packageJson.name) |
| 41 | + ); |
| 42 | + |
| 43 | + for (const { packageJson } of packagesToUpdate) { |
| 44 | + packageJson.version = newVersion; |
| 45 | + // Look for different types of dependencies |
| 46 | + // ex: dependencies, devDependencies, peerDependencies |
| 47 | + const pkgDependencyTypes = Object.keys(packageJson).filter((key) => |
| 48 | + key.match(/.*[dD]ependencies/) |
| 49 | + ); |
| 50 | + // Update dependencies in package.json |
| 51 | + for (const pkgDependencyType of pkgDependencyTypes) { |
| 52 | + for (const pkgDepName of Object.keys(packageJson[pkgDependencyType])) { |
| 53 | + if (workspacesPackageJson.has(pkgDepName)) { |
| 54 | + // ex: packageJson[devDependencies][@lwc/template-compiler] |
| 55 | + packageJson[pkgDependencyType][pkgDepName] = newVersion; |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + // Update package.json files and print updated packges |
| 62 | + for (const { originalVersion, packageJson, packageJsonPath } of packagesToUpdate) { |
| 63 | + fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 4) + '\n'); |
| 64 | + console.log( |
| 65 | + `Updated ${packageJson.name} from ${originalVersion} to ${packageJson.version}` |
| 66 | + ); |
| 67 | + } |
| 68 | + } catch (error) { |
| 69 | + console.error(error); |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +function getPackagesToUpdate() { |
| 74 | + const rootPath = path.resolve(__dirname, '../../'); |
| 75 | + const rootPackageJsonPath = `${rootPath}/package.json`; |
| 76 | + const rootPackageJson = JSON.parse(fs.readFileSync(rootPackageJsonPath, 'utf-8')); |
| 77 | + const packagesToUpdate = []; |
| 78 | + |
| 79 | + const workspacePkgs = rootPackageJson.workspaces.reduce( |
| 80 | + (accWorkspacePkgs, workspace) => { |
| 81 | + const workspacePkg = globSync(`${workspace}/package.json`); |
| 82 | + return [...accWorkspacePkgs, ...workspacePkg]; |
| 83 | + }, |
| 84 | + [rootPackageJsonPath] |
| 85 | + ); |
| 86 | + |
| 87 | + for (const pkgName of workspacePkgs) { |
| 88 | + const packageJsonPath = path.resolve(rootPath, pkgName); |
| 89 | + if (fs.existsSync(packageJsonPath)) { |
| 90 | + const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf-8')); |
| 91 | + packagesToUpdate.push({ |
| 92 | + originalVersion: packageJson.version, |
| 93 | + packageJsonPath, |
| 94 | + packageJson, |
| 95 | + }); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + return packagesToUpdate; |
| 100 | +} |
0 commit comments