Skip to content

Commit 8675c8f

Browse files
committedDec 17, 2018
feat: Add @lerna/get-packed
1 parent 00eb5bd commit 8675c8f

File tree

4 files changed

+121
-0
lines changed

4 files changed

+121
-0
lines changed
 

‎utils/get-packed/README.md

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# `@lerna/get-packed`
2+
3+
> Read contents of package tarball created by npm pack
4+
5+
## Usage
6+
7+
```
8+
const getPacked = require('@lerna/get-packed');
9+
10+
// TODO: DEMONSTRATE API
11+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
"use strict";
2+
3+
const getPacked = require("..");
4+
5+
describe("@lerna/get-packed", () => {
6+
it("needs tests", () => {
7+
expect(getPacked).toBeDefined();
8+
});
9+
});

‎utils/get-packed/lib/get-packed.js

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
"use strict";
2+
3+
const fs = require("fs-extra");
4+
const path = require("path");
5+
const ssri = require("ssri");
6+
const tar = require("tar");
7+
8+
module.exports = getPacked;
9+
10+
function getPacked(pkg, tarFilePath) {
11+
const bundledWanted = new Set(pkg.bundleDependencies || pkg.bundledDependencies || []);
12+
const bundled = new Set();
13+
const files = [];
14+
15+
let totalEntries = 0;
16+
let totalEntrySize = 0;
17+
18+
return tar
19+
.list({
20+
file: tarFilePath,
21+
onentry(entry) {
22+
totalEntries += 1;
23+
totalEntrySize += entry.size;
24+
25+
const p = entry.path;
26+
27+
if (p.startsWith("package/node_modules/")) {
28+
const name = p.match(/^package\/node_modules\/((?:@[^/]+\/)?[^/]+)/)[1];
29+
30+
if (bundledWanted.has(name)) {
31+
bundled.add(name);
32+
}
33+
} else {
34+
files.push({
35+
path: entry.path.replace(/^package\//, ""),
36+
size: entry.size,
37+
mode: entry.mode,
38+
});
39+
}
40+
},
41+
strip: 1,
42+
})
43+
.then(() =>
44+
Promise.all([
45+
fs.stat(tarFilePath),
46+
ssri.fromStream(fs.createReadStream(tarFilePath), {
47+
algorithms: ["sha1", "sha512"],
48+
}),
49+
])
50+
)
51+
.then(([{ size }, { sha1, sha512 }]) => {
52+
const shasum = sha1[0].hexDigest();
53+
54+
return {
55+
id: `${pkg.name}@${pkg.version}`,
56+
name: pkg.name,
57+
version: pkg.version,
58+
size,
59+
unpackedSize: totalEntrySize,
60+
shasum,
61+
integrity: ssri.parse(sha512[0]),
62+
filename: path.basename(tarFilePath),
63+
files,
64+
entryCount: totalEntries,
65+
bundled: Array.from(bundled),
66+
};
67+
});
68+
}

‎utils/get-packed/package.json

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{
2+
"name": "@lerna/get-packed",
3+
"version": "3.6.0",
4+
"description": "Read contents of package tarball created by npm pack",
5+
"keywords": [
6+
"lerna",
7+
"npm",
8+
"pack",
9+
"tarball"
10+
],
11+
"author": "Daniel Stockman <daniel.stockman@gmail.com>",
12+
"homepage": "https://github.com/lerna/lerna/tree/master/utils/get-packed#readme",
13+
"license": "MIT",
14+
"main": "lib/get-packed.js",
15+
"files": [
16+
"lib"
17+
],
18+
"publishConfig": {
19+
"access": "public"
20+
},
21+
"repository": {
22+
"type": "git",
23+
"url": "git+https://github.com/lerna/lerna.git"
24+
},
25+
"scripts": {
26+
"test": "echo \"Error: run tests from root\" && exit 1"
27+
},
28+
"dependencies": {
29+
"fs-extra": "^7.0.0",
30+
"ssri": "^6.0.1",
31+
"tar": "^4.4.8"
32+
}
33+
}

0 commit comments

Comments
 (0)
Please sign in to comment.