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) {