|
| 1 | +import { fileURLToPath } from "node:url"; |
| 2 | + |
| 3 | +const packageName = "@claudiu-ceia/dhash"; |
| 4 | +const npm = Deno.build.os === "windows" ? "npm.cmd" : "npm"; |
| 5 | +const npx = Deno.build.os === "windows" ? "npx.cmd" : "npx"; |
| 6 | +const fixtureDirectory = fileURLToPath(new URL("../tests", import.meta.url)); |
| 7 | + |
| 8 | +async function run( |
| 9 | + command: string, |
| 10 | + args: string[], |
| 11 | + cwd: string, |
| 12 | + env?: Record<string, string>, |
| 13 | +) { |
| 14 | + const child = new Deno.Command(command, { |
| 15 | + args, |
| 16 | + cwd, |
| 17 | + env, |
| 18 | + stdin: "null", |
| 19 | + stdout: "inherit", |
| 20 | + stderr: "inherit", |
| 21 | + }).spawn(); |
| 22 | + const status = await child.status; |
| 23 | + |
| 24 | + if (!status.success) { |
| 25 | + throw new Error(`${command} exited with code ${status.code}`); |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +const directory = await Deno.makeTempDir({ prefix: "dhash-npm-" }); |
| 30 | +const archive = `${directory}/dhash.tgz`; |
| 31 | + |
| 32 | +try { |
| 33 | + await run( |
| 34 | + Deno.execPath(), |
| 35 | + ["pack", "--allow-dirty", "--ignore=deno.lock", "--output", archive], |
| 36 | + Deno.cwd(), |
| 37 | + ); |
| 38 | + |
| 39 | + await Deno.writeTextFile( |
| 40 | + `${directory}/package.json`, |
| 41 | + JSON.stringify({ private: true, type: "module" }), |
| 42 | + ); |
| 43 | + await Deno.copyFile( |
| 44 | + new URL("./npm_integration.mjs", import.meta.url), |
| 45 | + `${directory}/integration.mjs`, |
| 46 | + ); |
| 47 | + await Deno.writeTextFile( |
| 48 | + `${directory}/consumer.ts`, |
| 49 | + `import { type DHashOptions, dhash } from "${packageName}"; |
| 50 | +const options: DHashOptions = { invert: true }; |
| 51 | +const hash: Promise<string> = dhash(new Uint8Array(), options); |
| 52 | +void hash; |
| 53 | +`, |
| 54 | + ); |
| 55 | + await Deno.writeTextFile( |
| 56 | + `${directory}/tsconfig.json`, |
| 57 | + JSON.stringify({ |
| 58 | + compilerOptions: { |
| 59 | + module: "NodeNext", |
| 60 | + moduleResolution: "NodeNext", |
| 61 | + noEmit: true, |
| 62 | + strict: true, |
| 63 | + target: "ES2022", |
| 64 | + }, |
| 65 | + include: ["consumer.ts"], |
| 66 | + }), |
| 67 | + ); |
| 68 | + |
| 69 | + await run(npm, ["install", "--no-audit", "--no-fund", archive], directory); |
| 70 | + await run(npm, ["audit", "--omit=dev", "--audit-level=high"], directory); |
| 71 | + await run( |
| 72 | + npx, |
| 73 | + ["--yes", "--package=typescript@7.0.2", "tsc", "--project", "."], |
| 74 | + directory, |
| 75 | + ); |
| 76 | + |
| 77 | + const versions = Deno.args.length > 0 ? Deno.args : [null]; |
| 78 | + for (const version of versions) { |
| 79 | + const command = version === null ? "node" : npx; |
| 80 | + const args = version === null |
| 81 | + ? ["integration.mjs"] |
| 82 | + : ["--yes", `--package=node@${version}`, "node", "integration.mjs"]; |
| 83 | + await run(command, args, directory, { |
| 84 | + DHASH_FIXTURES: fixtureDirectory, |
| 85 | + }); |
| 86 | + } |
| 87 | + |
| 88 | + console.log(`Verified ${packageName} from a packed npm tarball.`); |
| 89 | +} finally { |
| 90 | + await Deno.remove(directory, { recursive: true }); |
| 91 | +} |
0 commit comments