|
| 1 | +const { readFileSync, writeFileSync } = require('node:fs'); |
| 2 | + |
| 3 | +const outDir = './lib'; |
| 4 | +const betaPackages = [ |
| 5 | + '@aws-lambda-powertools/parameters', |
| 6 | +]; |
| 7 | + |
| 8 | +/** |
| 9 | + * This script is used to create a new package.json file for the tarball that will be published to npm. |
| 10 | + * |
| 11 | + * The original package.json file is read and the following fields are extracted: |
| 12 | + * - name |
| 13 | + * - version |
| 14 | + * - description |
| 15 | + * - author |
| 16 | + * - license |
| 17 | + * - homepage |
| 18 | + * - repository |
| 19 | + * - bugs |
| 20 | + * - keywords |
| 21 | + * - dependencies |
| 22 | + * - devDependencies |
| 23 | + * |
| 24 | + * For beta packages, the version number is updated to include a beta suffix. |
| 25 | + * |
| 26 | + * The new package.json file is written to the lib folder, which is the folder that will be packed and published to npm. |
| 27 | + * For beta packages, the original package.json file is also temporarily updated with the new beta version number so that |
| 28 | + * the version number in the registry is correct and matches the tarball. |
| 29 | + */ |
| 30 | +(() => { |
| 31 | + try { |
| 32 | + // Read the original package.json file |
| 33 | + const pkgJson = JSON.parse(readFileSync('./package.json', 'utf8')) |
| 34 | + // Extract the fields we want to keep |
| 35 | + const { |
| 36 | + name, |
| 37 | + version: originalVersion, |
| 38 | + description, |
| 39 | + author, |
| 40 | + license, |
| 41 | + homepage, |
| 42 | + repository, |
| 43 | + bugs, |
| 44 | + keywords, |
| 45 | + dependencies, |
| 46 | + devDependencies, |
| 47 | + } = pkgJson; |
| 48 | + |
| 49 | + let version = originalVersion; |
| 50 | + // Add a beta suffix to the version |
| 51 | + if (betaPackages.includes(name)) { |
| 52 | + version = `${version}-beta`; |
| 53 | + } |
| 54 | + |
| 55 | + // Create a new package.json file with the updated version for the tarball |
| 56 | + const newPkgJson = { |
| 57 | + name, |
| 58 | + version, |
| 59 | + description, |
| 60 | + author, |
| 61 | + license, |
| 62 | + homepage, |
| 63 | + repository, |
| 64 | + bugs, |
| 65 | + keywords, |
| 66 | + dependencies, |
| 67 | + devDependencies, |
| 68 | + main: './index.js', |
| 69 | + types: './index.d.ts', |
| 70 | + }; |
| 71 | + |
| 72 | + // Write the new package.json file inside the folder that will be packed |
| 73 | + writeFileSync(`${outDir}/package.json`, JSON.stringify(newPkgJson, null, 2)); |
| 74 | + |
| 75 | + if (betaPackages.includes(name)) { |
| 76 | + // Temporarily update the original package.json file with the new beta version. |
| 77 | + // This version number will be picked up during the `npm publish` step, so that |
| 78 | + // the version number in the registry is correct and matches the tarball. |
| 79 | + // The original package.json file will be restored by lerna after the publish step. |
| 80 | + writeFileSync('package.json', JSON.stringify({ ...pkgJson, version }, null, 2)); |
| 81 | + } |
| 82 | + } catch (err) { |
| 83 | + throw err; |
| 84 | + } |
| 85 | +})(); |
0 commit comments