|
| 1 | +/* eslint-disable no-console */ |
| 2 | + |
| 3 | +// This script changes the file structure inside the `build` directory so that |
| 4 | +// the final structure is a) compatible with the Angular package format and |
| 5 | +// b) as similar as possible to our other SDKs. In order to achieve b), this |
| 6 | +// script moves the type declaration files (+maps) from `build` to `build/types` |
| 7 | +// and modifies the entry points in the generated package.json accordingly. |
| 8 | + |
| 9 | +// This script needs to be executed after compiling because our package.json |
| 10 | +// (i.e. the one that we maintain and that serves as a basis for ng-packagr) |
| 11 | +// also has to point to `build/types`. |
| 12 | + |
| 13 | +import * as fs from 'fs'; |
| 14 | +import * as path from 'path'; |
| 15 | + |
| 16 | +const BUILD_DIR = 'build'; |
| 17 | +const TYPES_DIR = 'types'; |
| 18 | + |
| 19 | +/** |
| 20 | + * Moves all type declaration files (*.d.ts, *.d.ts.map) to `build/types` to conform with |
| 21 | + * our other SDK's structure. |
| 22 | + */ |
| 23 | +function moveTypeDeclarations(): void { |
| 24 | + try { |
| 25 | + const buildDir = path.resolve(BUILD_DIR); |
| 26 | + const typesDir = path.resolve(BUILD_DIR, TYPES_DIR); |
| 27 | + |
| 28 | + if (!fs.existsSync(typesDir)) { |
| 29 | + fs.mkdirSync(typesDir); |
| 30 | + } |
| 31 | + |
| 32 | + const declarationFiles = fs |
| 33 | + .readdirSync(buildDir) |
| 34 | + .filter(file => file.endsWith('.d.ts') || file.endsWith('.d.ts.map')); |
| 35 | + |
| 36 | + declarationFiles.forEach(f => fs.renameSync(path.resolve(buildDir, f), path.resolve(typesDir, f))); |
| 37 | + } catch (error) { |
| 38 | + console.error('Error while moving type declaration files'); |
| 39 | + process.exit(-1); |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +/** |
| 44 | + * Takes the package.json file that is generated by `ng-packagr` when running `yarn build` |
| 45 | + * and modifies the type entry points to point to the `build/types` directory. |
| 46 | + * Additionally, this function deletes unnecessary properties that do not need to be |
| 47 | + * shipped with the NPM package. |
| 48 | + */ |
| 49 | +function modifyPkgJson(): void { |
| 50 | + const typeEntryPoints = ['types', 'typings']; |
| 51 | + const typeEntryFile = 'sentry-angular.d.ts'; |
| 52 | + |
| 53 | + try { |
| 54 | + // this is the path to the package.json that is generated by ng-packagr |
| 55 | + const packageJsonPath = path.resolve(BUILD_DIR, 'package.json'); |
| 56 | + const pkgJson: { [key: string]: unknown } = JSON.parse(fs.readFileSync(packageJsonPath, { encoding: 'utf8' })); |
| 57 | + |
| 58 | + const newPkgJson = { ...pkgJson }; |
| 59 | + |
| 60 | + typeEntryPoints.forEach(entryPoint => { |
| 61 | + newPkgJson[entryPoint] = path.join(TYPES_DIR, typeEntryFile); |
| 62 | + }); |
| 63 | + |
| 64 | + // We do not need volta information in the shipped package.json |
| 65 | + delete newPkgJson.volta; |
| 66 | + |
| 67 | + fs.writeFileSync(packageJsonPath, JSON.stringify(newPkgJson, null, 2)); |
| 68 | + } catch (error) { |
| 69 | + console.error('Error while modifying generated package.json'); |
| 70 | + process.exit(-1); |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +function postbuild(): void { |
| 75 | + moveTypeDeclarations(); |
| 76 | + modifyPkgJson(); |
| 77 | + console.log('\nSuccessfully finished postbuild commands for @sentry/angular\n'); |
| 78 | +} |
| 79 | + |
| 80 | +postbuild(); |
0 commit comments