Skip to content

Commit c6819c0

Browse files
committedDec 21, 2018
fix(pack-directory): Accept lazy Package, passing directory as second parameter
1 parent 26e6ec2 commit c6819c0

File tree

5 files changed

+83
-13
lines changed

5 files changed

+83
-13
lines changed
 

‎commands/publish/index.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -519,7 +519,7 @@ class PublishCommand extends Command {
519519
this.options.requireScripts && (pkg => this.execScript(pkg, "prepublish")),
520520

521521
pkg =>
522-
pulseTillDone(packDirectory(pkg, opts)).then(packed => {
522+
pulseTillDone(packDirectory(pkg, pkg.location, opts)).then(packed => {
523523
tracker.completeWork(1);
524524

525525
// store metadata for use in this.publishPacked()
@@ -573,6 +573,7 @@ class PublishCommand extends Command {
573573

574574
chain = chain.then(() => runParallelBatches(this.batchedPackages, this.concurrency, mapper));
575575

576+
// we do not run root "publish" lifecycle because it has a great chance of being cyclical
576577
chain = chain.then(() => this.runPackageLifecycle(this.project.manifest, "postpublish"));
577578

578579
return pFinally(chain, () => tracker.finish());

‎package-lock.json

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎utils/pack-directory/__tests__/pack-directory.test.js

+62-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
"use strict";
22

3+
// actually _run_ the lifecycles, gorrammit
4+
jest.unmock("@lerna/run-lifecycle");
5+
36
const fs = require("fs-extra");
47
const path = require("path");
58
const normalizePath = require("normalize-path");
@@ -76,7 +79,9 @@ describe("pack-directory", () => {
7679
const pkgs = await getPackages(cwd);
7780

7881
// choose first and last package since the middle two are repetitive
79-
const [head, tail] = await Promise.all([pkgs.shift(), pkgs.pop()].map(pkg => packDirectory(pkg, conf)));
82+
const [head, tail] = await Promise.all(
83+
[pkgs.shift(), pkgs.pop()].map(pkg => packDirectory(pkg, pkg.location, conf))
84+
);
8085

8186
// the generated tarball is _not_ moved into the package directory
8287
expect(fs.move).not.toHaveBeenCalled();
@@ -145,5 +150,61 @@ Object {
145150
`);
146151
expect(tail.integrity.toString()).toMatch(INTEGRITY_PATTERN);
147152
expect(tail.shasum).toMatch(SHASUM_PATTERN);
153+
154+
const lazy = await packDirectory(
155+
{
156+
name: "package-3",
157+
// scripts are only read once, effectively ignoring pkg.refresh()
158+
scripts: {
159+
prepublish: "exit 1",
160+
prepublishOnly: "echo badgerbadgerbadgerbadger > index.js",
161+
},
162+
// pkg.version is "live", thus this custom value is overwritten by pkg.refresh()
163+
version: "1.2.3",
164+
},
165+
path.join(cwd, "package-3"),
166+
Object.assign({}, conf, {
167+
"ignore-prepublish": true,
168+
"lerna-command": "publish",
169+
})
170+
);
171+
172+
expect(lazy).toMatchInlineSnapshot(`
173+
Object {
174+
"bundled": Array [],
175+
"entryCount": 4,
176+
"filename": "package-3-1.0.0.tgz",
177+
"files": Array [
178+
{
179+
"mode": "MODE",
180+
"path": "package.json",
181+
"size": 385,
182+
},
183+
{
184+
"mode": "MODE",
185+
"path": "cli1.js",
186+
"size": 108,
187+
},
188+
{
189+
"mode": "MODE",
190+
"path": "cli2.js",
191+
"size": 108,
192+
},
193+
{
194+
"mode": "MODE",
195+
"path": "index.js",
196+
"size": 25,
197+
},
198+
],
199+
"id": "package-3@1.0.0",
200+
"integrity": "INTEGRITY",
201+
"name": "package-3",
202+
"shasum": "SHASUM",
203+
"size": 413,
204+
"tarFilePath": "__TAR_DIR__/package-3-1.0.0.tgz",
205+
"unpackedSize": 626,
206+
"version": "1.0.0",
207+
}
208+
`);
148209
});
149210
});

‎utils/pack-directory/lib/pack-directory.js

+17-11
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
"use strict";
22

3+
const path = require("path");
34
const figgyPudding = require("figgy-pudding");
45
const packlist = require("npm-packlist");
56
const log = require("libnpm/log");
67
const tar = require("tar");
78
const tempWrite = require("temp-write");
89
const getPacked = require("@lerna/get-packed");
10+
const Package = require("@lerna/package");
911
const runLifecycle = require("@lerna/run-lifecycle");
1012

1113
module.exports = packDirectory;
@@ -18,21 +20,15 @@ const PackConfig = figgyPudding({
1820
ignorePrepublish: "ignore-prepublish",
1921
});
2022

21-
function packDirectory(pkg, _opts) {
23+
function packDirectory(_pkg, dir, _opts) {
24+
const pkg = Package.lazy(_pkg, dir);
2225
const opts = PackConfig(_opts);
23-
const dir = pkg.location;
24-
const name =
25-
pkg.name[0] === "@"
26-
? // scoped packages get special treatment
27-
pkg.name.substr(1).replace(/\//g, "-")
28-
: pkg.name;
29-
const outputFileName = `${name}-${pkg.version}.tgz`;
3026

31-
opts.log.verbose("packDirectory", dir);
27+
opts.log.verbose("pack-directory", path.relative(".", dir));
3228

3329
let chain = Promise.resolve();
3430

35-
if (opts.ignorePrepublish !== false) {
31+
if (opts.ignorePrepublish !== true) {
3632
chain = chain.then(() => runLifecycle(pkg, "prepublish", opts));
3733
}
3834

@@ -64,7 +60,7 @@ function packDirectory(pkg, _opts) {
6460
files.map(f => `./${f}`)
6561
)
6662
);
67-
chain = chain.then(stream => tempWrite(stream, outputFileName));
63+
chain = chain.then(stream => tempWrite(stream, getTarballName(pkg)));
6864
chain = chain.then(tarFilePath =>
6965
getPacked(pkg, tarFilePath).then(packed =>
7066
Promise.resolve()
@@ -75,3 +71,13 @@ function packDirectory(pkg, _opts) {
7571

7672
return chain;
7773
}
74+
75+
function getTarballName(pkg) {
76+
const name =
77+
pkg.name[0] === "@"
78+
? // scoped packages get special treatment
79+
pkg.name.substr(1).replace(/\//g, "-")
80+
: pkg.name;
81+
82+
return `${name}-${pkg.version}.tgz`;
83+
}

‎utils/pack-directory/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
},
2828
"dependencies": {
2929
"@lerna/get-packed": "file:../get-packed",
30+
"@lerna/package": "file:../../core/package",
3031
"@lerna/run-lifecycle": "file:../run-lifecycle",
3132
"figgy-pudding": "^3.5.1",
3233
"libnpm": "^2.0.1",

0 commit comments

Comments
 (0)
Please sign in to comment.