From 99bd44bb430e634c5b898fe6ef993860ac6e42f0 Mon Sep 17 00:00:00 2001 From: Jeff Valore Date: Thu, 26 Apr 2018 07:27:17 -0400 Subject: [PATCH] fix(publish): Run pre and postpack lifecycle scripts on publish (#5712) **Summary** npm runs the pre and post pack scripts on publish, but yarn was not. Added these scripts. fixes #5707 **Test plan** Added a test that regex's the output to look for scripts being run. If you look at the test, the regex is kinda weird, but I did it that way to make sure the log statements were in the correct order in the output. --- __tests__/commands/publish.js | 19 +++++++++++++++++++ .../publish/lifecycle-scripts/package.json | 15 +++++++++++++++ src/cli/commands/publish.js | 3 +++ 3 files changed, 37 insertions(+) create mode 100644 __tests__/fixtures/publish/lifecycle-scripts/package.json diff --git a/__tests__/commands/publish.js b/__tests__/commands/publish.js index 98674b23f8..56c0263442 100644 --- a/__tests__/commands/publish.js +++ b/__tests__/commands/publish.js @@ -100,6 +100,25 @@ test.concurrent('publish should allow `--access` to override publishConfig.acces }); }); +test.concurrent('publish should run lifecycle scripts in the correct order', () => { + return runPublish([], {newVersion: '1.0.0'}, 'lifecycle-scripts', (config, reporter, stdout) => { + expect(stdout).toMatch( + new RegExp( + [ + 'running the prepublish hook[\\s\\S]*', + 'running the prepare hook[\\s\\S]*', + 'running the prepublishOnly hook[\\s\\S]*', + 'running the prepack hook[\\s\\S]*', + 'running the postpack hook[\\s\\S]*', + 'running the publish hook[\\s\\S]*', + 'running the postpublish hook', + ].join(''), + 'm', + ), + ); + }); +}); + test.concurrent('can specify a path', () => { return runPublish(['mypkg'], {newVersion: '0.0.1'}, 'subdir', config => { expect(config.registries.npm.request).toBeCalledWith( diff --git a/__tests__/fixtures/publish/lifecycle-scripts/package.json b/__tests__/fixtures/publish/lifecycle-scripts/package.json new file mode 100644 index 0000000000..062fa4c5f9 --- /dev/null +++ b/__tests__/fixtures/publish/lifecycle-scripts/package.json @@ -0,0 +1,15 @@ +{ + "name": "lifecycle-scripts", + "version": "1.0.0", + "main": "index.js", + "license": "MIT", + "scripts": { + "prepublish": "echo running the prepublish hook", + "prepublishOnly": "echo running the prepublishOnly hook", + "publish": "echo running the publish hook", + "postpublish": "echo running the postpublish hook", + "prepare": "echo running the prepare hook", + "prepack": "echo running the prepack hook", + "postpack": "echo running the postpack hook" + } +} diff --git a/src/cli/commands/publish.js b/src/cli/commands/publish.js index a52a1b1241..0cf87b3564 100644 --- a/src/cli/commands/publish.js +++ b/src/cli/commands/publish.js @@ -45,6 +45,7 @@ async function publish(config: Config, pkg: any, flags: Object, dir: string): Pr await config.executeLifecycleScript('prepublish'); await config.executeLifecycleScript('prepare'); await config.executeLifecycleScript('prepublishOnly'); + await config.executeLifecycleScript('prepack'); // get tarball stream const stat = await fs.lstat(dir); @@ -62,6 +63,8 @@ async function publish(config: Config, pkg: any, flags: Object, dir: string): Pr stream.on('data', data.push.bind(data)).on('end', () => resolve(Buffer.concat(data))).on('error', reject); }); + await config.executeLifecycleScript('postpack'); + // copy normalized package and remove internal keys as they may be sensitive or yarn specific pkg = Object.assign({}, pkg); for (const key in pkg) {