Skip to content
Permalink
Browse files
feat(package): Add tarball property
  • Loading branch information
evocateur committed Aug 8, 2018
1 parent ebc8ba6 commit be453cd189e515daecd3d08612741759345a8d15
Showing 2 changed files with 29 additions and 0 deletions.
@@ -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 = {
@@ -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) => {
@@ -116,6 +127,12 @@ class Package {
serialize: {
value: () => writePkg(this.manifestLocation, pkg),
},
// compute result of `npm pack` filename
tarball: {
get() {
return packedFileName(pkg);
},
},
});
}

0 comments on commit be453cd

Please sign in to comment.