Open History to see the last commit
1import assert from 'node:assert/strict'
2import { execFileSync } from 'node:child_process'
3import {
4 existsSync,
5 mkdtempSync,
6 readFileSync,
7 rmSync,
8 statSync,
9} from 'node:fs'
10import { tmpdir } from 'node:os'
11import { join } from 'node:path'
12
13type ExportTarget = string | { [condition: string]: ExportTarget }
14
15interface PackedManifest {
16 name: string
17 type?: string
18 keywords?: Array<string>
19 types: string
20 main?: string
21 engines: { node: string }
22 files: Array<string>
23 exports: Record<string, ExportTarget>
24 dependencies?: Record<string, string>
25 'ember-addon'?: { version: number; main: string }
26}
27
28const useClient = /^\s*['"]use client['"]/
29
30// Validate an actual pnpm tarball, not the workspace where unpublished source
31// files and stale build output can mask a broken release.
32const temporaryDirectory = mkdtempSync(join(tmpdir(), 'hotkeys-package-'))
33
34try {
35 const archive = join(temporaryDirectory, 'package.tgz')
36 execFileSync('pnpm', ['pack', '--out', archive], { stdio: 'pipe' })
37 const files = execFileSync('tar', ['-tzf', archive], { encoding: 'utf8' })
38 .trim()
39 .split('\n')
40 .filter((file) => !file.endsWith('/'))
41 .map((file) => file.replace(/^package\//, ''))
42
43 execFileSync('tar', ['-xzf', archive, '-C', temporaryDirectory])
44 const packageDirectory = join(temporaryDirectory, 'package')
45 const manifest = JSON.parse(
46 readFileSync(join(packageDirectory, 'package.json'), 'utf8'),
47 ) as PackedManifest
48
49 const emberAddon = manifest['ember-addon']?.version === 2
50 if (emberAddon) {
51 // ember-auto-import only treats packages with this keyword as v2 addons.
52 assert.ok(
53 manifest.keywords?.includes('ember-addon'),
54 'Ember v2 addons need the ember-addon keyword',
55 )
56 // Classic builds break when webpack applies strict ESM interop to
57 // `@ember/*` default imports, so v2 addons leave `type` unset.
58 assert.equal(manifest.type, undefined, 'Ember v2 addons must not set type')
59 } else {
60 assert.equal(manifest.type, 'module', 'Packages must be ESM')
61 }
62 assert.equal(
63 manifest.engines.node,
64 manifest.name === '@tanstack/octane-hotkeys' ? '>=22.22.2' : '>=20',
65 )
66 assert.equal(manifest.types, './dist/index.d.ts')
67 assert.equal(manifest.main, undefined, 'Do not restore a legacy main entry')
68 // Packages that ship Intent skills list `skills` after the build output.
69 const shipsSkills = manifest.files.at(-1) === 'skills'
70 assert.deepEqual(manifest.files, [
71 ...(emberAddon ? ['dist', 'addon-main.cjs'] : ['dist']),
72 ...(shipsSkills ? ['skills'] : []),
73 ])
74 if (shipsSkills) {
75 assert.ok(
76 files.some((file) => /^skills\/[a-z0-9-]+\/SKILL\.md$/.test(file)),
77 'The skills entry must publish at least one SKILL.md',
78 )
79 }
80 assert.ok(manifest.exports['.'], 'The package must export its main entry')
81
82 function checkFile(target: string) {
83 const file = target.replace(/^\.\//, '')
84 assert.ok(files.includes(file), `Missing packed export: ${target}`)
85 }
86
87 function checkExport(target: ExportTarget) {
88 if (typeof target === 'string') {
89 checkFile(target)
90 if (target.endsWith('.js')) {
91 checkFile(target.replace(/\.js$/, '.d.ts'))
92 }
93 return
94 }
95 assert.ok(
96 !('require' in target),
97 'CommonJS export conditions are forbidden',
98 )
99 for (const value of Object.values(target)) checkExport(value)
100 }
101
102 checkFile(manifest.types)
103 for (const target of Object.values(manifest.exports)) checkExport(target)
104 for (const version of Object.values(manifest.dependencies ?? {})) {
105 assert.ok(
106 !version.startsWith('workspace:'),
107 'Unresolved workspace dependency',
108 )
109 }
110
111 let unpackedBytes = 0
112 for (const file of files) {
113 assert.ok(
114 (emberAddon && file === 'addon-main.cjs') ||
115 file.startsWith('dist/') ||
116 /^skills\/[a-z0-9-]+\/(?:SKILL|references\/[a-z0-9-]+)\.md$/.test(
117 file,
118 ) ||
119 /^(package\.json|readme(?:\..*)?|licen[cs]e(?:\..*)?|changelog(?:\..*)?)$/i.test(
120 file,
121 ),
122 `Unexpected published file: ${file}`,
123 )
124 assert.ok(!/(^|\/)src\//.test(file), `Published source directory: ${file}`)
125 assert.ok(
126 (emberAddon && file === 'addon-main.cjs') ||
127 !/\.(?:cjs|cts|map)$/.test(file),
128 `Legacy output or map: ${file}`,
129 )
130 assert.ok(
131 !/\.[cm]?tsx?$/.test(file) || file.endsWith('.d.ts'),
132 `Uncompiled TypeScript: ${file}`,
133 )
134 const path = join(packageDirectory, file)
135 unpackedBytes += statSync(path).size
136 // React Server Components need 'use client' at the top of each built file
137 // whose source declares it.
138 const source = /^dist\/(.+)\.js$/.exec(file)?.[1]
139 const sourceFile = source
140 ? [`src/${source}.ts`, `src/${source}.tsx`].find((candidate) =>
141 existsSync(candidate),
142 )
143 : undefined
144 if (sourceFile && useClient.test(readFileSync(sourceFile, 'utf8'))) {
145 assert.ok(
146 useClient.test(readFileSync(path, 'utf8')),
147 `Lost 'use client' directive: ${file}`,
148 )
149 }
150 if (/\.(?:js|ts|svelte|css)$/.test(file)) {
151 assert.ok(
152 !/sourceMappingURL\s*=/.test(readFileSync(path, 'utf8')),
153 `Published source map reference: ${file}`,
154 )
155 }
156 }
157
158 console.log(
159 `${manifest.name}: verified ${files.length} packed files, ${statSync(archive).size} compressed bytes, ${unpackedBytes} unpacked bytes`,
160 )
161} finally {
162 rmSync(temporaryDirectory, { recursive: true, force: true })
163}