mirror of
https://github.com/affaan-m/everything-claude-code.git
synced 2026-03-30 21:53:28 +08:00
Merge pull request #359 from pythonstrup/feat/optimize-biome-hooks
perf(hooks): optimize formatter hooks(x52 faster) — local binary, merged invocations, direct require()
This commit is contained in:
@@ -7,158 +7,74 @@
|
||||
* Runs after Edit tool use. If the edited file is a JS/TS file,
|
||||
* auto-detects the project formatter (Biome or Prettier) by looking
|
||||
* for config files, then formats accordingly.
|
||||
*
|
||||
* For Biome, uses `check --write` (format + lint in one pass) to
|
||||
* avoid a redundant second invocation from quality-gate.js.
|
||||
*
|
||||
* Prefers the local node_modules/.bin binary over npx to skip
|
||||
* package-resolution overhead (~200-500ms savings per invocation).
|
||||
*
|
||||
* Fails silently if no formatter is found or installed.
|
||||
*/
|
||||
|
||||
const { execFileSync, spawnSync } = require('child_process');
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const { getPackageManager } = require('../lib/package-manager');
|
||||
|
||||
// Shell metacharacters that cmd.exe interprets as command separators/operators
|
||||
const UNSAFE_PATH_CHARS = /[&|<>^%!]/;
|
||||
|
||||
const { findProjectRoot, detectFormatter, resolveFormatterBin } = require('../lib/resolve-formatter');
|
||||
|
||||
const MAX_STDIN = 1024 * 1024; // 1MB limit
|
||||
const BIOME_CONFIGS = ['biome.json', 'biome.jsonc'];
|
||||
const PRETTIER_CONFIGS = [
|
||||
'.prettierrc',
|
||||
'.prettierrc.json',
|
||||
'.prettierrc.json5',
|
||||
'.prettierrc.js',
|
||||
'.prettierrc.cjs',
|
||||
'.prettierrc.mjs',
|
||||
'.prettierrc.ts',
|
||||
'.prettierrc.cts',
|
||||
'.prettierrc.mts',
|
||||
'.prettierrc.yml',
|
||||
'.prettierrc.yaml',
|
||||
'.prettierrc.toml',
|
||||
'prettier.config.js',
|
||||
'prettier.config.cjs',
|
||||
'prettier.config.mjs',
|
||||
'prettier.config.ts',
|
||||
'prettier.config.cts',
|
||||
'prettier.config.mts',
|
||||
];
|
||||
const PROJECT_ROOT_MARKERS = ['package.json', ...BIOME_CONFIGS, ...PRETTIER_CONFIGS];
|
||||
let data = '';
|
||||
process.stdin.setEncoding('utf8');
|
||||
|
||||
process.stdin.on('data', chunk => {
|
||||
if (data.length < MAX_STDIN) {
|
||||
const remaining = MAX_STDIN - data.length;
|
||||
data += chunk.substring(0, remaining);
|
||||
}
|
||||
});
|
||||
|
||||
function findProjectRoot(startDir) {
|
||||
let dir = startDir;
|
||||
let fallbackDir = null;
|
||||
|
||||
while (true) {
|
||||
if (detectFormatter(dir)) {
|
||||
return dir;
|
||||
}
|
||||
|
||||
if (!fallbackDir && PROJECT_ROOT_MARKERS.some(marker => fs.existsSync(path.join(dir, marker)))) {
|
||||
fallbackDir = dir;
|
||||
}
|
||||
|
||||
const parentDir = path.dirname(dir);
|
||||
if (parentDir === dir) break;
|
||||
dir = parentDir;
|
||||
}
|
||||
|
||||
return fallbackDir || startDir;
|
||||
}
|
||||
|
||||
function detectFormatter(projectRoot) {
|
||||
for (const cfg of BIOME_CONFIGS) {
|
||||
if (fs.existsSync(path.join(projectRoot, cfg))) return 'biome';
|
||||
}
|
||||
|
||||
for (const cfg of PRETTIER_CONFIGS) {
|
||||
if (fs.existsSync(path.join(projectRoot, cfg))) return 'prettier';
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
function getRunnerBin(bin) {
|
||||
if (process.platform !== 'win32') return bin;
|
||||
if (bin === 'npx') return 'npx.cmd';
|
||||
if (bin === 'pnpm') return 'pnpm.cmd';
|
||||
if (bin === 'yarn') return 'yarn.cmd';
|
||||
if (bin === 'bunx') return 'bunx.cmd';
|
||||
return bin;
|
||||
}
|
||||
|
||||
function getFormatterRunner(projectRoot) {
|
||||
const pm = getPackageManager({ projectDir: projectRoot });
|
||||
const execCmd = pm?.config?.execCmd || 'npx';
|
||||
const [bin = 'npx', ...prefix] = execCmd.split(/\s+/).filter(Boolean);
|
||||
|
||||
return {
|
||||
bin: getRunnerBin(bin),
|
||||
prefix
|
||||
};
|
||||
}
|
||||
|
||||
function getFormatterCommand(formatter, filePath, projectRoot) {
|
||||
const runner = getFormatterRunner(projectRoot);
|
||||
|
||||
if (formatter === 'biome') {
|
||||
return {
|
||||
bin: runner.bin,
|
||||
args: [...runner.prefix, '@biomejs/biome', 'format', '--write', filePath]
|
||||
};
|
||||
}
|
||||
if (formatter === 'prettier') {
|
||||
return {
|
||||
bin: runner.bin,
|
||||
args: [...runner.prefix, 'prettier', '--write', filePath]
|
||||
};
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
function runFormatterCommand(cmd, projectRoot) {
|
||||
if (process.platform === 'win32' && cmd.bin.endsWith('.cmd')) {
|
||||
const result = spawnSync(cmd.bin, cmd.args, {
|
||||
cwd: projectRoot,
|
||||
shell: true,
|
||||
stdio: 'pipe',
|
||||
timeout: 15000
|
||||
});
|
||||
|
||||
if (result.error) {
|
||||
throw result.error;
|
||||
}
|
||||
|
||||
if (typeof result.status === 'number' && result.status !== 0) {
|
||||
throw new Error(result.stderr?.toString() || `Formatter exited with status ${result.status}`);
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
execFileSync(cmd.bin, cmd.args, {
|
||||
cwd: projectRoot,
|
||||
stdio: ['pipe', 'pipe', 'pipe'],
|
||||
timeout: 15000
|
||||
});
|
||||
}
|
||||
|
||||
process.stdin.on('end', () => {
|
||||
/**
|
||||
* Core logic — exported so run-with-flags.js can call directly
|
||||
* without spawning a child process.
|
||||
*
|
||||
* @param {string} rawInput - Raw JSON string from stdin
|
||||
* @returns {string} The original input (pass-through)
|
||||
*/
|
||||
function run(rawInput) {
|
||||
try {
|
||||
const input = JSON.parse(data);
|
||||
const input = JSON.parse(rawInput);
|
||||
const filePath = input.tool_input?.file_path;
|
||||
|
||||
if (filePath && /\.(ts|tsx|js|jsx)$/.test(filePath)) {
|
||||
try {
|
||||
const projectRoot = findProjectRoot(path.dirname(path.resolve(filePath)));
|
||||
const resolvedFilePath = path.resolve(filePath);
|
||||
const projectRoot = findProjectRoot(path.dirname(resolvedFilePath));
|
||||
const formatter = detectFormatter(projectRoot);
|
||||
const cmd = getFormatterCommand(formatter, filePath, projectRoot);
|
||||
if (!formatter) return rawInput;
|
||||
|
||||
if (cmd) {
|
||||
runFormatterCommand(cmd, projectRoot);
|
||||
const resolved = resolveFormatterBin(projectRoot, formatter);
|
||||
if (!resolved) return rawInput;
|
||||
|
||||
// Biome: `check --write` = format + lint in one pass
|
||||
// Prettier: `--write` = format only
|
||||
const args = formatter === 'biome' ? [...resolved.prefix, 'check', '--write', resolvedFilePath] : [...resolved.prefix, '--write', resolvedFilePath];
|
||||
|
||||
if (process.platform === 'win32' && resolved.bin.endsWith('.cmd')) {
|
||||
// Windows: .cmd files require shell to execute. Guard against
|
||||
// command injection by rejecting paths with shell metacharacters.
|
||||
if (UNSAFE_PATH_CHARS.test(resolvedFilePath)) {
|
||||
throw new Error('File path contains unsafe shell characters');
|
||||
}
|
||||
const result = spawnSync(resolved.bin, args, {
|
||||
cwd: projectRoot,
|
||||
shell: true,
|
||||
stdio: 'pipe',
|
||||
timeout: 15000
|
||||
});
|
||||
if (result.error) throw result.error;
|
||||
if (typeof result.status === 'number' && result.status !== 0) {
|
||||
throw new Error(result.stderr?.toString() || `Formatter exited with status ${result.status}`);
|
||||
}
|
||||
} else {
|
||||
execFileSync(resolved.bin, args, {
|
||||
cwd: projectRoot,
|
||||
stdio: ['pipe', 'pipe', 'pipe'],
|
||||
timeout: 15000
|
||||
});
|
||||
}
|
||||
} catch {
|
||||
// Formatter not installed, file missing, or failed — non-blocking
|
||||
@@ -168,6 +84,26 @@ process.stdin.on('end', () => {
|
||||
// Invalid input — pass through
|
||||
}
|
||||
|
||||
process.stdout.write(data);
|
||||
process.exit(0);
|
||||
});
|
||||
return rawInput;
|
||||
}
|
||||
|
||||
// ── stdin entry point (backwards-compatible) ────────────────────
|
||||
if (require.main === module) {
|
||||
let data = '';
|
||||
process.stdin.setEncoding('utf8');
|
||||
|
||||
process.stdin.on('data', chunk => {
|
||||
if (data.length < MAX_STDIN) {
|
||||
const remaining = MAX_STDIN - data.length;
|
||||
data += chunk.substring(0, remaining);
|
||||
}
|
||||
});
|
||||
|
||||
process.stdin.on('end', () => {
|
||||
data = run(data);
|
||||
process.stdout.write(data);
|
||||
process.exit(0);
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = { run };
|
||||
|
||||
@@ -5,6 +5,11 @@
|
||||
* Runs lightweight quality checks after file edits.
|
||||
* - Targets one file when file_path is provided
|
||||
* - Falls back to no-op when language/tooling is unavailable
|
||||
*
|
||||
* For JS/TS files with Biome, this hook is skipped because
|
||||
* post-edit-format.js already runs `biome check --write`.
|
||||
* This hook still handles .json/.md files for Biome, and all
|
||||
* Prettier / Go / Python checks.
|
||||
*/
|
||||
|
||||
'use strict';
|
||||
@@ -13,56 +18,105 @@ const fs = require('fs');
|
||||
const path = require('path');
|
||||
const { spawnSync } = require('child_process');
|
||||
|
||||
const MAX_STDIN = 1024 * 1024;
|
||||
let raw = '';
|
||||
const { findProjectRoot, detectFormatter, resolveFormatterBin } = require('../lib/resolve-formatter');
|
||||
|
||||
function run(command, args, cwd = process.cwd()) {
|
||||
const MAX_STDIN = 1024 * 1024;
|
||||
|
||||
/**
|
||||
* Execute a command synchronously, returning the spawnSync result.
|
||||
*
|
||||
* @param {string} command - Executable path or name
|
||||
* @param {string[]} args - Arguments to pass
|
||||
* @param {string} [cwd] - Working directory (defaults to process.cwd())
|
||||
* @returns {import('child_process').SpawnSyncReturns<string>}
|
||||
*/
|
||||
function exec(command, args, cwd = process.cwd()) {
|
||||
return spawnSync(command, args, {
|
||||
cwd,
|
||||
encoding: 'utf8',
|
||||
env: process.env,
|
||||
timeout: 15000
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Write a message to stderr for logging.
|
||||
*
|
||||
* @param {string} msg - Message to log
|
||||
*/
|
||||
function log(msg) {
|
||||
process.stderr.write(`${msg}\n`);
|
||||
}
|
||||
|
||||
/**
|
||||
* Run quality-gate checks for a single file based on its extension.
|
||||
* Skips JS/TS files when Biome is configured (handled by post-edit-format).
|
||||
*
|
||||
* @param {string} filePath - Path to the edited file
|
||||
*/
|
||||
function maybeRunQualityGate(filePath) {
|
||||
if (!filePath || !fs.existsSync(filePath)) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Resolve to absolute path so projectRoot-relative comparisons work
|
||||
filePath = path.resolve(filePath);
|
||||
|
||||
const ext = path.extname(filePath).toLowerCase();
|
||||
const fix = String(process.env.ECC_QUALITY_GATE_FIX || '').toLowerCase() === 'true';
|
||||
const strict = String(process.env.ECC_QUALITY_GATE_STRICT || '').toLowerCase() === 'true';
|
||||
|
||||
if (['.ts', '.tsx', '.js', '.jsx', '.json', '.md'].includes(ext)) {
|
||||
// Prefer biome if present
|
||||
if (fs.existsSync(path.join(process.cwd(), 'biome.json')) || fs.existsSync(path.join(process.cwd(), 'biome.jsonc'))) {
|
||||
const args = ['biome', 'check', filePath];
|
||||
const projectRoot = findProjectRoot(path.dirname(filePath));
|
||||
const formatter = detectFormatter(projectRoot);
|
||||
|
||||
if (formatter === 'biome') {
|
||||
// JS/TS already handled by post-edit-format via `biome check --write`
|
||||
if (['.ts', '.tsx', '.js', '.jsx'].includes(ext)) {
|
||||
return;
|
||||
}
|
||||
|
||||
// .json / .md — still need quality gate
|
||||
const resolved = resolveFormatterBin(projectRoot, 'biome');
|
||||
if (!resolved) return;
|
||||
const args = [...resolved.prefix, 'check', filePath];
|
||||
if (fix) args.push('--write');
|
||||
const result = run('npx', args);
|
||||
const result = exec(resolved.bin, args, projectRoot);
|
||||
if (result.status !== 0 && strict) {
|
||||
log(`[QualityGate] Biome check failed for ${filePath}`);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// Fallback to prettier when installed
|
||||
const prettierArgs = ['prettier', '--check', filePath];
|
||||
if (fix) {
|
||||
prettierArgs[1] = '--write';
|
||||
}
|
||||
const prettier = run('npx', prettierArgs);
|
||||
if (prettier.status !== 0 && strict) {
|
||||
log(`[QualityGate] Prettier check failed for ${filePath}`);
|
||||
if (formatter === 'prettier') {
|
||||
const resolved = resolveFormatterBin(projectRoot, 'prettier');
|
||||
if (!resolved) return;
|
||||
const args = [...resolved.prefix, fix ? '--write' : '--check', filePath];
|
||||
const result = exec(resolved.bin, args, projectRoot);
|
||||
if (result.status !== 0 && strict) {
|
||||
log(`[QualityGate] Prettier check failed for ${filePath}`);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// No formatter configured — skip
|
||||
return;
|
||||
}
|
||||
|
||||
if (ext === '.go' && fix) {
|
||||
run('gofmt', ['-w', filePath]);
|
||||
if (ext === '.go') {
|
||||
if (fix) {
|
||||
const r = exec('gofmt', ['-w', filePath]);
|
||||
if (r.status !== 0 && strict) {
|
||||
log(`[QualityGate] gofmt failed for ${filePath}`);
|
||||
}
|
||||
} else if (strict) {
|
||||
const r = exec('gofmt', ['-l', filePath]);
|
||||
if (r.status !== 0) {
|
||||
log(`[QualityGate] gofmt failed for ${filePath}`);
|
||||
} else if (r.stdout && r.stdout.trim()) {
|
||||
log(`[QualityGate] gofmt check failed for ${filePath}`);
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -70,29 +124,45 @@ function maybeRunQualityGate(filePath) {
|
||||
const args = ['format'];
|
||||
if (!fix) args.push('--check');
|
||||
args.push(filePath);
|
||||
const r = run('ruff', args);
|
||||
const r = exec('ruff', args);
|
||||
if (r.status !== 0 && strict) {
|
||||
log(`[QualityGate] Ruff check failed for ${filePath}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
process.stdin.setEncoding('utf8');
|
||||
process.stdin.on('data', chunk => {
|
||||
if (raw.length < MAX_STDIN) {
|
||||
const remaining = MAX_STDIN - raw.length;
|
||||
raw += chunk.substring(0, remaining);
|
||||
}
|
||||
});
|
||||
|
||||
process.stdin.on('end', () => {
|
||||
/**
|
||||
* Core logic — exported so run-with-flags.js can call directly.
|
||||
*
|
||||
* @param {string} rawInput - Raw JSON string from stdin
|
||||
* @returns {string} The original input (pass-through)
|
||||
*/
|
||||
function run(rawInput) {
|
||||
try {
|
||||
const input = JSON.parse(raw);
|
||||
const input = JSON.parse(rawInput);
|
||||
const filePath = String(input.tool_input?.file_path || '');
|
||||
maybeRunQualityGate(filePath);
|
||||
} catch {
|
||||
// Ignore parse errors.
|
||||
}
|
||||
return rawInput;
|
||||
}
|
||||
|
||||
process.stdout.write(raw);
|
||||
});
|
||||
// ── stdin entry point (backwards-compatible) ────────────────────
|
||||
if (require.main === module) {
|
||||
let raw = '';
|
||||
process.stdin.setEncoding('utf8');
|
||||
process.stdin.on('data', chunk => {
|
||||
if (raw.length < MAX_STDIN) {
|
||||
const remaining = MAX_STDIN - raw.length;
|
||||
raw += chunk.substring(0, remaining);
|
||||
}
|
||||
});
|
||||
|
||||
process.stdin.on('end', () => {
|
||||
const result = run(raw);
|
||||
process.stdout.write(result);
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = { run };
|
||||
|
||||
@@ -52,7 +52,15 @@ async function main() {
|
||||
}
|
||||
|
||||
const pluginRoot = getPluginRoot();
|
||||
const scriptPath = path.join(pluginRoot, relScriptPath);
|
||||
const resolvedRoot = path.resolve(pluginRoot);
|
||||
const scriptPath = path.resolve(pluginRoot, relScriptPath);
|
||||
|
||||
// Prevent path traversal outside the plugin root
|
||||
if (!scriptPath.startsWith(resolvedRoot + path.sep)) {
|
||||
process.stderr.write(`[Hook] Path traversal rejected for ${hookId}: ${scriptPath}\n`);
|
||||
process.stdout.write(raw);
|
||||
process.exit(0);
|
||||
}
|
||||
|
||||
if (!fs.existsSync(scriptPath)) {
|
||||
process.stderr.write(`[Hook] Script not found for ${hookId}: ${scriptPath}\n`);
|
||||
@@ -60,11 +68,43 @@ async function main() {
|
||||
process.exit(0);
|
||||
}
|
||||
|
||||
// Prefer direct require() when the hook exports a run(rawInput) function.
|
||||
// This eliminates one Node.js process spawn (~50-100ms savings per hook).
|
||||
//
|
||||
// SAFETY: Only require() hooks that export run(). Legacy hooks execute
|
||||
// side effects at module scope (stdin listeners, process.exit, main() calls)
|
||||
// which would interfere with the parent process or cause double execution.
|
||||
let hookModule;
|
||||
const src = fs.readFileSync(scriptPath, 'utf8');
|
||||
const hasRunExport = /\bmodule\.exports\b/.test(src) && /\brun\b/.test(src);
|
||||
|
||||
if (hasRunExport) {
|
||||
try {
|
||||
hookModule = require(scriptPath);
|
||||
} catch (requireErr) {
|
||||
process.stderr.write(`[Hook] require() failed for ${hookId}: ${requireErr.message}\n`);
|
||||
// Fall through to legacy spawnSync path
|
||||
}
|
||||
}
|
||||
|
||||
if (hookModule && typeof hookModule.run === 'function') {
|
||||
try {
|
||||
const output = hookModule.run(raw);
|
||||
if (output !== null && output !== undefined) process.stdout.write(output);
|
||||
} catch (runErr) {
|
||||
process.stderr.write(`[Hook] run() error for ${hookId}: ${runErr.message}\n`);
|
||||
process.stdout.write(raw);
|
||||
}
|
||||
process.exit(0);
|
||||
}
|
||||
|
||||
// Legacy path: spawn a child Node process for hooks without run() export
|
||||
const result = spawnSync('node', [scriptPath], {
|
||||
input: raw,
|
||||
encoding: 'utf8',
|
||||
env: process.env,
|
||||
cwd: process.cwd(),
|
||||
timeout: 30000
|
||||
});
|
||||
|
||||
if (result.stdout) process.stdout.write(result.stdout);
|
||||
|
||||
185
scripts/lib/resolve-formatter.js
Normal file
185
scripts/lib/resolve-formatter.js
Normal file
@@ -0,0 +1,185 @@
|
||||
/**
|
||||
* Shared formatter resolution utilities with caching.
|
||||
*
|
||||
* Extracts project-root discovery, formatter detection, and binary
|
||||
* resolution into a single module so that post-edit-format.js and
|
||||
* quality-gate.js avoid duplicating work and filesystem lookups.
|
||||
*/
|
||||
|
||||
'use strict';
|
||||
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
|
||||
// ── Caches (per-process, cleared on next hook invocation) ───────────
|
||||
const projectRootCache = new Map();
|
||||
const formatterCache = new Map();
|
||||
const binCache = new Map();
|
||||
|
||||
// ── Config file lists (single source of truth) ─────────────────────
|
||||
|
||||
const BIOME_CONFIGS = ['biome.json', 'biome.jsonc'];
|
||||
|
||||
const PRETTIER_CONFIGS = [
|
||||
'.prettierrc',
|
||||
'.prettierrc.json',
|
||||
'.prettierrc.js',
|
||||
'.prettierrc.cjs',
|
||||
'.prettierrc.mjs',
|
||||
'.prettierrc.yml',
|
||||
'.prettierrc.yaml',
|
||||
'.prettierrc.toml',
|
||||
'prettier.config.js',
|
||||
'prettier.config.cjs',
|
||||
'prettier.config.mjs'
|
||||
];
|
||||
|
||||
const PROJECT_ROOT_MARKERS = ['package.json', ...BIOME_CONFIGS, ...PRETTIER_CONFIGS];
|
||||
|
||||
// ── Windows .cmd shim mapping ───────────────────────────────────────
|
||||
const WIN_CMD_SHIMS = { npx: 'npx.cmd', pnpm: 'pnpm.cmd', yarn: 'yarn.cmd', bunx: 'bunx.cmd' };
|
||||
|
||||
// ── Formatter → package name mapping ────────────────────────────────
|
||||
const FORMATTER_PACKAGES = {
|
||||
biome: { binName: 'biome', pkgName: '@biomejs/biome' },
|
||||
prettier: { binName: 'prettier', pkgName: 'prettier' }
|
||||
};
|
||||
|
||||
// ── Public helpers ──────────────────────────────────────────────────
|
||||
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
function findProjectRoot(startDir) {
|
||||
if (projectRootCache.has(startDir)) return projectRootCache.get(startDir);
|
||||
|
||||
let dir = startDir;
|
||||
while (dir !== path.dirname(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);
|
||||
}
|
||||
|
||||
projectRootCache.set(startDir, startDir);
|
||||
return startDir;
|
||||
}
|
||||
|
||||
/**
|
||||
* Detect the formatter configured in the project.
|
||||
* Biome takes priority over Prettier.
|
||||
*
|
||||
* @param {string} projectRoot - Absolute path to the project root
|
||||
* @returns {'biome' | 'prettier' | null}
|
||||
*/
|
||||
function detectFormatter(projectRoot) {
|
||||
if (formatterCache.has(projectRoot)) return formatterCache.get(projectRoot);
|
||||
|
||||
for (const cfg of BIOME_CONFIGS) {
|
||||
if (fs.existsSync(path.join(projectRoot, cfg))) {
|
||||
formatterCache.set(projectRoot, 'biome');
|
||||
return 'biome';
|
||||
}
|
||||
}
|
||||
|
||||
// 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 ('prettier' in pkg) {
|
||||
formatterCache.set(projectRoot, 'prettier');
|
||||
return 'prettier';
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
// Malformed package.json — continue to file-based detection
|
||||
}
|
||||
|
||||
for (const cfg of PRETTIER_CONFIGS) {
|
||||
if (fs.existsSync(path.join(projectRoot, cfg))) {
|
||||
formatterCache.set(projectRoot, 'prettier');
|
||||
return 'prettier';
|
||||
}
|
||||
}
|
||||
|
||||
formatterCache.set(projectRoot, null);
|
||||
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[] }}
|
||||
*/
|
||||
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 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[] } | 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) {
|
||||
const cacheKey = `${projectRoot}:${formatter}`;
|
||||
if (binCache.has(cacheKey)) return binCache.get(cacheKey);
|
||||
|
||||
const pkg = FORMATTER_PACKAGES[formatter];
|
||||
if (!pkg) {
|
||||
binCache.set(cacheKey, null);
|
||||
return null;
|
||||
}
|
||||
|
||||
const isWin = process.platform === 'win32';
|
||||
const localBin = path.join(projectRoot, 'node_modules', '.bin', isWin ? `${pkg.binName}.cmd` : pkg.binName);
|
||||
|
||||
if (fs.existsSync(localBin)) {
|
||||
const result = { bin: localBin, prefix: [] };
|
||||
binCache.set(cacheKey, result);
|
||||
return result;
|
||||
}
|
||||
|
||||
const runner = getRunnerFromPackageManager(projectRoot);
|
||||
const result = { bin: runner.bin, prefix: [...runner.prefix, pkg.pkgName] };
|
||||
binCache.set(cacheKey, result);
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear all caches. Useful for testing.
|
||||
*/
|
||||
function clearCaches() {
|
||||
projectRootCache.clear();
|
||||
formatterCache.clear();
|
||||
binCache.clear();
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
findProjectRoot,
|
||||
detectFormatter,
|
||||
resolveFormatterBin,
|
||||
clearCaches
|
||||
};
|
||||
File diff suppressed because it is too large
Load Diff
246
tests/lib/resolve-formatter.test.js
Normal file
246
tests/lib/resolve-formatter.test.js
Normal file
@@ -0,0 +1,246 @@
|
||||
/**
|
||||
* Tests for scripts/lib/resolve-formatter.js
|
||||
*
|
||||
* Run with: node tests/lib/resolve-formatter.test.js
|
||||
*/
|
||||
|
||||
const assert = require('assert');
|
||||
const path = require('path');
|
||||
const fs = require('fs');
|
||||
const os = require('os');
|
||||
|
||||
const { findProjectRoot, detectFormatter, resolveFormatterBin, clearCaches } = require('../../scripts/lib/resolve-formatter');
|
||||
|
||||
/**
|
||||
* Run a single test case, printing pass/fail.
|
||||
*
|
||||
* @param {string} name - Test description
|
||||
* @param {() => void} fn - Test body (throws on failure)
|
||||
* @returns {boolean} Whether the test passed
|
||||
*/
|
||||
function test(name, fn) {
|
||||
try {
|
||||
fn();
|
||||
console.log(` ✓ ${name}`);
|
||||
return true;
|
||||
} catch (err) {
|
||||
console.log(` ✗ ${name}`);
|
||||
console.log(` Error: ${err.message}`);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/** Track all created tmp dirs for cleanup */
|
||||
const tmpDirs = [];
|
||||
|
||||
/**
|
||||
* Create a temporary directory and track it for cleanup.
|
||||
*
|
||||
* @returns {string} Absolute path to the new temp directory
|
||||
*/
|
||||
function makeTmpDir() {
|
||||
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'resolve-fmt-'));
|
||||
tmpDirs.push(dir);
|
||||
return dir;
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove all tracked temporary directories.
|
||||
*/
|
||||
function cleanupTmpDirs() {
|
||||
for (const dir of tmpDirs) {
|
||||
try {
|
||||
fs.rmSync(dir, { recursive: true, force: true });
|
||||
} catch {
|
||||
// Best-effort cleanup
|
||||
}
|
||||
}
|
||||
tmpDirs.length = 0;
|
||||
}
|
||||
|
||||
function runTests() {
|
||||
console.log('\n=== Testing resolve-formatter.js ===\n');
|
||||
|
||||
let passed = 0;
|
||||
let failed = 0;
|
||||
|
||||
function run(name, fn) {
|
||||
clearCaches();
|
||||
if (test(name, fn)) passed++;
|
||||
else failed++;
|
||||
}
|
||||
|
||||
// ── findProjectRoot ───────────────────────────────────────────
|
||||
|
||||
run('findProjectRoot: finds package.json in parent dir', () => {
|
||||
const root = makeTmpDir();
|
||||
const sub = path.join(root, 'src', 'lib');
|
||||
fs.mkdirSync(sub, { recursive: true });
|
||||
fs.writeFileSync(path.join(root, 'package.json'), '{}');
|
||||
|
||||
assert.strictEqual(findProjectRoot(sub), root);
|
||||
});
|
||||
|
||||
run('findProjectRoot: returns startDir when no package.json', () => {
|
||||
const root = makeTmpDir();
|
||||
const sub = path.join(root, 'deep');
|
||||
fs.mkdirSync(sub, { recursive: true });
|
||||
|
||||
// No package.json anywhere in tmp → falls back to startDir
|
||||
assert.strictEqual(findProjectRoot(sub), sub);
|
||||
});
|
||||
|
||||
run('findProjectRoot: caches result for same startDir', () => {
|
||||
const root = makeTmpDir();
|
||||
fs.writeFileSync(path.join(root, 'package.json'), '{}');
|
||||
|
||||
const first = findProjectRoot(root);
|
||||
// Remove package.json — cache should still return the old result
|
||||
fs.unlinkSync(path.join(root, 'package.json'));
|
||||
const second = findProjectRoot(root);
|
||||
|
||||
assert.strictEqual(first, second);
|
||||
});
|
||||
|
||||
// ── detectFormatter ───────────────────────────────────────────
|
||||
|
||||
run('detectFormatter: detects biome.json', () => {
|
||||
const root = makeTmpDir();
|
||||
fs.writeFileSync(path.join(root, 'biome.json'), '{}');
|
||||
assert.strictEqual(detectFormatter(root), 'biome');
|
||||
});
|
||||
|
||||
run('detectFormatter: detects biome.jsonc', () => {
|
||||
const root = makeTmpDir();
|
||||
fs.writeFileSync(path.join(root, 'biome.jsonc'), '{}');
|
||||
assert.strictEqual(detectFormatter(root), 'biome');
|
||||
});
|
||||
|
||||
run('detectFormatter: detects .prettierrc', () => {
|
||||
const root = makeTmpDir();
|
||||
fs.writeFileSync(path.join(root, '.prettierrc'), '{}');
|
||||
assert.strictEqual(detectFormatter(root), 'prettier');
|
||||
});
|
||||
|
||||
run('detectFormatter: detects prettier.config.js', () => {
|
||||
const root = makeTmpDir();
|
||||
fs.writeFileSync(path.join(root, 'prettier.config.js'), 'module.exports = {}');
|
||||
assert.strictEqual(detectFormatter(root), 'prettier');
|
||||
});
|
||||
|
||||
run('detectFormatter: detects prettier key in package.json', () => {
|
||||
const root = makeTmpDir();
|
||||
fs.writeFileSync(path.join(root, 'package.json'), JSON.stringify({ name: 'test', prettier: { singleQuote: true } }));
|
||||
assert.strictEqual(detectFormatter(root), 'prettier');
|
||||
});
|
||||
|
||||
run('detectFormatter: ignores package.json without prettier key', () => {
|
||||
const root = makeTmpDir();
|
||||
fs.writeFileSync(path.join(root, 'package.json'), JSON.stringify({ name: 'test' }));
|
||||
assert.strictEqual(detectFormatter(root), null);
|
||||
});
|
||||
|
||||
run('detectFormatter: biome takes priority over prettier', () => {
|
||||
const root = makeTmpDir();
|
||||
fs.writeFileSync(path.join(root, 'biome.json'), '{}');
|
||||
fs.writeFileSync(path.join(root, '.prettierrc'), '{}');
|
||||
assert.strictEqual(detectFormatter(root), 'biome');
|
||||
});
|
||||
|
||||
run('detectFormatter: returns null when no config found', () => {
|
||||
const root = makeTmpDir();
|
||||
assert.strictEqual(detectFormatter(root), null);
|
||||
});
|
||||
|
||||
// ── resolveFormatterBin ───────────────────────────────────────
|
||||
|
||||
run('resolveFormatterBin: uses local biome binary when available', () => {
|
||||
const root = makeTmpDir();
|
||||
const binDir = path.join(root, 'node_modules', '.bin');
|
||||
fs.mkdirSync(binDir, { recursive: true });
|
||||
const binName = process.platform === 'win32' ? 'biome.cmd' : 'biome';
|
||||
fs.writeFileSync(path.join(binDir, binName), '');
|
||||
|
||||
const result = resolveFormatterBin(root, 'biome');
|
||||
assert.strictEqual(result.bin, path.join(binDir, binName));
|
||||
assert.deepStrictEqual(result.prefix, []);
|
||||
});
|
||||
|
||||
run('resolveFormatterBin: falls back to npx for biome', () => {
|
||||
const root = makeTmpDir();
|
||||
const result = resolveFormatterBin(root, 'biome');
|
||||
const expectedBin = process.platform === 'win32' ? 'npx.cmd' : 'npx';
|
||||
assert.strictEqual(result.bin, expectedBin);
|
||||
assert.deepStrictEqual(result.prefix, ['@biomejs/biome']);
|
||||
});
|
||||
|
||||
run('resolveFormatterBin: uses local prettier binary when available', () => {
|
||||
const root = makeTmpDir();
|
||||
const binDir = path.join(root, 'node_modules', '.bin');
|
||||
fs.mkdirSync(binDir, { recursive: true });
|
||||
const binName = process.platform === 'win32' ? 'prettier.cmd' : 'prettier';
|
||||
fs.writeFileSync(path.join(binDir, binName), '');
|
||||
|
||||
const result = resolveFormatterBin(root, 'prettier');
|
||||
assert.strictEqual(result.bin, path.join(binDir, binName));
|
||||
assert.deepStrictEqual(result.prefix, []);
|
||||
});
|
||||
|
||||
run('resolveFormatterBin: falls back to npx for prettier', () => {
|
||||
const root = makeTmpDir();
|
||||
const result = resolveFormatterBin(root, 'prettier');
|
||||
const expectedBin = process.platform === 'win32' ? 'npx.cmd' : 'npx';
|
||||
assert.strictEqual(result.bin, expectedBin);
|
||||
assert.deepStrictEqual(result.prefix, ['prettier']);
|
||||
});
|
||||
|
||||
run('resolveFormatterBin: returns null for unknown formatter', () => {
|
||||
const root = makeTmpDir();
|
||||
const result = resolveFormatterBin(root, 'unknown');
|
||||
assert.strictEqual(result, null);
|
||||
});
|
||||
|
||||
run('resolveFormatterBin: caches resolved binary', () => {
|
||||
const root = makeTmpDir();
|
||||
const binDir = path.join(root, 'node_modules', '.bin');
|
||||
fs.mkdirSync(binDir, { recursive: true });
|
||||
const binName = process.platform === 'win32' ? 'biome.cmd' : 'biome';
|
||||
fs.writeFileSync(path.join(binDir, binName), '');
|
||||
|
||||
const first = resolveFormatterBin(root, 'biome');
|
||||
fs.unlinkSync(path.join(binDir, binName));
|
||||
const second = resolveFormatterBin(root, 'biome');
|
||||
|
||||
assert.strictEqual(first.bin, second.bin);
|
||||
});
|
||||
|
||||
// ── clearCaches ───────────────────────────────────────────────
|
||||
|
||||
run('clearCaches: clears all cached values', () => {
|
||||
const root = makeTmpDir();
|
||||
fs.writeFileSync(path.join(root, 'package.json'), '{}');
|
||||
fs.writeFileSync(path.join(root, 'biome.json'), '{}');
|
||||
|
||||
findProjectRoot(root);
|
||||
detectFormatter(root);
|
||||
resolveFormatterBin(root, 'biome');
|
||||
|
||||
clearCaches();
|
||||
|
||||
// After clearing, removing config should change detection
|
||||
fs.unlinkSync(path.join(root, 'biome.json'));
|
||||
assert.strictEqual(detectFormatter(root), null);
|
||||
});
|
||||
|
||||
// ── Summary & Cleanup ─────────────────────────────────────────
|
||||
|
||||
cleanupTmpDirs();
|
||||
|
||||
console.log('\n=== Test Results ===');
|
||||
console.log(`Passed: ${passed}`);
|
||||
console.log(`Failed: ${failed}`);
|
||||
console.log(`Total: ${passed + failed}`);
|
||||
process.exit(failed > 0 ? 1 : 0);
|
||||
}
|
||||
|
||||
runTests();
|
||||
@@ -31,7 +31,7 @@ function discoverTestFiles(dir, baseDir = dir, acc = []) {
|
||||
const testFiles = discoverTestFiles(testsDir);
|
||||
|
||||
const BOX_W = 58; // inner width between ║ delimiters
|
||||
const boxLine = (s) => `║${s.padEnd(BOX_W)}║`;
|
||||
const boxLine = s => `║${s.padEnd(BOX_W)}║`;
|
||||
|
||||
console.log('╔' + '═'.repeat(BOX_W) + '╗');
|
||||
console.log(boxLine(' Everything Claude Code - Test Suite'));
|
||||
|
||||
Reference in New Issue
Block a user