perf(hooks): use direct require() instead of spawning child process

Invoke hook scripts directly via require() when they export a
run(rawInput) function, eliminating one Node.js process spawn per
hook invocation (~50-100ms).

Includes path traversal guard, timeouts, error logging, PR review
feedback, legacy hooks guard, normalized filePath, and restored
findProjectRoot config detection with package manager support.
This commit is contained in:
Jonghyeok Park
2026-03-08 16:53:20 +09:00
parent e5d02000c3
commit 66498ae9ac
6 changed files with 228 additions and 63 deletions

View File

@@ -18,9 +18,29 @@ const binCache = new Map();
// ── Public helpers ──────────────────────────────────────────────────
// Markers that indicate a project root (formatter configs included so
// repos without package.json are still detected correctly).
const PROJECT_ROOT_MARKERS = [
'package.json',
'biome.json',
'biome.jsonc',
'.prettierrc',
'.prettierrc.json',
'.prettierrc.js',
'.prettierrc.cjs',
'.prettierrc.mjs',
'.prettierrc.yml',
'.prettierrc.yaml',
'.prettierrc.toml',
'prettier.config.js',
'prettier.config.cjs',
'prettier.config.mjs'
];
/**
* Walk up from `startDir` until a directory containing package.json is found.
* Returns `startDir` as fallback when no package.json exists above it.
* Walk up from `startDir` until a directory containing a known project
* root marker (package.json or formatter config) is found.
* Returns `startDir` as fallback when no marker exists above it.
*
* @param {string} startDir - Absolute directory path to start from
* @returns {string} Absolute path to the project root
@@ -30,9 +50,11 @@ function findProjectRoot(startDir) {
let dir = startDir;
while (dir !== path.dirname(dir)) {
if (fs.existsSync(path.join(dir, 'package.json'))) {
projectRootCache.set(startDir, dir);
return dir;
for (const marker of PROJECT_ROOT_MARKERS) {
if (fs.existsSync(path.join(dir, marker))) {
projectRootCache.set(startDir, dir);
return dir;
}
}
dir = path.dirname(dir);
}
@@ -59,6 +81,20 @@ function detectFormatter(projectRoot) {
}
}
// Check package.json "prettier" key before config files
try {
const pkgPath = path.join(projectRoot, 'package.json');
if (fs.existsSync(pkgPath)) {
const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf8'));
if (pkg.prettier != null) {
formatterCache.set(projectRoot, 'prettier');
return 'prettier';
}
}
} catch {
// Malformed package.json — continue to file-based detection
}
const prettierConfigs = [
'.prettierrc',
'.prettierrc.json',
@@ -70,7 +106,7 @@ function detectFormatter(projectRoot) {
'.prettierrc.toml',
'prettier.config.js',
'prettier.config.cjs',
'prettier.config.mjs',
'prettier.config.mjs'
];
for (const cfg of prettierConfigs) {
if (fs.existsSync(path.join(projectRoot, cfg))) {
@@ -83,14 +119,35 @@ function detectFormatter(projectRoot) {
return null;
}
/**
* Resolve the runner binary and prefix args for the configured package
* manager (respects CLAUDE_PACKAGE_MANAGER env and project config).
*
* @param {string} projectRoot - Absolute path to the project root
* @returns {{ bin: string, prefix: string[] }}
*/
// Windows .cmd shim mapping for cross-platform safety
const WIN_CMD_SHIMS = { npx: 'npx.cmd', pnpm: 'pnpm.cmd', yarn: 'yarn.cmd', bunx: 'bunx.cmd' };
function getRunnerFromPackageManager(projectRoot) {
const isWin = process.platform === 'win32';
const { getPackageManager } = require('./package-manager');
const pm = getPackageManager({ projectDir: projectRoot });
const execCmd = pm?.config?.execCmd || 'npx';
const [rawBin = 'npx', ...prefix] = execCmd.split(/\s+/).filter(Boolean);
const bin = isWin ? WIN_CMD_SHIMS[rawBin] || rawBin : rawBin;
return { bin, prefix };
}
/**
* Resolve the formatter binary, preferring the local node_modules/.bin
* installation over npx to avoid package-resolution overhead.
* installation over the package manager exec command to avoid
* package-resolution overhead.
*
* @param {string} projectRoot - Absolute path to the project root
* @param {'biome' | 'prettier'} formatter - Detected formatter name
* @returns {{ bin: string, prefix: string[] }}
* `bin` executable path (absolute local path or npx/npx.cmd)
* @returns {{ bin: string, prefix: string[] } | null}
* `bin` executable path (absolute local path or runner binary)
* `prefix` extra args to prepend (e.g. ['@biomejs/biome'] when using npx)
*/
function resolveFormatterBin(projectRoot, formatter) {
@@ -98,45 +155,36 @@ function resolveFormatterBin(projectRoot, formatter) {
if (binCache.has(cacheKey)) return binCache.get(cacheKey);
const isWin = process.platform === 'win32';
const npxBin = isWin ? 'npx.cmd' : 'npx';
if (formatter === 'biome') {
const localBin = path.join(
projectRoot,
'node_modules',
'.bin',
isWin ? 'biome.cmd' : 'biome',
);
const localBin = path.join(projectRoot, 'node_modules', '.bin', isWin ? 'biome.cmd' : 'biome');
if (fs.existsSync(localBin)) {
const result = { bin: localBin, prefix: [] };
binCache.set(cacheKey, result);
return result;
}
const result = { bin: npxBin, prefix: ['@biomejs/biome'] };
const runner = getRunnerFromPackageManager(projectRoot);
const result = { bin: runner.bin, prefix: [...runner.prefix, '@biomejs/biome'] };
binCache.set(cacheKey, result);
return result;
}
if (formatter === 'prettier') {
const localBin = path.join(
projectRoot,
'node_modules',
'.bin',
isWin ? 'prettier.cmd' : 'prettier',
);
const localBin = path.join(projectRoot, 'node_modules', '.bin', isWin ? 'prettier.cmd' : 'prettier');
if (fs.existsSync(localBin)) {
const result = { bin: localBin, prefix: [] };
binCache.set(cacheKey, result);
return result;
}
const result = { bin: npxBin, prefix: ['prettier'] };
const runner = getRunnerFromPackageManager(projectRoot);
const result = { bin: runner.bin, prefix: [...runner.prefix, 'prettier'] };
binCache.set(cacheKey, result);
return result;
}
const result = { bin: npxBin, prefix: [] };
binCache.set(cacheKey, result);
return result;
// Unknown formatter — return null so callers can handle gracefully
binCache.set(cacheKey, null);
return null;
}
/**
@@ -152,5 +200,5 @@ module.exports = {
findProjectRoot,
detectFormatter,
resolveFormatterBin,
clearCaches,
};
clearCaches
};