Skip to content

Commit

Permalink
fix(publish): Run pre and postpack lifecycle scripts on publish (yarn…
Browse files Browse the repository at this point in the history
…pkg#5712)

**Summary**

npm runs the pre and post pack scripts on publish, but yarn was not. Added these scripts.

fixes yarnpkg#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.
  • Loading branch information
rally25rs authored and BYK committed Apr 26, 2018
1 parent 8bf8eb0 commit 99bd44b
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
19 changes: 19 additions & 0 deletions __tests__/commands/publish.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
15 changes: 15 additions & 0 deletions __tests__/fixtures/publish/lifecycle-scripts/package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
3 changes: 3 additions & 0 deletions src/cli/commands/publish.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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) {
Expand Down

0 comments on commit 99bd44b

Please sign in to comment.