const fs = require('fs/promises') const path = require('path') const { spawn } = require('child_process') const repoRoot = process.cwd() const nextBin = path.join(repoRoot, 'node_modules', 'next', 'dist', 'bin', 'next') const tscBin = path.join(repoRoot, 'node_modules', 'typescript', 'bin', 'tsc') const tsconfigPath = path.join(repoRoot, 'tsconfig.json') function run(command, args) { return new Promise((resolve, reject) => { const child = spawn(command, args, { cwd: repoRoot, stdio: 'inherit', shell: false, }) child.on('exit', (code) => { if (code === 0) { resolve() return } reject(new Error(`${command} ${args.join(' ')} exited with code ${code}`)) }) }) } async function ensureNextCacheLifeStub() { const candidatePaths = [ path.join(repoRoot, '.next', 'types', 'cache-life.d.ts'), path.join(repoRoot, '.next', 'dev', 'types', 'cache-life.d.ts'), ] for (const cacheLifePath of candidatePaths) { await fs.mkdir(path.dirname(cacheLifePath), { recursive: true }) try { await fs.access(cacheLifePath) } catch { await fs.writeFile(cacheLifePath, 'export {}\n', 'utf8') } } } async function main() { await run(process.execPath, [nextBin, 'typegen']) await ensureNextCacheLifeStub() await run(process.execPath, [tscBin, '-p', tsconfigPath, '--noEmit', '--incremental', 'false']) } main().catch((error) => { console.error(error) process.exitCode = 1 })