|
| 1 | +/* |
| 2 | + This script prepares the central `build` directory for NPM package creation. |
| 3 | + It first copies all non-code files into the `build` directory, including `package.json` which |
| 4 | + is edited to adjust entry point paths. These corrections are performed so that they align with |
| 5 | + the directory structure inside `build`. |
| 6 | +*/ |
| 7 | + |
| 8 | +import * as fs from 'fs'; |
| 9 | + |
| 10 | +const BUILD_DIR = 'build'; |
| 11 | +const ASSETS = ['README.md', 'LICENSE', 'package.json', '.npmignore']; |
| 12 | +const ENTRY_POINTS = ['main', 'module', 'types']; |
| 13 | + |
| 14 | +// check if build dirs exists |
| 15 | +if (!fs.existsSync(BUILD_DIR)) { |
| 16 | + console.error(`Directory ${BUILD_DIR} DOES NOT exist`); |
| 17 | + console.error("This script should only be executed after you've run `yarn build`."); |
| 18 | + process.exit(1); |
| 19 | +} |
| 20 | + |
| 21 | +// copy non-code assets to build dir |
| 22 | +ASSETS.forEach(asset => { |
| 23 | + if (!fs.existsSync(asset)) { |
| 24 | + console.error(`Asset ${asset} does not exist.`); |
| 25 | + process.exit(1); |
| 26 | + } |
| 27 | + fs.copyFileSync(asset, `${BUILD_DIR}/${asset}`); |
| 28 | +}); |
| 29 | + |
| 30 | +// package.json modifications |
| 31 | +const packageJsonPath = `${process.cwd()}/${BUILD_DIR}/package.json`; |
| 32 | +const pkg: { [key: string]: string } = require(packageJsonPath); |
| 33 | + |
| 34 | +// modify entry points to point to correct paths (i.e. delete the build directory) |
| 35 | +ENTRY_POINTS.filter(entryPoint => !!pkg[entryPoint]).forEach(entryPoint => { |
| 36 | + pkg[entryPoint] = pkg[entryPoint].replace(`${BUILD_DIR}/`, ''); |
| 37 | +}); |
| 38 | + |
| 39 | +// TODO decide if we want this: |
| 40 | +delete pkg.scripts; |
| 41 | +delete pkg.volta; |
| 42 | + |
| 43 | +// write modified package.json to file |
| 44 | +fs.writeFileSync(packageJsonPath, JSON.stringify(pkg, null, 2)); |
| 45 | + |
| 46 | +console.log('Successfully finishied postbuild commands'); |
0 commit comments