|
| 1 | +#!/usr/bin/env node |
| 2 | +// It will make following updates to package.json |
| 3 | +// 1 - align the version in a package.json file to the version of the repo |
| 4 | +// 2 - Remove all entries starting with @aws-cdk/* and constructs from "dependencies": { ... } |
| 5 | +// 3 - Remove all entries starting with @aws-cdk/* and constructs from "peerDependencies": { ... }, Add { "aws-cdk-lib": "^2.0.0-rc.1", "constructs": "^10.0.0" } |
| 6 | +// 4 - Add { "aws-cdk-lib": "2.0.0-rc.1", "constructs": "^10.0.0" } to "devDependencies" |
| 7 | +const fs = require('fs'); |
| 8 | + |
| 9 | +const findVersion = process.argv[2]; |
| 10 | +const replaceVersion = process.argv[3]; |
| 11 | + |
| 12 | +// these versions need to be sourced from a config file |
| 13 | +const awsCdkLibVersion = '2.0.0-rc.16'; |
| 14 | +const constructsVersion = '10.0.0'; |
| 15 | + |
| 16 | +for (const file of process.argv.splice(4)) { |
| 17 | + const pkg = JSON.parse(fs.readFileSync(file).toString()); |
| 18 | + |
| 19 | + if (pkg.version !== findVersion && pkg.version !== replaceVersion) { |
| 20 | + throw new Error(`unexpected - all package.json files in this repo should have a version of ${findVersion} or ${replaceVersion}: ${file}`); |
| 21 | + } |
| 22 | + |
| 23 | + pkg.version = replaceVersion; |
| 24 | + |
| 25 | + pkg.dependencies = processDependencies(pkg.dependencies || { }, file); |
| 26 | + pkg.peerDependencies = processPeerDependencies(pkg.peerDependencies || { }, file); |
| 27 | + pkg.devDependencies = processDevDependencies(pkg.devDependencies || { }, file); |
| 28 | + |
| 29 | + console.error(`${file} => ${replaceVersion}`); |
| 30 | + fs.writeFileSync(file, JSON.stringify(pkg, undefined, 2)); |
| 31 | + |
| 32 | +} |
| 33 | + |
| 34 | +function processDependencies(section, file) { |
| 35 | + let newdependencies = {}; |
| 36 | + for (const [ name, version ] of Object.entries(section)) { |
| 37 | + // Remove all entries starting with @aws-cdk/* and constructs |
| 38 | + if (!name.startsWith('@aws-cdk/') && !name.startsWith('constructs')) { |
| 39 | + newdependencies[name] = version.replace(findVersion, replaceVersion); |
| 40 | + } |
| 41 | + } |
| 42 | + return newdependencies; |
| 43 | +} |
| 44 | + |
| 45 | +function processPeerDependencies(section, file) { |
| 46 | + let newdependencies = {}; |
| 47 | + for (const [ name, version ] of Object.entries(section)) { |
| 48 | + // Remove all entries starting with @aws-cdk/* and constructs |
| 49 | + if (!name.startsWith('@aws-cdk/') && !name.startsWith('constructs')) { |
| 50 | + newdependencies[name] = version.replace(findVersion, replaceVersion); |
| 51 | + } |
| 52 | + } |
| 53 | + newdependencies["aws-cdk-lib"] = `^${awsCdkLibVersion}`; |
| 54 | + newdependencies["constructs"] = `^${constructsVersion}`; |
| 55 | + return newdependencies; |
| 56 | +} |
| 57 | + |
| 58 | +function processDevDependencies(section, file) { |
| 59 | + let newdependencies = section; |
| 60 | + for (const [ name, version ] of Object.entries(newdependencies)) { |
| 61 | + // Remove all entries starting with @aws-cdk/* and constructs |
| 62 | + if (version === findVersion || version === '^' + findVersion) { |
| 63 | + newdependencies[name] = version.replace(findVersion, replaceVersion); |
| 64 | + } |
| 65 | + } |
| 66 | + // note: no ^ to make sure we test against the minimum version |
| 67 | + newdependencies["aws-cdk-lib"] = `${awsCdkLibVersion}`; |
| 68 | + newdependencies["constructs"] = `^${constructsVersion}`; |
| 69 | + return newdependencies; |
| 70 | +} |
0 commit comments