Skip to content

Commit

Permalink
feat(npm-publish): Store entire tarball metadata object on Package in…
Browse files Browse the repository at this point in the history
…stances
  • Loading branch information
evocateur committed Aug 27, 2018
1 parent 7200fd0 commit 063d743
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 20 deletions.
4 changes: 3 additions & 1 deletion commands/__mocks__/@lerna/npm-publish.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ const mockNpmPack = jest.fn((rootManifest, packages) => {
packed.add(pkg.name);

// simulate decoration after npm pack
pkg.tarball = `${pkg.name}-MOCKED.tgz`;
pkg.tarball = {
filename: `${pkg.name}-MOCKED.tgz`,
};
});

return Promise.resolve();
Expand Down
30 changes: 16 additions & 14 deletions utils/npm-publish/__tests__/npm-publish.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ describe("npm-publish", () => {
);

// technically decorated in npmPack, stubbed here
pkg.tarball = "test-1.10.100.tgz";
pkg.tarball = {
filename: "test-1.10.100.tgz",
};

it("runs npm publish in a directory with --tag support", async () => {
await npmPublish(pkg, "published-tag", { npmClient: "npm" });
Expand All @@ -45,7 +47,7 @@ describe("npm-publish", () => {
pkg,
}
);
expect(fs.remove).lastCalledWith(path.join(pkg.location, pkg.tarball));
expect(fs.remove).lastCalledWith(path.join(pkg.location, pkg.tarball.filename));
});

it("does not pass --tag when none present (npm default)", async () => {
Expand Down Expand Up @@ -153,8 +155,8 @@ describe("npmPack", () => {
// resolve promise
await cmd;

expect(pkg1.tarball).toBe("mocked-pkg-1-pack.tgz");
expect(pkg2.tarball).toBe("mocked-pkg-2-pack.tgz");
expect(pkg1.tarball.filename).toBe("mocked-pkg-1-pack.tgz");
expect(pkg2.tarball.filename).toBe("mocked-pkg-2-pack.tgz");

expect(hasNpmVersion).lastCalledWith(">=5.10.0");
expect(logPacked.mock.calls.map(call => call[0])).toEqual(expectedJSON);
Expand Down Expand Up @@ -195,8 +197,8 @@ describe("npmPack", () => {
// resolve promise
await cmd;

expect(pkg3.tarball).toBe("mocked-pkg-3-pack.tgz");
expect(pkg4.tarball).toBe("mocked-pkg-4-pack.tgz");
expect(pkg3.tarball.filename).toBe("mocked-pkg-3-pack.tgz");
expect(pkg4.tarball.filename).toBe("mocked-pkg-4-pack.tgz");

expect(logPacked.mock.calls.map(call => call[0])).toEqual(expectedJSON);
expect(process.stdout.write.mock.calls.map(call => call[0])).toEqual(chunks);
Expand All @@ -209,13 +211,13 @@ describe("npmPack", () => {

// post-hoc moving from root to leaf
expect(fs.move).toBeCalledWith(
path.join(pkg3.rootPath, pkg3.tarball),
path.join(pkg3.location, pkg3.tarball),
path.join(pkg3.rootPath, pkg3.tarball.filename),
path.join(pkg3.location, pkg3.tarball.filename),
{ overwrite: true }
);
expect(fs.move).toBeCalledWith(
path.join(pkg4.rootPath, pkg4.tarball),
path.join(pkg4.location, pkg4.tarball),
path.join(pkg4.rootPath, pkg4.tarball.filename),
path.join(pkg4.location, pkg4.tarball.filename),
{ overwrite: true }
);
});
Expand Down Expand Up @@ -250,10 +252,10 @@ describe("makePacker", () => {
// resolve all promises
await Promise.all(commands);

expect(packages[0].tarball).toBe("mocked-pkg-5-pack.tgz");
expect(packages[1].tarball).toBe("mocked-pkg-6-pack.tgz");
expect(packages[2].tarball).toBe("mocked-pkg-7-pack.tgz");
expect(packages[3].tarball).toBe("mocked-pkg-8-pack.tgz");
expect(packages[0].tarball.filename).toBe("mocked-pkg-5-pack.tgz");
expect(packages[1].tarball.filename).toBe("mocked-pkg-6-pack.tgz");
expect(packages[2].tarball.filename).toBe("mocked-pkg-7-pack.tgz");
expect(packages[3].tarball.filename).toBe("mocked-pkg-8-pack.tgz");

expect(hasNpmVersion).toHaveBeenCalledTimes(1);
expect(logPacked.mock.calls.map(call => call[0])).toEqual(expectedJSON);
Expand Down
12 changes: 7 additions & 5 deletions utils/npm-publish/npm-publish.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ function npmPublish(pkg, tag, { npmClient, registry }) {
}

// always add tarball file, created by npmPack()
args.push(pkg.tarball);
args.push(pkg.tarball.filename);

log.silly("exec", npmClient, args);
return ChildProcessUtilities.exec(npmClient, args, opts).then(() =>
// don't leave the generated tarball hanging around after success
fs.remove(path.join(pkg.location, pkg.tarball))
fs.remove(path.join(pkg.location, pkg.tarball.filename))
);
}

Expand Down Expand Up @@ -116,16 +116,18 @@ function npmPack(rootManifest, packages, opts = makePackOptions(rootManifest)) {

// npm pack --json list is in the same order as the packages input
for (let i = 0; i < packages.length; i += 1) {
// could do this inside the mapper, but we need this metadata regardless of p-map success
const pkg = packages[i];

// instead of complicated copypasta, use literally what npm did
pkg.tarball = tarballs[i].filename;
pkg.tarball = tarballs[i];
}

return pMap(
packages,
pkg => {
const inRoot = path.join(pkg.rootPath, pkg.tarball);
const toLeaf = path.join(pkg.location, pkg.tarball);
const inRoot = path.join(pkg.rootPath, pkg.tarball.filename);
const toLeaf = path.join(pkg.location, pkg.tarball.filename);

return fs.move(inRoot, toLeaf, { overwrite: true });
},
Expand Down

0 comments on commit 063d743

Please sign in to comment.