#!/usr/bin/env node import { spawnSync } from 'node:child_process'; import { existsSync, mkdirSync } from 'node:fs'; import { dirname, resolve } from 'node:path'; import { fileURLToPath } from 'node:url'; const scriptDir = dirname(fileURLToPath(import.meta.url)); const repoRoot = resolve(scriptDir, '..', '..'); const logDir = resolve(repoRoot, '.codex', 'logs'); const hasCodegraphConfig = existsSync(resolve(repoRoot, '.codegraph', 'config.json')); const npmCommand = process.platform === 'win32' ? 'cmd' : 'npm'; if (!hasCodegraphConfig) { console.log('[codex-hook] 未发现 .codegraph/config.json,跳过 CodeGraph 同步。'); process.exit(0); } const result = spawnSync(npmCommand, process.platform === 'win32' ? ['/d', '/s', '/c', 'npm run codegraph:sync'] : ['run', 'codegraph:sync'], { cwd: repoRoot, shell: false, encoding: 'utf8', env: { ...process.env, NO_COLOR: process.env.NO_COLOR ?? '1', }, }); mkdirSync(logDir, { recursive: true }); if (result.stdout) { process.stdout.write(result.stdout); } if (result.stderr) { process.stderr.write(result.stderr); } if (result.error) { console.error(`[codex-hook] CodeGraph 同步启动失败:${result.error.message}`); process.exit(1); } if (result.signal) { console.error(`[codex-hook] CodeGraph 同步被信号终止:${result.signal}`); process.exit(1); } if ((result.status ?? 0) !== 0) { console.error('[codex-hook] CodeGraph 同步失败,请手动运行 npm run codegraph:sync 查看详情。'); process.exit(result.status ?? 1); } console.log('[codex-hook] CodeGraph 已同步。');