|
| 1 | +import { cp, mkdir, readFile, writeFile } from "node:fs/promises"; |
| 2 | +import path from "node:path"; |
| 3 | +import { fileURLToPath } from "node:url"; |
| 4 | + |
| 5 | +type ExportTarget = { |
| 6 | + types?: string; |
| 7 | + default?: string; |
| 8 | +}; |
| 9 | + |
| 10 | +type PackageJson = { |
| 11 | + name: string; |
| 12 | + version: string; |
| 13 | + description?: string; |
| 14 | + keywords?: string[]; |
| 15 | + license?: string; |
| 16 | + repository?: unknown; |
| 17 | + type?: string; |
| 18 | + main?: string; |
| 19 | + types?: string; |
| 20 | + exports: Record<string, ExportTarget>; |
| 21 | + bin?: Record<string, string>; |
| 22 | + dependencies?: Record<string, string>; |
| 23 | +}; |
| 24 | + |
| 25 | +const repoRoot = path.resolve(fileURLToPath(new URL("..", import.meta.url))); |
| 26 | + |
| 27 | +export async function preparePackageDist(packageRoot: string) { |
| 28 | + const distDir = path.join(packageRoot, "dist"); |
| 29 | + const pkg = JSON.parse( |
| 30 | + await readFile(path.join(packageRoot, "package.json"), "utf8"), |
| 31 | + ) as PackageJson; |
| 32 | + |
| 33 | + await mkdir(distDir, { recursive: true }); |
| 34 | + |
| 35 | + const publishManifest = omitUndefined({ |
| 36 | + name: pkg.name, |
| 37 | + version: pkg.version, |
| 38 | + description: pkg.description, |
| 39 | + keywords: pkg.keywords, |
| 40 | + license: pkg.license, |
| 41 | + repository: pkg.repository, |
| 42 | + type: pkg.type, |
| 43 | + main: toDistRuntimePath(pkg.main), |
| 44 | + types: toDistTypesPath(pkg.types), |
| 45 | + exports: Object.fromEntries( |
| 46 | + Object.entries(pkg.exports).map(([subpath, target]) => [ |
| 47 | + subpath, |
| 48 | + omitUndefined({ |
| 49 | + types: toDistTypesPath(target.types), |
| 50 | + default: toDistRuntimePath(target.default), |
| 51 | + }), |
| 52 | + ]), |
| 53 | + ), |
| 54 | + bin: pkg.bin |
| 55 | + ? Object.fromEntries( |
| 56 | + Object.entries(pkg.bin).map(([name, target]) => [name, toDistRuntimePath(target)]), |
| 57 | + ) |
| 58 | + : undefined, |
| 59 | + dependencies: rewriteWorkspaceDependencies(pkg.dependencies, pkg.version), |
| 60 | + }); |
| 61 | + |
| 62 | + await writeFile( |
| 63 | + path.join(distDir, "package.json"), |
| 64 | + `${JSON.stringify(publishManifest, null, 2)}\n`, |
| 65 | + "utf8", |
| 66 | + ); |
| 67 | + |
| 68 | + await copyFirstExisting(distDir, [ |
| 69 | + path.join(packageRoot, "README.md"), |
| 70 | + path.join(repoRoot, "README.md"), |
| 71 | + ]); |
| 72 | + await copyFirstExisting(distDir, [ |
| 73 | + path.join(packageRoot, "LICENSE"), |
| 74 | + path.join(packageRoot, "LICENSE.md"), |
| 75 | + path.join(repoRoot, "LICENSE"), |
| 76 | + path.join(repoRoot, "LICENSE.md"), |
| 77 | + ]); |
| 78 | +} |
| 79 | + |
| 80 | +function toDistRuntimePath(filePath: string | undefined) { |
| 81 | + if (!filePath) return undefined; |
| 82 | + return toDistPath(filePath).replace(/\.(?:[cm]?ts|tsx)$/, ".js"); |
| 83 | +} |
| 84 | + |
| 85 | +function toDistTypesPath(filePath: string | undefined) { |
| 86 | + if (!filePath) return undefined; |
| 87 | + return toDistPath(filePath).replace(/\.(?:[cm]?ts|tsx)$/, ".d.ts"); |
| 88 | +} |
| 89 | + |
| 90 | +function toDistPath(filePath: string) { |
| 91 | + const normalized = filePath.startsWith("./") ? filePath.slice(2) : filePath; |
| 92 | + if (normalized.startsWith("src/")) return `./${normalized.slice(4)}`; |
| 93 | + if (normalized.startsWith("dist/")) return `./${normalized.slice(5)}`; |
| 94 | + return `./${normalized}`; |
| 95 | +} |
| 96 | + |
| 97 | +function rewriteWorkspaceDependencies( |
| 98 | + dependencies: Record<string, string> | undefined, |
| 99 | + packageVersion: string, |
| 100 | +) { |
| 101 | + if (!dependencies) return undefined; |
| 102 | + |
| 103 | + return Object.fromEntries( |
| 104 | + Object.entries(dependencies).map(([name, version]) => [ |
| 105 | + name, |
| 106 | + version === "workspace:*" ? packageVersion : version, |
| 107 | + ]), |
| 108 | + ); |
| 109 | +} |
| 110 | + |
| 111 | +function omitUndefined<T extends Record<string, unknown>>(value: T) { |
| 112 | + return Object.fromEntries(Object.entries(value).filter(([, entry]) => entry !== undefined)); |
| 113 | +} |
| 114 | + |
| 115 | +async function copyFirstExisting(distDir: string, candidates: string[]) { |
| 116 | + for (const candidate of candidates) { |
| 117 | + try { |
| 118 | + await cp(candidate, path.join(distDir, path.basename(candidate))); |
| 119 | + return; |
| 120 | + } catch {} |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +if (process.argv[1] && path.resolve(process.argv[1]) === fileURLToPath(import.meta.url)) { |
| 125 | + await preparePackageDist(process.cwd()); |
| 126 | +} |
0 commit comments