chore: add codex workspace hooks

This commit is contained in:
kdletters
2026-05-21 20:21:32 +08:00
parent fda916ac63
commit 321e1ea33a
5 changed files with 211 additions and 2 deletions

View File

@@ -0,0 +1,51 @@
#!/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 已同步。');