From 6f872aad54c72354141eb3db0430a966a7594dc6 Mon Sep 17 00:00:00 2001 From: Vamsi D Date: Thu, 1 Jul 2021 09:50:07 +0530 Subject: [PATCH] feat(packager): ability to run custom scripts after packager install --- README.md | 4 ++++ src/index.ts | 9 ++++++++- src/pack-externals.ts | 18 ++++++++++++++++++ 3 files changed, 30 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 4a4e132a..0011215b 100644 --- a/README.md +++ b/README.md @@ -64,6 +64,10 @@ custom: esbuild: packager: yarn # optional - npm or yarn, default is npm packagePath: absolute/path/to/package.json # optional - by default it looks for a package.json in the working directory + packagerOptions: # optional - packager related options, currently supports only 'scripts' for both npm and yarn + scripts: # scripts to be executed, can be a string or array of strings + - echo 'Hello World!' + - rm -rf node_modules ``` To easily mark all the `dependencies` in `package.json` as `external`, you can utilize `esbuild-node-externals` [plugin](https://www.npmjs.com/package/esbuild-node-externals). diff --git a/src/index.ts b/src/index.ts index 7ca3eb01..cdaa9898 100644 --- a/src/index.ts +++ b/src/index.ts @@ -27,6 +27,10 @@ export interface WatchConfiguration { ignore?: string[] | string; } +export interface PackagerOptions { + scripts?: string[] | string; +} + export interface Configuration extends Omit { packager: 'npm' | 'yarn'; packagePath: string; @@ -34,6 +38,7 @@ export interface Configuration extends Omit { watch: WatchConfiguration; plugins?: string; keepOutputDirectory?: boolean; + packagerOptions?: PackagerOptions; } const DEFAULT_BUILD_OPTIONS: Partial = { @@ -46,7 +51,8 @@ const DEFAULT_BUILD_OPTIONS: Partial = { pattern: './**/*.(js|ts)', ignore: [WORK_FOLDER, 'dist', 'node_modules', SERVERLESS_FOLDER], }, - keepOutputDirectory: false + keepOutputDirectory: false, + packagerOptions: {}, }; export class EsbuildPlugin implements Plugin { @@ -238,6 +244,7 @@ export class EsbuildPlugin implements Plugin { delete config['packagePath']; delete config['watch']; delete config['keepOutputDirectory']; + delete config['packagerOptions']; const bundlePath = entry.substr(0, entry.lastIndexOf('.')) + '.js'; diff --git a/src/pack-externals.ts b/src/pack-externals.ts index 47637a5b..d97008c2 100644 --- a/src/pack-externals.ts +++ b/src/pack-externals.ts @@ -224,6 +224,15 @@ export async function packExternalModules(this: EsbuildPlugin) { // Fetch needed original package.json sections const sectionNames = packager.copyPackageSectionNames; + // Get scripts from packager options + const packagerScripts = this.buildOptions.packagerOptions + ? ([].concat(this.buildOptions.packagerOptions.scripts || [])) + .reduce((scripts, script, index) => { + scripts[`script${index}`] = script; + return scripts; + }, {}) + : {}; + const rootPackageJson: Record = this.serverless.utils.readFileSync( rootPackageJsonPath ); @@ -267,6 +276,7 @@ export async function packExternalModules(this: EsbuildPlugin) { version: '1.0.0', description: `Packaged externals for ${this.serverless.service.service}`, private: true, + scripts: packagerScripts, }, packageSections ); @@ -314,4 +324,12 @@ export async function packExternalModules(this: EsbuildPlugin) { await packager.prune(compositeModulePath); this.options.verbose && this.serverless.cli.log(`Prune: ${compositeModulePath} [${Date.now() - startPrune} ms]`); + + // Run packager scripts + if (Object.keys(packagerScripts).length > 0) { + const startScripts = Date.now(); + await packager.runScripts(this.buildDirPath, Object.keys(packagerScripts)); + this.options.verbose && + this.serverless.cli.log(`Packager scripts took [${Date.now() - startScripts} ms].\nExecuted scripts: ${Object.values(packagerScripts).map(script => `\n ${script}`)}`); + } }