Repository navigation
Expand file tree
/
Copy pathpack.ts
More file actions
74 lines (63 loc) · 2.78 KB
/
Copy pathpack.ts
File metadata and controls
74 lines (63 loc) · 2.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import { $ } from "bun"
import { mkdir, rm } from "node:fs/promises"
import path from "node:path"
const root = path.resolve(import.meta.dir, "../../..")
// Release outputs land at the repository root, where the release workflow uploads them.
export const npmDir = path.join(root, "dist", "npm")
export const archiveDir = path.join(root, "dist", "archives")
type Manifest = {
name: string
version: string
exports: Record<string, string | null>
imports?: Record<string, Record<string, string>>
}
/**
* Builds the package in `directory`, points its TypeScript exports at the build output, and packs it
* into dist/npm. `types` is where the build writes declarations; `source` keeps `import` on the
* TypeScript sources for packages that ship them. `workspaceOnly` exports map to null, so the published package
* resolves none of them, even through a wildcard export.
*/
export async function pack(
directory: string,
options: { types?: string; source?: boolean; workspaceOnly?: readonly string[] } = {},
) {
const types = options.types ?? "./dist/"
const file = path.join(directory, "package.json")
const original = await Bun.file(file).text()
// SAFETY: release packages declare a name, a version, and string export targets.
const pkg = JSON.parse(original) as Manifest
const declaration = (value: string) => value.replace("./src/", types).replace(/\.tsx?$/, ".d.ts")
const output = (value: string) => value.replace("./src/", "./dist/").replace(/\.tsx?$/, ".js")
const tarball = path.join(npmDir, `${pkg.name.replace("@", "").replace("/", "-")}-${pkg.version}.tgz`)
await $`bun run build`.cwd(directory)
const exports = Object.fromEntries(
Object.entries(pkg.exports).map(([key, value]) => {
if (value === null || options.workspaceOnly?.includes(key)) return [key, null]
if (!/\.tsx?$/.test(value)) return [key, value]
// TypeScript takes the first condition it matches, so `types` goes first or it type-checks the `import` target.
return [key, { types: declaration(value), import: options.source ? value : output(value) }]
}),
)
const imports = pkg.imports
? Object.fromEntries(
Object.entries(pkg.imports).map(([key, conditions]) => [
key,
Object.fromEntries(
Object.entries(conditions).map(([condition, value]) => [
condition,
condition === "types" ? declaration(value) : output(value),
]),
),
]),
)
: undefined
await mkdir(npmDir, { recursive: true })
await rm(tarball, { force: true })
await Bun.write(file, JSON.stringify({ ...pkg, exports, ...(imports ? { imports } : {}) }, null, 2) + "\n")
try {
await $`bun pm pack --destination ${npmDir}`.cwd(directory)
} finally {
await Bun.write(file, original)
}
return tarball
}