Skip to content

Commit 49de10a

Browse files
committed
test: support npm 12 pack results
1 parent 77a01e6 commit 49de10a

1 file changed

Lines changed: 41 additions & 17 deletions

File tree

‎test/plugin-npm-package-manifest.test.ts‎

Lines changed: 41 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,38 @@ function writeFileText(filePath: string, text: string): void {
5656
writeFileSync(filePath, text, "utf8");
5757
}
5858

59+
type NpmPackResult = { filename: string; files: Array<{ path: string }> };
60+
61+
function parseNpmPackResult(stdout: string): NpmPackResult {
62+
const parsed = JSON.parse(stdout) as unknown;
63+
const candidates = Array.isArray(parsed)
64+
? parsed
65+
: parsed && typeof parsed === "object" && "files" in parsed
66+
? [parsed]
67+
: parsed && typeof parsed === "object"
68+
? Object.values(parsed)
69+
: [];
70+
const [packResult] = candidates;
71+
if (
72+
!packResult ||
73+
typeof packResult !== "object" ||
74+
!("filename" in packResult) ||
75+
typeof packResult.filename !== "string" ||
76+
!("files" in packResult) ||
77+
!Array.isArray(packResult.files) ||
78+
!packResult.files.every(
79+
(file: unknown) =>
80+
file !== null &&
81+
typeof file === "object" &&
82+
"path" in file &&
83+
typeof file.path === "string",
84+
)
85+
) {
86+
throw new Error("npm pack --json did not return a package result");
87+
}
88+
return packResult as NpmPackResult;
89+
}
90+
5991
function listNpmPackDryRunFiles(packageDir: string): string[] {
6092
const invocation = resolvePluginNpmCommand(["pack", "--dry-run", "--json", "--ignore-scripts"]);
6193
const result = spawnSync(invocation.command, invocation.args, {
@@ -74,19 +106,7 @@ function listNpmPackDryRunFiles(packageDir: string): string[] {
74106
if (result.status !== 0) {
75107
throw new Error(result.stderr.trim() || `npm pack failed with exit ${result.status}`);
76108
}
77-
const parsed = JSON.parse(result.stdout) as unknown;
78-
const packResult = (
79-
Array.isArray(parsed)
80-
? parsed[0]
81-
: parsed && typeof parsed === "object" && "files" in parsed
82-
? parsed
83-
: parsed && typeof parsed === "object"
84-
? Object.values(parsed)[0]
85-
: undefined
86-
) as { files?: { path?: string }[] } | undefined;
87-
return (packResult?.files ?? []).flatMap((entry) =>
88-
typeof entry.path === "string" ? [entry.path] : [],
89-
);
109+
return parseNpmPackResult(result.stdout).files.map((entry) => entry.path);
90110
}
91111

92112
function writePublishablePluginPackage(repoDir: string): string {
@@ -546,9 +566,7 @@ describe("plugin npm package manifest staging", () => {
546566
: {}),
547567
});
548568
expect(pack.status, pack.stderr).toBe(0);
549-
const [packedPackage] = JSON.parse(pack.stdout) as [
550-
{ filename: string; files: Array<{ path: string }> },
551-
];
569+
const packedPackage = parseNpmPackResult(pack.stdout);
552570
const packedFiles = packedPackage.files.map((file) => file.path);
553571
expect(packedFiles).toContain("dist/configured-state.cjs");
554572
expect(packedFiles).toContain("dist/auth-presence.cjs");
@@ -679,7 +697,13 @@ withAugmentedPluginNpmManifestForPackage(
679697
: {}),
680698
});
681699
if (pack.status !== 0) throw new Error(pack.stderr || "npm pack failed");
682-
const [packedPackage] = JSON.parse(pack.stdout);
700+
const parsedPack = JSON.parse(pack.stdout);
701+
const packedPackage = Array.isArray(parsedPack)
702+
? parsedPack[0]
703+
: parsedPack.files
704+
? parsedPack
705+
: Object.values(parsedPack)[0];
706+
if (!packedPackage?.files) throw new Error("npm pack --json did not return a package result");
683707
const packedFiles = packedPackage.files.map((file) => file.path);
684708
if (!packedFiles.includes("node_modules/local-runtime-dep/package.json")) {
685709
throw new Error("bundled runtime dependency was not packed");

0 commit comments

Comments
 (0)