Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

check-cjs-types-smoke.mjs

137 lines3.6 KB

1import { execFile as execFileCallback } from 'node:child_process'
2import { mkdir, mkdtemp, rm, writeFile } from 'node:fs/promises'
3import { tmpdir } from 'node:os'
4import { dirname, join, resolve } from 'node:path'
5import { fileURLToPath } from 'node:url'
6import { promisify } from 'node:util'
7​
8const execFile = promisify(execFileCallback)
9const npmCommand = process.platform === 'win32' ? 'npm.cmd' : 'npm'
10const pnpmCommand = process.platform === 'win32' ? 'pnpm.cmd' : 'pnpm'
11const projectDir = resolve(dirname(fileURLToPath(import.meta.url)), '..')
12const workspaceRoot = resolve(projectDir, '..', '..')
13const typeRootsDir = resolve(workspaceRoot, 'node_modules', '@types')
14const tscEntrypoint = resolve(
15 workspaceRoot,
16 'node_modules',
17 'typescript',
18 'lib',
19 'tsc.js',
21​
22const tempRoot = await mkdtemp(join(tmpdir(), 'tanstack-lit-query-cjs-smoke-'))
23const packDir = resolve(tempRoot, 'pack')
24const consumerDir = resolve(tempRoot, 'consumer')
25​
26try {
27 const tarballPath = await packProject(packDir)
28​
29 await writeConsumerFixture(consumerDir)
30 await installConsumer(consumerDir, tarballPath)
31 await typecheckConsumer(consumerDir)
32​
33 console.log('CommonJS TypeScript smoke test passed.')
34} finally {
35 await rm(tempRoot, { recursive: true, force: true })
37​
38async function packProject(destination) {
39 await mkdir(destination, { recursive: true })
40​
41 const { stdout } = await execFile(
42 pnpmCommand,
43 ['pack', '--json', '--pack-destination', destination],
44 {
45 cwd: projectDir,
46 },
47 )
48 const packResult = JSON.parse(stdout)
49 const filename = Array.isArray(packResult)
50 ? packResult[0]?.filename
51 : packResult?.filename
52​
53 if (typeof filename !== 'string') {
54 throw new Error(`Unexpected pack output: ${stdout}`)
55 }
56​
57 return resolve(destination, filename)
59​
60async function writeConsumerFixture(consumerDirectory) {
61 await rm(consumerDirectory, { recursive: true, force: true })
62 await mkdir(consumerDirectory, { recursive: true })
63​
64 await writeFile(
65 resolve(consumerDirectory, 'package.json'),
66 `${JSON.stringify(
67 {
68 private: true,
69 type: 'commonjs',
70 },
71 null,
72 2,
73 )}\n`,
74 'utf8',
75 )
76​
77 await writeFile(
78 resolve(consumerDirectory, 'index.cts'),
79 [
80 "const pkg = require('@tanstack/lit-query')",
81 '',
82 "type CreateQueryOptions = import('@tanstack/lit-query').CreateQueryOptions<string>",
83 '',
84 'const options: CreateQueryOptions = {',
85 " queryKey: ['cjs-smoke'],",
86 " queryFn: async () => 'ok',",
87 '}',
88 '',
89 "if (typeof pkg.createQueryController !== 'function') {",
90 " throw new Error('createQueryController export is missing in CommonJS consumer.')",
91 '}',
92 '',
93 'void pkg.queryOptions(options)',
94 '',
95 ].join('\n'),
96 'utf8',
97 )
98​
99 await writeFile(
100 resolve(consumerDirectory, 'tsconfig.json'),
101 `${JSON.stringify(
102 {
103 compilerOptions: {
104 module: 'Node16',
105 moduleResolution: 'Node16',
106 target: 'ES2022',
107 strict: true,
108 noEmit: true,
109 types: ['node'],
110 typeRoots: [typeRootsDir],
111 },
112 include: ['index.cts'],
113 },
114 null,
115 2,
116 )}\n`,
117 'utf8',
118 )
119​
120 await writeFile(
121 resolve(consumerDirectory, '.npmrc'),
122 'package-lock=false\n',
123 'utf8',
124 )
126​
127async function installConsumer(consumerDirectory, tarballPath) {
128 await execFile(npmCommand, ['install', '--silent', tarballPath], {
129 cwd: consumerDirectory,
130 })
132​
133async function typecheckConsumer(consumerDirectory) {
134 await execFile(process.execPath, [tscEntrypoint, '-p', 'tsconfig.json'], {
135 cwd: consumerDirectory,
136 })