Skip to content

Commit f26f870

Browse files
authored
feat: add inspect npm pack function (#174)
This allows snapshot testing of package content prior to publishing.
1 parent 8fa73fb commit f26f870

File tree

5 files changed

+115
-1
lines changed

5 files changed

+115
-1
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
exports['package exported files 1'] = {
2+
"name": "vis-dev-utils ",
3+
"files": [
4+
" LICENSE-APACHE-2.0",
5+
" LICENSE-MIT",
6+
" README.md",
7+
" babel-preset/index.js",
8+
" babel-register/index.js",
9+
" babel-register/index.js.map",
10+
" bin/generate-examples-index.js",
11+
" bin/generate-examples-index.js.map",
12+
" declarations/constants.d.ts",
13+
" declarations/constants.d.ts.map",
14+
" declarations/generate-rollup-configuration/index.d.ts",
15+
" declarations/generate-rollup-configuration/index.d.ts.map",
16+
" declarations/header.d.ts",
17+
" declarations/header.d.ts.map",
18+
" declarations/index.d.ts",
19+
" declarations/index.d.ts.map",
20+
" declarations/inspect-npm-pack.d.ts",
21+
" declarations/inspect-npm-pack.d.ts.map",
22+
" declarations/util.d.ts",
23+
" declarations/util.d.ts.map",
24+
" dist/assets/bundle-file.d.ts",
25+
" dist/assets/bundle-index.d.ts",
26+
" dist/assets/bundle-index.js",
27+
" dist/assets/bundle-root.d.ts",
28+
" dist/assets/bundle-root.js",
29+
" dist/vis-dev-utils.cjs.js",
30+
" dist/vis-dev-utils.cjs.js.map",
31+
" dist/vis-dev-utils.esm.js",
32+
" dist/vis-dev-utils.esm.js.map",
33+
" lib/header.d.ts",
34+
" lib/header.d.ts.map",
35+
" lib/header.js",
36+
" lib/header.js.map",
37+
" lib/inspect-npm-pack.d.ts",
38+
" lib/inspect-npm-pack.d.ts.map",
39+
" lib/inspect-npm-pack.js",
40+
" lib/inspect-npm-pack.js.map",
41+
" package.json"
42+
]
43+
}

src/module/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export * from "./constants";
22
export * from "./generate-rollup-configuration";
33
export * from "./header";
4+
export * from "./inspect-npm-pack";

src/module/inspect-npm-pack.ts

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import { spawnSync } from "child_process";
2+
3+
function runNpmPack(): string[] {
4+
const result = spawnSync("npm", ["pack", "--dry-run-a"], {
5+
env: { ...process.env, LC_ALL: "C" },
6+
stdio: "pipe",
7+
encoding: "utf-8"
8+
});
9+
10+
return result.stderr.split(/[\r\n]+/);
11+
}
12+
13+
function extractFiles(lines: string[]): string[] {
14+
const fileListingStart = lines.indexOf(
15+
"npm notice === Tarball Contents === "
16+
);
17+
const fileListingEnd = lines.indexOf("npm notice === Tarball Details === ");
18+
19+
const files = lines
20+
.slice(fileListingStart + 1, fileListingEnd)
21+
.map((line): string => line.replace(/^npm notice /, ""))
22+
.map((line): string => line.trim())
23+
.map((line): string =>
24+
line.replace(
25+
/^([^ ]+) +(.*)/,
26+
(_, size, path): string =>
27+
`${path}${size === "0" ? "(empty)" : " "}`
28+
)
29+
)
30+
.sort()
31+
.map(line => line.replace(/^(.*)(.{7})$/, "$2 $1"));
32+
33+
return files;
34+
}
35+
36+
function extractName(lines: string[]): string {
37+
const nameLine = lines.find((line): boolean =>
38+
/^npm notice name:/.test(line)
39+
);
40+
41+
if (nameLine == null) {
42+
throw new Error("Can't find the name of the package.");
43+
}
44+
45+
return nameLine.replace(/^npm notice name: */, "");
46+
}
47+
48+
export function inspectNpmPack(): { name: string; files: string[] } {
49+
const lines = runNpmPack();
50+
const files = extractFiles(lines);
51+
const name = extractName(lines);
52+
53+
return {
54+
name,
55+
files
56+
};
57+
}

test/package.test.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import snapshot from "snap-shot-it";
2+
3+
import { inspectNpmPack } from "../lib/inspect-npm-pack";
4+
5+
describe("package", function(): void {
6+
it("exported files", function(): void {
7+
snapshot(inspectNpmPack());
8+
});
9+
});

tsconfig.internal.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,9 @@
99
"target": "es2015"
1010
},
1111
"exclude": ["node_modules", "**/__tests__/*"],
12-
"include": ["@types ", "src/module/header.ts"]
12+
"include": [
13+
"@types",
14+
"src/module/header.ts",
15+
"src/module/inspect-npm-pack.ts"
16+
]
1317
}

0 commit comments

Comments
 (0)