Skip to content

Commit

Permalink
feat: Add @lerna/log-packed module, extracted from npm
Browse files Browse the repository at this point in the history
  • Loading branch information
evocateur committed Aug 9, 2018
1 parent 012afcc commit 9c767ac
Show file tree
Hide file tree
Showing 5 changed files with 235 additions and 0 deletions.
14 changes: 14 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions utils/log-packed/README.md
@@ -0,0 +1,17 @@
# `@lerna/log-packed`

> Log the result of npm pack --json
Extracted from the [npm source](https://github.com/npm/cli/blob/4f801d8a476f7ca52b0f182bf4e17a80db12b4e2/lib/pack.js#L175-L212).

## Usage

```js
const execa = require('execa');
const logPacked = require('@lerna/log-packed');

execa('npm', ['pack', '--json']).then(result => {
const tarballs = JSON.parse(result.stdout);
tarballs.forEach(logPacked);
});
```
87 changes: 87 additions & 0 deletions utils/log-packed/__tests__/log-packed.test.js
@@ -0,0 +1,87 @@
"use strict";

const loggingOutput = require("@lerna-test/logging-output");
const logPacked = require("../lib/log-packed");

// remove quotes around top-level strings
expect.addSnapshotSerializer({
test(val) {
return typeof val === "string";
},
serialize(val, config, indentation, depth) {
// top-level strings don't need quotes, but nested ones do (object properties, etc)
return depth ? `"${val}"` : val;
},
});

const fixture = [
{
id: "package-1@1.1.0",
name: "package-1",
version: "1.1.0",
size: 223,
unpackedSize: 396,
shasum: "8f339308bfabffcddd89e379ab76c8fbbc5c429a",
integrity:
"sha512-s+D+5+Kovk2mi2Jg+P6egJ6ZBKzMOdRaWAL5S+a4dWnRa6taym9EeEwpwx5ASF1wbLb0Tduv2XqcwFATxxUGVw==",
filename: "package-1-1.1.0.tgz",
files: [
{
path: "package.json",
size: 396,
mode: 420,
},
],
entryCount: 1,
bundled: [],
},
{
id: "package-2@1.1.0",
name: "package-2",
version: "1.1.0",
size: 172,
unpackedSize: 99,
shasum: "9a868dcbaa1812afb10ae2366909d6bf3a1c6f95",
integrity:
"sha512-k9Ao8IyLZUq2fAT3a/8/B5lQI6de7mTTMuyUHjs6XVbKo69zQpKtBjFqk8DCh78gEyd05Ls4NbMINb3uj3wxkw==",
filename: "package-2-1.1.0.tgz",
files: [
{
path: "package.json",
size: 99,
mode: 420,
},
],
entryCount: 1,
bundled: [],
},
];

describe("@lerna/log-packed", () => {
it("logs tarball contents from json", () => {
fixture.forEach(logPacked);

expect(loggingOutput().join("\n")).toMatchInlineSnapshot(`
📦 package-1@1.1.0
396B package.json
name: package-1
version: 1.1.0
filename: package-1-1.1.0.tgz
package size: 223 B
unpacked size: 396 B
shasum: 8f339308bfabffcddd89e379ab76c8fbbc5c429a
integrity: sha512-s+D+5+Kovk2mi[...]XqcwFATxxUGVw==
total files: 1
📦 package-2@1.1.0
99B package.json
name: package-2
version: 1.1.0
filename: package-2-1.1.0.tgz
package size: 172 B
unpacked size: 99 B
shasum: 9a868dcbaa1812afb10ae2366909d6bf3a1c6f95
integrity: sha512-k9Ao8IyLZUq2f[...]bMINb3uj3wxkw==
total files: 1
`);
});
});
80 changes: 80 additions & 0 deletions utils/log-packed/lib/log-packed.js
@@ -0,0 +1,80 @@
"use strict";

const byteSize = require("byte-size");
const columnify = require("columnify");
const hasUnicode = require("has-unicode")();
const log = require("npmlog");

module.exports = logContents;

function logContents(tarball) {
log.notice("");
log.notice("", `${hasUnicode ? "📦 " : "package:"} ${tarball.name}@${tarball.version}`);
log.notice("=== Tarball Contents ===");

if (tarball.files.length) {
log.notice(
"",
columnify(
tarball.files.map(f => {
const bytes = byteSize(f.size);
return {
path: f.path,
size: `${bytes.value}${bytes.unit}`,
};
}),
{
include: ["size", "path"],
showHeaders: false,
}
)
);
}

if (tarball.bundled.length) {
log.notice("=== Bundled Dependencies ===");
tarball.bundled.forEach(name => log.notice("", name));
}

log.notice("=== Tarball Details ===");
log.notice(
"",
columnify(
[
{ name: "name:", value: tarball.name },
{ name: "version:", value: tarball.version },
tarball.filename && { name: "filename:", value: tarball.filename },
{ name: "package size:", value: byteSize(tarball.size) },
{ name: "unpacked size:", value: byteSize(tarball.unpackedSize) },
{ name: "shasum:", value: tarball.shasum },
{ name: "integrity:", value: elideIntegrity(tarball) },
tarball.bundled.length && {
name: "bundled deps:",
value: tarball.bundled.length,
},
tarball.bundled.length && {
name: "bundled files:",
value: tarball.entryCount - tarball.files.length,
},
tarball.bundled.length && {
name: "own files:",
value: tarball.files.length,
},
{ name: "total files:", value: tarball.entryCount },
].filter(x => x),
{
include: ["name", "value"],
showHeaders: false,
}
)
);

// an empty line
log.notice("", "");
}

function elideIntegrity(tarball) {
const str = tarball.integrity.toString();

return `${str.substr(0, 20)}[...]${str.substr(80)}`;
}
37 changes: 37 additions & 0 deletions utils/log-packed/package.json
@@ -0,0 +1,37 @@
{
"name": "@lerna/log-packed",
"version": "3.0.0-rc.0",
"description": "Log the result of npm pack --json",
"keywords": [
"lerna",
"npm",
"log",
"pack"
],
"author": "Daniel Stockman <daniel.stockman@gmail.com>",
"homepage": "https://github.com/lerna/lerna/tree/master/utils/log-packed#readme",
"license": "MIT",
"main": "lib/log-packed.js",
"files": [
"lib"
],
"engines": {
"node": ">= 6.9.0"
},
"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": {
"byte-size": "^4.0.3",
"columnify": "^1.5.4",
"has-unicode": "^2.0.1",
"npmlog": "^4.1.2"
}
}

0 comments on commit 9c767ac

Please sign in to comment.