|
| 1 | +const path = require('path'); |
| 2 | +const process = require('process'); |
| 3 | +const fs = require('fs'); |
| 4 | +const childProcess = require('child_process'); |
| 5 | + |
| 6 | +const findUp = require('find-up'); |
| 7 | +const packList = require('npm-packlist'); |
| 8 | +const readPkg = require('read-pkg'); |
| 9 | + |
| 10 | +const serverlessPackage = require('../package.json'); |
| 11 | + |
| 12 | +// AWS Lambda layer are being uploaded as zip archive, whose content is then being unpacked to the /opt |
| 13 | +// directory in the lambda environment. |
| 14 | +// |
| 15 | +// So this script does the following: it builds a 'dist-awslambda-layer/nodejs/node_modules/@sentry/serverless' |
| 16 | +// directory with a special index.js and with all necessary @sentry packages symlinked as node_modules. |
| 17 | +// Then, this directory is compressed with zip. |
| 18 | +// |
| 19 | +// The tricky part about it is that one cannot just symlink the entire package directories into node_modules because |
| 20 | +// all the src/ contents and other unnecessary files will end up in the zip archive. So, we need to symlink only |
| 21 | +// individual files from package and it must be only those of them that are distributable. |
| 22 | +// There exists a `npm-packlist` library for such purpose. So we need to traverse all the dependencies, |
| 23 | +// execute `npm-packlist` on them and symlink the files into 'dist-awslambda-layer/.../@sentry/serverless/node_modules'. |
| 24 | +// I didn't find any way to achieve this goal using standard command-line tools so I have to write this script. |
| 25 | +// |
| 26 | +// Another, and much simpler way to assemble such zip bundle is install all the dependencies from npm registry and |
| 27 | +// just bundle the entire node_modules. |
| 28 | +// It's easier and looks more stable but it's inconvenient if one wants build a zip bundle out of current source tree. |
| 29 | +// |
| 30 | +// And yet another way is to bundle everything with webpack into a single file. I tried and it seems to be error-prone |
| 31 | +// so I think it's better to have a classic package directory with node_modules file structure. |
| 32 | + |
| 33 | +/** Recursively traverse all the dependencies and collect all the info to the map */ |
| 34 | +async function collectPackages(cwd, packages = {}) { |
| 35 | + const packageJson = await readPkg({ cwd }); |
| 36 | + |
| 37 | + packages[packageJson.name] = { cwd, packageJson }; |
| 38 | + |
| 39 | + if (!packageJson.dependencies) { |
| 40 | + return packages; |
| 41 | + } |
| 42 | + |
| 43 | + await Promise.all( |
| 44 | + Object.keys(packageJson.dependencies).map(async dep => { |
| 45 | + // We are interested only in 'external' dependencies which are strictly upper than current directory. |
| 46 | + // Internal deps aka local node_modules folder of each package is handled differently. |
| 47 | + const searchPath = path.resolve(cwd, '..'); |
| 48 | + const depPath = fs.realpathSync( |
| 49 | + await findUp(path.join('node_modules', dep), |
| 50 | + { type: 'directory', cwd: searchPath }) |
| 51 | + ); |
| 52 | + if (packages[dep]) { |
| 53 | + if (packages[dep].cwd != depPath) { |
| 54 | + throw new Error(`${packageJson.name}'s dependenciy ${dep} maps to both ${packages[dep].cwd} and ${depPath}`); |
| 55 | + } |
| 56 | + return; |
| 57 | + } |
| 58 | + await collectPackages(depPath, packages); |
| 59 | + }), |
| 60 | + ); |
| 61 | + |
| 62 | + return packages; |
| 63 | +} |
| 64 | + |
| 65 | +async function main() { |
| 66 | + const workDir = path.resolve(__dirname, '..'); // packages/serverless directory |
| 67 | + const packages = await collectPackages(workDir); |
| 68 | + |
| 69 | + const dist = path.resolve(workDir, 'dist-awslambda-layer'); |
| 70 | + const destRootRelative = 'nodejs/node_modules/@sentry/serverless'; |
| 71 | + const destRoot = path.resolve(dist, destRootRelative); |
| 72 | + const destModulesRoot = path.resolve(destRoot, 'node_modules'); |
| 73 | + |
| 74 | + try { |
| 75 | + // Setting `force: true` ignores exceptions when paths don't exist. |
| 76 | + fs.rmSync(destRoot, { force: true, recursive: true, maxRetries: 1 }); |
| 77 | + fs.mkdirSync(destRoot, { recursive: true }); |
| 78 | + } catch (error) { |
| 79 | + // Ignore errors. |
| 80 | + } |
| 81 | + |
| 82 | + await Promise.all( |
| 83 | + Object.entries(packages).map(async ([name, pkg]) => { |
| 84 | + const isRoot = name == serverlessPackage.name; |
| 85 | + const destPath = isRoot ? destRoot : path.resolve(destModulesRoot, name); |
| 86 | + |
| 87 | + // Scan over the distributable files of the module and symlink each of them. |
| 88 | + const sourceFiles = await packList({ path: pkg.cwd }); |
| 89 | + await Promise.all( |
| 90 | + sourceFiles.map(async filename => { |
| 91 | + const sourceFilename = path.resolve(pkg.cwd, filename); |
| 92 | + const destFilename = path.resolve(destPath, filename); |
| 93 | + |
| 94 | + try { |
| 95 | + fs.mkdirSync(path.dirname(destFilename), { recursive: true }); |
| 96 | + fs.symlinkSync(sourceFilename, destFilename); |
| 97 | + } catch (error) { |
| 98 | + // Ignore errors. |
| 99 | + } |
| 100 | + }), |
| 101 | + ); |
| 102 | + |
| 103 | + const sourceModulesRoot = path.resolve(pkg.cwd, 'node_modules'); |
| 104 | + // `fs.constants.F_OK` indicates whether the file is visible to the current process, but it doesn't check |
| 105 | + // its permissions. For more information, refer to https://nodejs.org/api/fs.html#fs_file_access_constants. |
| 106 | + try { |
| 107 | + fs.accessSync(path.resolve(sourceModulesRoot), fs.constants.F_OK); |
| 108 | + } catch (error) { |
| 109 | + return; |
| 110 | + } |
| 111 | + |
| 112 | + // Scan over local node_modules folder of the package and symlink its non-dev dependencies. |
| 113 | + const sourceModules = fs.readdirSync(sourceModulesRoot); |
| 114 | + await Promise.all( |
| 115 | + sourceModules.map(async sourceModule => { |
| 116 | + if (!pkg.packageJson.dependencies || !pkg.packageJson.dependencies[sourceModule]) { |
| 117 | + return; |
| 118 | + } |
| 119 | + |
| 120 | + const sourceModulePath = path.resolve(sourceModulesRoot, sourceModule); |
| 121 | + const destModulePath = path.resolve(destPath, 'node_modules', sourceModule); |
| 122 | + |
| 123 | + try { |
| 124 | + fs.mkdirSync(path.dirname(destModulePath), { recursive: true }); |
| 125 | + fs.symlinkSync(sourceModulePath, destModulePath); |
| 126 | + } catch (error) { |
| 127 | + // Ignore errors. |
| 128 | + } |
| 129 | + }), |
| 130 | + ); |
| 131 | + }), |
| 132 | + ); |
| 133 | + |
| 134 | + const version = serverlessPackage.version; |
| 135 | + const zipFilename = `sentry-node-serverless-${version}.zip`; |
| 136 | + |
| 137 | + try { |
| 138 | + fs.unlinkSync(path.resolve(dist, zipFilename)); |
| 139 | + } catch (error) { |
| 140 | + // If the ZIP file hasn't been previously created (e.g. running this script for the first time), |
| 141 | + // `unlinkSync` will try to delete a non-existing file. This error is ignored. |
| 142 | + } |
| 143 | + |
| 144 | + try { |
| 145 | + childProcess.execSync(`zip -r ${zipFilename} ${destRootRelative}`, { cwd: dist }); |
| 146 | + } catch (error) { |
| 147 | + // The child process timed out or had non-zero exit code. |
| 148 | + // The error contains the entire result from `childProcess.spawnSync`. |
| 149 | + console.log(error); // eslint-disable-line no-console |
| 150 | + } |
| 151 | +} |
| 152 | + |
| 153 | +main().then( |
| 154 | + () => { |
| 155 | + process.exit(0); |
| 156 | + }, |
| 157 | + err => { |
| 158 | + console.error(err); // eslint-disable-line no-console |
| 159 | + process.exit(-1); |
| 160 | + }, |
| 161 | +); |
0 commit comments