Skip to content

Commit

Permalink
feat(package): Add tarball property
Browse files Browse the repository at this point in the history
  • Loading branch information
evocateur committed Aug 8, 2018
1 parent ebc8ba6 commit be453cd
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
12 changes: 12 additions & 0 deletions core/package/__tests__/core-package.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,18 @@ describe("Package", () => {
});
});

describe("get .tarball", () => {
it("generates the destination filename of npm pack output", () => {
const pkg = factory({ name: "pack-me", version: "1.2.3" });
expect(pkg.tarball).toBe("pack-me-1.2.3.tgz");
});

it("normalizes package scopes", () => {
const pkg = factory({ name: "@scoped/pack-me", version: "4.5.6" });
expect(pkg.tarball).toBe("scoped-pack-me-4.5.6.tgz");
});
});

describe(".toJSON()", () => {
it("should return clone of internal package for serialization", () => {
const json = {
Expand Down
17 changes: 17 additions & 0 deletions core/package/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,17 @@ function binSafeName({ name, scope }) {
return scope ? name.substring(scope.length + 1) : name;
}

function packedFileName(pkg) {
// copied almost verbatim from https://git.io/fNobb
const name =
pkg.name[0] === "@"
? // scoped packages get special treatment
pkg.name.substr(1).replace(/\//g, "-")
: pkg.name;

return `${name}-${pkg.version}.tgz`;
}

// package.json files are not that complicated, so this is intentionally naïve
function shallowCopy(json) {
return Object.keys(json).reduce((obj, key) => {
Expand Down Expand Up @@ -116,6 +127,12 @@ class Package {
serialize: {
value: () => writePkg(this.manifestLocation, pkg),
},
// compute result of `npm pack` filename
tarball: {
get() {
return packedFileName(pkg);
},
},
});
}

Expand Down

0 comments on commit be453cd

Please sign in to comment.