|
| 1 | +/* eslint-disable no-console */ |
| 2 | +import * as childProcess from 'child_process'; |
| 3 | +import * as fs from 'fs'; |
| 4 | +import * as rimraf from 'rimraf'; |
| 5 | + |
| 6 | +import { ensureBundleBuildPrereqs } from '../../../scripts/ensure-bundle-deps'; |
| 7 | + |
| 8 | +/** |
| 9 | + * Run the given shell command, piping the shell process's `stdin`, `stdout`, and `stderr` to that of the current |
| 10 | + * process. Returns contents of `stdout`. |
| 11 | + */ |
| 12 | +function run(cmd: string, options?: childProcess.ExecSyncOptions): string { |
| 13 | + return String(childProcess.execSync(cmd, { stdio: 'inherit', ...options })); |
| 14 | +} |
| 15 | + |
| 16 | +async function buildLambdaLayer(): Promise<void> { |
| 17 | + // Create the main SDK bundle |
| 18 | + await ensureBundleBuildPrereqs({ |
| 19 | + dependencies: ['@sentry/utils', '@sentry/hub', '@sentry/core', '@sentry/tracing', '@sentry/node'], |
| 20 | + }); |
| 21 | + run('yarn rollup --config rollup.aws.config.js'); |
| 22 | + |
| 23 | + // We build a minified bundle, but it's standing in for the regular `index.js` file listed in `package.json`'s `main` |
| 24 | + // property, so we have to rename it so it's findable. |
| 25 | + fs.renameSync( |
| 26 | + 'build/aws/dist-serverless/nodejs/node_modules/@sentry/serverless/build/npm/cjs/index.min.js', |
| 27 | + 'build/aws/dist-serverless/nodejs/node_modules/@sentry/serverless/build/npm/cjs/index.js', |
| 28 | + ); |
| 29 | + |
| 30 | + // We're creating a bundle for the SDK, but still using it in a Node context, so we need to copy in `package.json`, |
| 31 | + // purely for its `main` property. |
| 32 | + console.log('Copying `package.json` into lambda layer.'); |
| 33 | + fs.copyFileSync('package.json', 'build/aws/dist-serverless/nodejs/node_modules/@sentry/serverless/package.json'); |
| 34 | + |
| 35 | + // The layer also includes `awslambda-auto.js`, a helper file which calls `Sentry.init()` and wraps the lambda |
| 36 | + // handler. It gets run when Node is launched inside the lambda, using the environment variable |
| 37 | + // |
| 38 | + // `NODE_OPTIONS="-r @sentry/serverless/dist/awslambda-auto"`. |
| 39 | + // |
| 40 | + // (The`-r` is what runs the script on startup.) The `dist` directory is no longer where we emit our built code, so |
| 41 | + // for backwards compatibility, we create a symlink. |
| 42 | + console.log('Creating symlink for `awslambda-auto.js` in legacy `dist` directory.'); |
| 43 | + fsForceMkdirSync('build/aws/dist-serverless/nodejs/node_modules/@sentry/serverless/dist'); |
| 44 | + fs.symlinkSync( |
| 45 | + '../build/npm/cjs/awslambda-auto.js', |
| 46 | + 'build/aws/dist-serverless/nodejs/node_modules/@sentry/serverless/dist/awslambda-auto.js', |
| 47 | + ); |
| 48 | +} |
| 49 | + |
| 50 | +void buildLambdaLayer(); |
| 51 | + |
| 52 | +/** |
| 53 | + * Make a directory synchronously, overwriting the old directory if necessary. |
| 54 | + * |
| 55 | + * This is what `fs.mkdirSync(path, { force: true })` would be, if it existed. Primarily useful for local building and |
| 56 | + * testing, where scripts are often run more than once (and so the directory you're trying to create may already be |
| 57 | + * there), but also harmless when used in CI. |
| 58 | + */ |
| 59 | +function fsForceMkdirSync(path: string): void { |
| 60 | + rimraf.sync(path); |
| 61 | + fs.mkdirSync(path); |
| 62 | +} |
0 commit comments