From 063d743de3d87e22e02ab51ed12c90111abcc1af Mon Sep 17 00:00:00 2001 From: Daniel Stockman Date: Mon, 27 Aug 2018 14:04:48 -0700 Subject: [PATCH] feat(npm-publish): Store entire tarball metadata object on Package instances --- commands/__mocks__/@lerna/npm-publish.js | 4 ++- .../npm-publish/__tests__/npm-publish.test.js | 30 ++++++++++--------- utils/npm-publish/npm-publish.js | 12 ++++---- 3 files changed, 26 insertions(+), 20 deletions(-) diff --git a/commands/__mocks__/@lerna/npm-publish.js b/commands/__mocks__/@lerna/npm-publish.js index 9f61334398..46592c1ffe 100644 --- a/commands/__mocks__/@lerna/npm-publish.js +++ b/commands/__mocks__/@lerna/npm-publish.js @@ -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(); diff --git a/utils/npm-publish/__tests__/npm-publish.test.js b/utils/npm-publish/__tests__/npm-publish.test.js index 0feba2e465..f53898e6b3 100644 --- a/utils/npm-publish/__tests__/npm-publish.test.js +++ b/utils/npm-publish/__tests__/npm-publish.test.js @@ -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" }); @@ -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 () => { @@ -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); @@ -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); @@ -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 } ); }); @@ -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); diff --git a/utils/npm-publish/npm-publish.js b/utils/npm-publish/npm-publish.js index 4b0aa29e55..7b77792e0e 100644 --- a/utils/npm-publish/npm-publish.js +++ b/utils/npm-publish/npm-publish.js @@ -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)) ); } @@ -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 }); },