|
| 1 | +import { execFileSync } from "node:child_process" |
| 2 | +import { existsSync, readFileSync } from "node:fs" |
| 3 | + |
| 4 | +const repoRoot = new URL("../", import.meta.url) |
| 5 | +const packageJsonPath = new URL("./package.json", repoRoot) |
| 6 | +const packageJson = JSON.parse(readFileSync(packageJsonPath, "utf8")) |
| 7 | + |
| 8 | +const requiredRepoFiles = [ |
| 9 | + "dist/index.js", |
| 10 | + "dist/index.d.ts", |
| 11 | + "dist/lib/config.js", |
| 12 | + "dist/tui/index.d.ts", |
| 13 | + "index.ts", |
| 14 | + "lib/config.ts", |
| 15 | + "tui/index.tsx", |
| 16 | + "tui/data/context.ts", |
| 17 | + "tui/routes/summary.tsx", |
| 18 | + "tui/shared/names.ts", |
| 19 | + "tui/shared/theme.ts", |
| 20 | + "tui/shared/types.ts", |
| 21 | + "tui/slots/sidebar-content.tsx", |
| 22 | + "README.md", |
| 23 | + "LICENSE", |
| 24 | + "dcp.schema.json", |
| 25 | +] |
| 26 | + |
| 27 | +const requiredTarballFiles = [ |
| 28 | + "package.json", |
| 29 | + "dist/index.js", |
| 30 | + "dist/index.d.ts", |
| 31 | + "dist/lib/config.js", |
| 32 | + "dist/tui/index.js", |
| 33 | + "dist/tui/index.d.ts", |
| 34 | + "index.ts", |
| 35 | + "lib/config.ts", |
| 36 | + "tui/index.tsx", |
| 37 | + "tui/data/context.ts", |
| 38 | + "tui/routes/summary.tsx", |
| 39 | + "tui/shared/names.ts", |
| 40 | + "tui/shared/theme.ts", |
| 41 | + "tui/shared/types.ts", |
| 42 | + "tui/slots/sidebar-content.tsx", |
| 43 | + "README.md", |
| 44 | + "LICENSE", |
| 45 | + "dcp.schema.json", |
| 46 | +] |
| 47 | + |
| 48 | +const forbiddenTarballPatterns = [ |
| 49 | + /^tui\/node_modules\//, |
| 50 | + /^node_modules\//, |
| 51 | + /^tests\//, |
| 52 | + /^scripts\//, |
| 53 | + /^docs\//, |
| 54 | + /^assets\//, |
| 55 | + /^notes\//, |
| 56 | + /^\.github\//, |
| 57 | + /^package-lock\.json$/, |
| 58 | +] |
| 59 | + |
| 60 | +const fail = (message) => { |
| 61 | + console.error(`package verification failed: ${message}`) |
| 62 | + process.exit(1) |
| 63 | +} |
| 64 | + |
| 65 | +for (const relativePath of requiredRepoFiles) { |
| 66 | + const absolutePath = new URL(`./${relativePath}`, repoRoot) |
| 67 | + if (!existsSync(absolutePath)) { |
| 68 | + fail(`missing required repo file '${relativePath}'`) |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +if (packageJson.exports?.["./tui"]?.import !== "./tui/index.tsx") { |
| 73 | + fail("expected package.json exports['./tui'].import to be './tui/index.tsx'") |
| 74 | +} |
| 75 | + |
| 76 | +if (packageJson.exports?.["."]?.import !== "./dist/index.js") { |
| 77 | + fail("expected package.json exports['.'].import to be './dist/index.js'") |
| 78 | +} |
| 79 | + |
| 80 | +const packOutput = execFileSync("npm", ["pack", "--dry-run", "--json"], { |
| 81 | + cwd: repoRoot, |
| 82 | + encoding: "utf8", |
| 83 | +}) |
| 84 | + |
| 85 | +const packResult = JSON.parse(packOutput) |
| 86 | +if (!Array.isArray(packResult) || packResult.length !== 1 || !Array.isArray(packResult[0]?.files)) { |
| 87 | + fail("unexpected npm pack JSON output") |
| 88 | +} |
| 89 | + |
| 90 | +const tarballFiles = new Set(packResult[0].files.map((entry) => entry.path)) |
| 91 | + |
| 92 | +for (const relativePath of requiredTarballFiles) { |
| 93 | + if (!tarballFiles.has(relativePath)) { |
| 94 | + fail(`tarball is missing required file '${relativePath}'`) |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +for (const relativePath of tarballFiles) { |
| 99 | + for (const pattern of forbiddenTarballPatterns) { |
| 100 | + if (pattern.test(relativePath)) { |
| 101 | + fail(`tarball contains forbidden path '${relativePath}'`) |
| 102 | + } |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +console.log(`package verification passed for ${packageJson.name}@${packageJson.version}`) |
| 107 | +console.log(`tarball entries: ${packResult[0].entryCount}`) |
0 commit comments