27 lines
842 B
JavaScript
27 lines
842 B
JavaScript
'use strict';
|
|
/**
|
|
* 统一 Claude 根目录检测 (P3-4)
|
|
* 所有 hooks 共享此模块,消除 13+ 处重复定义
|
|
*/
|
|
const path = require('path');
|
|
const fs = require('fs');
|
|
|
|
let _cached = null;
|
|
function detectClaudeRoot() {
|
|
if (_cached) return _cached;
|
|
// 优先级: 环境变量 > 自身路径推断 > paths.config > 默认回退
|
|
if (process.env.CLAUDE_HOME) { _cached = process.env.CLAUDE_HOME; return _cached; }
|
|
const selfDir = path.dirname(__filename);
|
|
if (selfDir.includes('.claude')) {
|
|
_cached = selfDir.replace(/[\/\\]hooks[\/\\]lib$/, '');
|
|
return _cached;
|
|
}
|
|
try { _cached = require('../../scripts/paths.config.js').PATHS.root; return _cached; }
|
|
catch {
|
|
_cached = path.join(process.env.USERPROFILE || process.env.HOME || '', '.claude');
|
|
return _cached;
|
|
}
|
|
}
|
|
|
|
module.exports = detectClaudeRoot();
|