Skip to content

Commit

Permalink
fix(pack-directory): Accept lazy Package, passing directory as second…
Browse files Browse the repository at this point in the history
… parameter
  • Loading branch information
evocateur committed Dec 21, 2018
1 parent 26e6ec2 commit c6819c0
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 13 deletions.
3 changes: 2 additions & 1 deletion commands/publish/index.js
Expand Up @@ -519,7 +519,7 @@ class PublishCommand extends Command {
this.options.requireScripts && (pkg => this.execScript(pkg, "prepublish")),

pkg =>
pulseTillDone(packDirectory(pkg, opts)).then(packed => {
pulseTillDone(packDirectory(pkg, pkg.location, opts)).then(packed => {
tracker.completeWork(1);

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

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

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

return pFinally(chain, () => tracker.finish());
Expand Down
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

63 changes: 62 additions & 1 deletion utils/pack-directory/__tests__/pack-directory.test.js
@@ -1,5 +1,8 @@
"use strict";

// actually _run_ the lifecycles, gorrammit
jest.unmock("@lerna/run-lifecycle");

const fs = require("fs-extra");
const path = require("path");
const normalizePath = require("normalize-path");
Expand Down Expand Up @@ -76,7 +79,9 @@ describe("pack-directory", () => {
const pkgs = await getPackages(cwd);

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

// the generated tarball is _not_ moved into the package directory
expect(fs.move).not.toHaveBeenCalled();
Expand Down Expand Up @@ -145,5 +150,61 @@ Object {
`);
expect(tail.integrity.toString()).toMatch(INTEGRITY_PATTERN);
expect(tail.shasum).toMatch(SHASUM_PATTERN);

const lazy = await packDirectory(
{
name: "package-3",
// scripts are only read once, effectively ignoring pkg.refresh()
scripts: {
prepublish: "exit 1",
prepublishOnly: "echo badgerbadgerbadgerbadger > index.js",
},
// pkg.version is "live", thus this custom value is overwritten by pkg.refresh()
version: "1.2.3",
},
path.join(cwd, "package-3"),
Object.assign({}, conf, {
"ignore-prepublish": true,
"lerna-command": "publish",
})
);

expect(lazy).toMatchInlineSnapshot(`
Object {
"bundled": Array [],
"entryCount": 4,
"filename": "package-3-1.0.0.tgz",
"files": Array [
{
"mode": "MODE",
"path": "package.json",
"size": 385,
},
{
"mode": "MODE",
"path": "cli1.js",
"size": 108,
},
{
"mode": "MODE",
"path": "cli2.js",
"size": 108,
},
{
"mode": "MODE",
"path": "index.js",
"size": 25,
},
],
"id": "package-3@1.0.0",
"integrity": "INTEGRITY",
"name": "package-3",
"shasum": "SHASUM",
"size": 413,
"tarFilePath": "__TAR_DIR__/package-3-1.0.0.tgz",
"unpackedSize": 626,
"version": "1.0.0",
}
`);
});
});
28 changes: 17 additions & 11 deletions utils/pack-directory/lib/pack-directory.js
@@ -1,11 +1,13 @@
"use strict";

const path = require("path");
const figgyPudding = require("figgy-pudding");
const packlist = require("npm-packlist");
const log = require("libnpm/log");
const tar = require("tar");
const tempWrite = require("temp-write");
const getPacked = require("@lerna/get-packed");
const Package = require("@lerna/package");
const runLifecycle = require("@lerna/run-lifecycle");

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

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

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

let chain = Promise.resolve();

if (opts.ignorePrepublish !== false) {
if (opts.ignorePrepublish !== true) {
chain = chain.then(() => runLifecycle(pkg, "prepublish", opts));
}

Expand Down Expand Up @@ -64,7 +60,7 @@ function packDirectory(pkg, _opts) {
files.map(f => `./${f}`)
)
);
chain = chain.then(stream => tempWrite(stream, outputFileName));
chain = chain.then(stream => tempWrite(stream, getTarballName(pkg)));
chain = chain.then(tarFilePath =>
getPacked(pkg, tarFilePath).then(packed =>
Promise.resolve()
Expand All @@ -75,3 +71,13 @@ function packDirectory(pkg, _opts) {

return chain;
}

function getTarballName(pkg) {
const name =
pkg.name[0] === "@"
? // scoped packages get special treatment
pkg.name.substr(1).replace(/\//g, "-")
: pkg.name;

return `${name}-${pkg.version}.tgz`;
}
1 change: 1 addition & 0 deletions utils/pack-directory/package.json
Expand Up @@ -27,6 +27,7 @@
},
"dependencies": {
"@lerna/get-packed": "file:../get-packed",
"@lerna/package": "file:../../core/package",
"@lerna/run-lifecycle": "file:../run-lifecycle",
"figgy-pudding": "^3.5.1",
"libnpm": "^2.0.1",
Expand Down

0 comments on commit c6819c0

Please sign in to comment.