Skip to content

Commit 9c767ac

Browse files
committed
feat: Add @lerna/log-packed module, extracted from npm
1 parent 012afcc commit 9c767ac

5 files changed

Lines changed: 235 additions & 0 deletions

File tree

‎package-lock.json‎

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎utils/log-packed/README.md‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# `@lerna/log-packed`
2+
3+
> Log the result of npm pack --json
4+
5+
Extracted from the [npm source](https://github.com/npm/cli/blob/4f801d8a476f7ca52b0f182bf4e17a80db12b4e2/lib/pack.js#L175-L212).
6+
7+
## Usage
8+
9+
```js
10+
const execa = require('execa');
11+
const logPacked = require('@lerna/log-packed');
12+
13+
execa('npm', ['pack', '--json']).then(result => {
14+
const tarballs = JSON.parse(result.stdout);
15+
tarballs.forEach(logPacked);
16+
});
17+
```
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
"use strict";
2+
3+
const loggingOutput = require("@lerna-test/logging-output");
4+
const logPacked = require("../lib/log-packed");
5+
6+
// remove quotes around top-level strings
7+
expect.addSnapshotSerializer({
8+
test(val) {
9+
return typeof val === "string";
10+
},
11+
serialize(val, config, indentation, depth) {
12+
// top-level strings don't need quotes, but nested ones do (object properties, etc)
13+
return depth ? `"${val}"` : val;
14+
},
15+
});
16+
17+
const fixture = [
18+
{
19+
20+
name: "package-1",
21+
version: "1.1.0",
22+
size: 223,
23+
unpackedSize: 396,
24+
shasum: "8f339308bfabffcddd89e379ab76c8fbbc5c429a",
25+
integrity:
26+
"sha512-s+D+5+Kovk2mi2Jg+P6egJ6ZBKzMOdRaWAL5S+a4dWnRa6taym9EeEwpwx5ASF1wbLb0Tduv2XqcwFATxxUGVw==",
27+
filename: "package-1-1.1.0.tgz",
28+
files: [
29+
{
30+
path: "package.json",
31+
size: 396,
32+
mode: 420,
33+
},
34+
],
35+
entryCount: 1,
36+
bundled: [],
37+
},
38+
{
39+
40+
name: "package-2",
41+
version: "1.1.0",
42+
size: 172,
43+
unpackedSize: 99,
44+
shasum: "9a868dcbaa1812afb10ae2366909d6bf3a1c6f95",
45+
integrity:
46+
"sha512-k9Ao8IyLZUq2fAT3a/8/B5lQI6de7mTTMuyUHjs6XVbKo69zQpKtBjFqk8DCh78gEyd05Ls4NbMINb3uj3wxkw==",
47+
filename: "package-2-1.1.0.tgz",
48+
files: [
49+
{
50+
path: "package.json",
51+
size: 99,
52+
mode: 420,
53+
},
54+
],
55+
entryCount: 1,
56+
bundled: [],
57+
},
58+
];
59+
60+
describe("@lerna/log-packed", () => {
61+
it("logs tarball contents from json", () => {
62+
fixture.forEach(logPacked);
63+
64+
expect(loggingOutput().join("\n")).toMatchInlineSnapshot(`
65+
66+
396B package.json
67+
name: package-1
68+
version: 1.1.0
69+
filename: package-1-1.1.0.tgz
70+
package size: 223 B
71+
unpacked size: 396 B
72+
shasum: 8f339308bfabffcddd89e379ab76c8fbbc5c429a
73+
integrity: sha512-s+D+5+Kovk2mi[...]XqcwFATxxUGVw==
74+
total files: 1
75+
76+
99B package.json
77+
name: package-2
78+
version: 1.1.0
79+
filename: package-2-1.1.0.tgz
80+
package size: 172 B
81+
unpacked size: 99 B
82+
shasum: 9a868dcbaa1812afb10ae2366909d6bf3a1c6f95
83+
integrity: sha512-k9Ao8IyLZUq2f[...]bMINb3uj3wxkw==
84+
total files: 1
85+
`);
86+
});
87+
});

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

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
"use strict";
2+
3+
const byteSize = require("byte-size");
4+
const columnify = require("columnify");
5+
const hasUnicode = require("has-unicode")();
6+
const log = require("npmlog");
7+
8+
module.exports = logContents;
9+
10+
function logContents(tarball) {
11+
log.notice("");
12+
log.notice("", `${hasUnicode ? "📦 " : "package:"} ${tarball.name}@${tarball.version}`);
13+
log.notice("=== Tarball Contents ===");
14+
15+
if (tarball.files.length) {
16+
log.notice(
17+
"",
18+
columnify(
19+
tarball.files.map(f => {
20+
const bytes = byteSize(f.size);
21+
return {
22+
path: f.path,
23+
size: `${bytes.value}${bytes.unit}`,
24+
};
25+
}),
26+
{
27+
include: ["size", "path"],
28+
showHeaders: false,
29+
}
30+
)
31+
);
32+
}
33+
34+
if (tarball.bundled.length) {
35+
log.notice("=== Bundled Dependencies ===");
36+
tarball.bundled.forEach(name => log.notice("", name));
37+
}
38+
39+
log.notice("=== Tarball Details ===");
40+
log.notice(
41+
"",
42+
columnify(
43+
[
44+
{ name: "name:", value: tarball.name },
45+
{ name: "version:", value: tarball.version },
46+
tarball.filename && { name: "filename:", value: tarball.filename },
47+
{ name: "package size:", value: byteSize(tarball.size) },
48+
{ name: "unpacked size:", value: byteSize(tarball.unpackedSize) },
49+
{ name: "shasum:", value: tarball.shasum },
50+
{ name: "integrity:", value: elideIntegrity(tarball) },
51+
tarball.bundled.length && {
52+
name: "bundled deps:",
53+
value: tarball.bundled.length,
54+
},
55+
tarball.bundled.length && {
56+
name: "bundled files:",
57+
value: tarball.entryCount - tarball.files.length,
58+
},
59+
tarball.bundled.length && {
60+
name: "own files:",
61+
value: tarball.files.length,
62+
},
63+
{ name: "total files:", value: tarball.entryCount },
64+
].filter(x => x),
65+
{
66+
include: ["name", "value"],
67+
showHeaders: false,
68+
}
69+
)
70+
);
71+
72+
// an empty line
73+
log.notice("", "");
74+
}
75+
76+
function elideIntegrity(tarball) {
77+
const str = tarball.integrity.toString();
78+
79+
return `${str.substr(0, 20)}[...]${str.substr(80)}`;
80+
}

‎utils/log-packed/package.json‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
{
2+
"name": "@lerna/log-packed",
3+
"version": "3.0.0-rc.0",
4+
"description": "Log the result of npm pack --json",
5+
"keywords": [
6+
"lerna",
7+
"npm",
8+
"log",
9+
"pack"
10+
],
11+
"author": "Daniel Stockman <[email protected]>",
12+
"homepage": "https://github.com/lerna/lerna/tree/master/utils/log-packed#readme",
13+
"license": "MIT",
14+
"main": "lib/log-packed.js",
15+
"files": [
16+
"lib"
17+
],
18+
"engines": {
19+
"node": ">= 6.9.0"
20+
},
21+
"publishConfig": {
22+
"access": "public"
23+
},
24+
"repository": {
25+
"type": "git",
26+
"url": "git+https://github.com/lerna/lerna.git"
27+
},
28+
"scripts": {
29+
"test": "echo \"Error: run tests from root\" && exit 1"
30+
},
31+
"dependencies": {
32+
"byte-size": "^4.0.3",
33+
"columnify": "^1.5.4",
34+
"has-unicode": "^2.0.1",
35+
"npmlog": "^4.1.2"
36+
}
37+
}

0 commit comments

Comments
 (0)