Skip to content

Commit

Permalink
feat: Add @lerna/get-packed
Browse files Browse the repository at this point in the history
  • Loading branch information
evocateur committed Dec 17, 2018
1 parent 00eb5bd commit 8675c8f
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 0 deletions.
11 changes: 11 additions & 0 deletions utils/get-packed/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# `@lerna/get-packed`

> Read contents of package tarball created by npm pack
## Usage

```
const getPacked = require('@lerna/get-packed');
// TODO: DEMONSTRATE API
```
9 changes: 9 additions & 0 deletions utils/get-packed/__tests__/get-packed.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
"use strict";

const getPacked = require("..");

describe("@lerna/get-packed", () => {
it("needs tests", () => {
expect(getPacked).toBeDefined();
});
});
68 changes: 68 additions & 0 deletions utils/get-packed/lib/get-packed.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
"use strict";

const fs = require("fs-extra");
const path = require("path");
const ssri = require("ssri");
const tar = require("tar");

module.exports = getPacked;

function getPacked(pkg, tarFilePath) {
const bundledWanted = new Set(pkg.bundleDependencies || pkg.bundledDependencies || []);
const bundled = new Set();
const files = [];

let totalEntries = 0;
let totalEntrySize = 0;

return tar
.list({
file: tarFilePath,
onentry(entry) {
totalEntries += 1;
totalEntrySize += entry.size;

const p = entry.path;

if (p.startsWith("package/node_modules/")) {
const name = p.match(/^package\/node_modules\/((?:@[^/]+\/)?[^/]+)/)[1];

if (bundledWanted.has(name)) {
bundled.add(name);
}
} else {
files.push({
path: entry.path.replace(/^package\//, ""),
size: entry.size,
mode: entry.mode,
});
}
},
strip: 1,
})
.then(() =>
Promise.all([
fs.stat(tarFilePath),
ssri.fromStream(fs.createReadStream(tarFilePath), {
algorithms: ["sha1", "sha512"],
}),
])
)
.then(([{ size }, { sha1, sha512 }]) => {
const shasum = sha1[0].hexDigest();

return {
id: `${pkg.name}@${pkg.version}`,
name: pkg.name,
version: pkg.version,
size,
unpackedSize: totalEntrySize,
shasum,
integrity: ssri.parse(sha512[0]),
filename: path.basename(tarFilePath),
files,
entryCount: totalEntries,
bundled: Array.from(bundled),
};
});
}
33 changes: 33 additions & 0 deletions utils/get-packed/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"name": "@lerna/get-packed",
"version": "3.6.0",
"description": "Read contents of package tarball created by npm pack",
"keywords": [
"lerna",
"npm",
"pack",
"tarball"
],
"author": "Daniel Stockman <[email protected]>",
"homepage": "https://github.com/lerna/lerna/tree/master/utils/get-packed#readme",
"license": "MIT",
"main": "lib/get-packed.js",
"files": [
"lib"
],
"publishConfig": {
"access": "public"
},
"repository": {
"type": "git",
"url": "git+https://github.com/lerna/lerna.git"
},
"scripts": {
"test": "echo \"Error: run tests from root\" && exit 1"
},
"dependencies": {
"fs-extra": "^7.0.0",
"ssri": "^6.0.1",
"tar": "^4.4.8"
}
}

0 comments on commit 8675c8f

Please sign in to comment.