Skip to content

Commit

Permalink
fix(publish): Call synthetic prepublishOnly lifecycle before packing
Browse files Browse the repository at this point in the history
This was lost when the unitary `npm publish` was split into `npm pack && npm publish <tgz>`.

Fixes #1169
  • Loading branch information
evocateur committed Aug 27, 2018
1 parent b4c4ee5 commit dda9812
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 3 deletions.
9 changes: 7 additions & 2 deletions commands/publish/__tests__/publish-lifecycle-scripts.test.js
Expand Up @@ -29,10 +29,14 @@ describe("lifecycle scripts", () => {
expect(runLifecycle).toHaveBeenCalledWith(expect.objectContaining({ name: "lifecycle" }), script);
});

// all leaf package lifecycles _EXCEPT_ postpublish are called by npm pack
// all leaf package lifecycles _EXCEPT_ prepublishOnly & postpublish are called by npm pack
expect(runLifecycle).not.toHaveBeenCalledWith(
expect.objectContaining({ name: "package-1" }),
expect.stringMatching(/(prepare|prepublishOnly)/)
expect.stringMatching("prepare")
);
expect(runLifecycle).toHaveBeenCalledWith(
expect.objectContaining({ name: "package-1" }),
expect.stringMatching("prepublishOnly")
);
expect(runLifecycle).toHaveBeenCalledWith(
expect.objectContaining({ name: "package-1" }),
Expand All @@ -56,6 +60,7 @@ describe("lifecycle scripts", () => {
// publish-specific
["lifecycle", "prepare"],
["lifecycle", "prepublishOnly"],
["package-1", "prepublishOnly"],
["package-1", "postpublish"],
["lifecycle", "postpublish"],
]);
Expand Down
8 changes: 7 additions & 1 deletion commands/publish/index.js
Expand Up @@ -424,7 +424,13 @@ class PublishCommand extends Command {
chain = chain.then(() => this.runPackageLifecycle(this.project.manifest, "prepare"));
chain = chain.then(() => this.runPackageLifecycle(this.project.manifest, "prepublishOnly"));

const actions = [];
const actions = [
pkg =>
// npm pack already runs prepare and prepublish
// prepublishOnly is _not_ run when publishing a tarball
// TECHNICALLY out of order, but not much we can do about that
this.runPackageLifecycle(pkg, "prepublishOnly"),
];

if (this.options.requireScripts) {
actions.push(pkg => this.execScript(pkg, "prepublish"));
Expand Down
1 change: 1 addition & 0 deletions integration/lerna-publish-lifecycle-silent.test.js
Expand Up @@ -37,6 +37,7 @@ postversion-package-1
postversion-root
prepare-root
prepublishOnly-root
prepublishOnly-package-1
prepublish-package-1
prepare-package-1
postpublish-package-1
Expand Down
5 changes: 5 additions & 0 deletions integration/lerna-publish-lifecycle.test.js
Expand Up @@ -70,6 +70,11 @@ prepare-root
prepublishOnly-root
> package-1@1.1.0 prepublishOnly __TEST_ROOTDIR__/packages/package-1
> echo prepublishOnly-package-1
prepublishOnly-package-1
> package-1@1.1.0 prepublish __TEST_ROOTDIR__/packages/package-1
> echo prepublish-package-1
Expand Down

2 comments on commit dda9812

@artembatura
Copy link

@artembatura artembatura commented on dda9812 Jan 6, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@evocateur Does Lerna run NPM Hook prepublishOnly on root package?

@evocateur
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.