|
| 1 | +/** |
| 2 | + * This script updates a package.json file by replacing all dependencies and devDependencies |
| 3 | + * such that all packages from the @angular scope point to the packages-dist directory. |
| 4 | + * |
| 5 | + * Please be aware that updating of versions might introduce compatibility issues. For instance, |
| 6 | + * if a peer dependency of Angular, e.g. "typescript" changes, the package.json that is updated |
| 7 | + * by this script will not have updated the "typescript" dependency to satisfy the peer dependency |
| 8 | + * requirement. As a result, incompatibility errors might occur. |
| 9 | + */ |
| 10 | +'use strict'; |
| 11 | + |
| 12 | +const {yellow, green} = require('chalk'); |
| 13 | +const {existsSync, writeFileSync} = require('fs'); |
| 14 | +const {resolve} = require('path'); |
| 15 | + |
| 16 | +const [, , packageJsonPath, packagesDistRoot] = process.argv; |
| 17 | + |
| 18 | +const packageJson = require(packageJsonPath); |
| 19 | + |
| 20 | +const updated = []; |
| 21 | +const skipped = []; |
| 22 | +function updateDeps(dependencies) { |
| 23 | + for (const packageName of Object.keys(dependencies)) { |
| 24 | + // We're only interested to update packages in the @angular scope |
| 25 | + if (!packageName.startsWith('@angular/')) { |
| 26 | + continue; |
| 27 | + } |
| 28 | + |
| 29 | + // Within the packages-dist directory there's no scope name |
| 30 | + const packageNameWithoutScope = packageName.replace('@angular/', ''); |
| 31 | + const packagePath = resolve(packagesDistRoot, packageNameWithoutScope); |
| 32 | + |
| 33 | + // Check whether the package exists in packages-dist. Not all packages |
| 34 | + // in the @angular scope are published from the main Angular repo. |
| 35 | + if (existsSync(packagePath)) { |
| 36 | + // Update the dependency to point to the packages-dist location. |
| 37 | + dependencies[packageName] = `file:${packagePath}`; |
| 38 | + updated.push(packageName); |
| 39 | + } else { |
| 40 | + skipped.push(packageName); |
| 41 | + } |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | + |
| 46 | +// Update dependencies from @angular scope to those in the packages-dist folder |
| 47 | +updateDeps(packageJson.dependencies); |
| 48 | +updateDeps(packageJson.devDependencies); |
| 49 | + |
| 50 | +// Write the updated package.json contents |
| 51 | +writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2)); |
| 52 | + |
| 53 | +// Log all packages that were updated |
| 54 | +if (updated.length > 0) { |
| 55 | + console.log(green(`Updated ${packageJsonPath} to packages in ${packagesDistRoot}:`)); |
| 56 | + console.log(` ${updated.join('\n ')}\n`); |
| 57 | +} |
| 58 | + |
| 59 | +// Log the packages that were skipped, as they were not present in the packages-dist directory |
| 60 | +if (skipped.length > 0) { |
| 61 | + console.log(yellow(`Did not update packages that were not present in ${packagesDistRoot}:`)); |
| 62 | + console.log(` ${skipped.join('\n ')}\n`); |
| 63 | +} |
0 commit comments