import { access, readdir, writeFile } from 'node:fs/promises'; import { resolve } from 'node:path'; import { spawnSync } from 'node:child_process'; let declarations = process.argv[2] || process.env.COCOS_TYPES; if (!declarations) { const root = '/Applications/Cocos/Creator'; try { for (const version of (await readdir(root)).sort((a, b) => b.localeCompare(a, undefined, { numeric: true }))) { const candidate = `${root}/${version}/CocosCreator.app/Contents/Resources/resources/3d/engine/bin/.declarations/cc.d.ts`; try { await access(candidate); declarations = candidate; break; } catch {} } } catch {} } if (!declarations) throw new Error('请用 COCOS_TYPES 或命令参数提供 Creator 的 cc.d.ts 路径。'); const target = resolve('cocos/export/02-connect-1234'); const config = resolve('cocos/export/tsconfig.check.json'); await writeFile(config, JSON.stringify({ compilerOptions: { target: 'ES2020', module: 'ESNext', moduleResolution: 'Bundler', lib: ['ES2020', 'DOM'], strict: true, experimentalDecorators: true, useDefineForClassFields: false, noEmit: true, skipLibCheck: true, types: [], baseUrl: '.', paths: { cc: [resolve(declarations)] } }, include: [target] }, null, 2)); console.log(`检查真实 Creator 类型:${declarations}`); const result = spawnSync(process.execPath, ['node_modules/typescript/bin/tsc', '-p', config], { stdio: 'inherit' }); process.exit(result.status ?? 1);