run-next-typegen.mjs
1 #!/usr/bin/env node 2 3 import fs from 'node:fs' 4 import path from 'node:path' 5 import { spawnSync } from 'node:child_process' 6 import { createRequire } from 'node:module' 7 import { pathToFileURL } from 'node:url' 8 9 import { ensureBuildBootstrapPaths } from './build-bootstrap-env.mjs' 10 11 const require = createRequire(import.meta.url) 12 13 export const TYPEGEN_ARTIFACT_PATHS = [ 14 '.next/types', 15 '.next/dev/types', 16 'tsconfig.tsbuildinfo', 17 ] 18 19 export function cleanupTypegenArtifacts(cwd = process.cwd()) { 20 for (const relativePath of TYPEGEN_ARTIFACT_PATHS) { 21 fs.rmSync(path.join(cwd, relativePath), { recursive: true, force: true }) 22 } 23 } 24 25 export function buildNextTypegenEnv(env = process.env, cwd = process.cwd()) { 26 const bootstrapPaths = ensureBuildBootstrapPaths(cwd) 27 return { 28 ...env, 29 DATA_DIR: bootstrapPaths.dataDir, 30 WORKSPACE_DIR: bootstrapPaths.workspaceDir, 31 BROWSER_PROFILES_DIR: bootstrapPaths.browserProfilesDir, 32 SWARMCLAW_BUILD_MODE: env.SWARMCLAW_BUILD_MODE || '1', 33 } 34 } 35 36 export function runNextTypegen(args = process.argv.slice(2), env = process.env, cwd = process.cwd()) { 37 cleanupTypegenArtifacts(cwd) 38 const nextBin = require.resolve('next/dist/bin/next') 39 return spawnSync(process.execPath, [nextBin, 'typegen', ...args], { 40 stdio: 'inherit', 41 env: buildNextTypegenEnv(env, cwd), 42 cwd, 43 }) 44 } 45 46 function main() { 47 const result = runNextTypegen() 48 if (result.error) throw result.error 49 if (typeof result.status === 'number') { 50 process.exit(result.status) 51 } 52 if (result.signal) { 53 process.kill(process.pid, result.signal) 54 return 55 } 56 process.exit(1) 57 } 58 59 if (process.argv[1] && pathToFileURL(process.argv[1]).href === import.meta.url) { 60 main() 61 }