diff --git a/hooks/hooks.json b/hooks/hooks.json index 791a910c6..c8b77fbe4 100644 --- a/hooks/hooks.json +++ b/hooks/hooks.json @@ -25,7 +25,8 @@ "hooks": [ { "type": "command", - "command": "node -e \"const p=require('path');const r=(function(){var p=require('path'),f=require('fs'),o=require('os');var e=process.env.CLAUDE_PLUGIN_ROOT;if(e&&e.trim())return e.trim();var d=(process.env.CLAUDE_CONFIG_DIR||'').trim()||p.join(o.homedir(),'.claude');if(d==='~')d=o.homedir();else if(d.charAt(0)==='~'&&(d.charAt(1)==='/'||d.charCodeAt(1)===92))d=p.join(o.homedir(),d.slice(2));try{if(o.platform()==='linux'){var v=f.readFileSync('/proc/version','utf8').toLowerCase();if(v.includes('microsoft')){var b=String.fromCharCode(92),m=d.match(/^([A-Za-z]):/);if(m&&(d.charAt(2)==='/'||d.charAt(2)===b)){var dl=m[1].toLowerCase();d='/mnt/'+dl+d.slice(2).split(b).join('/')}}}}catch(_){}d=p.resolve(d);function L(x){try{return require(p.join(x,'scripts','lib','resolve-ecc-root')).resolveEccRoot()}catch(_){return null}}var r=L(d);if(r)return r;var s=['ecc','ecc@ecc','marketplaces/ecc','everything-claude-code','everything-claude-code@everything-claude-code','marketplaces/everything-claude-code'];for(var i=0;i { - process.stderr.write(`[Hook] run-with-flags error: ${err.message}\n`); - process.exit(0); -}); +async function main() { + const [, , hookId, relScriptPath, profilesCsv] = process.argv; + const input = await readStdinRaw(); + const result = await runHook(hookId, relScriptPath, profilesCsv, input); + exitWithStdout(result.stdout, result.exitCode); +} + +if (require.main === module) { + installHookWatchdog({ label: 'run-with-flags' }); + main().catch(err => { + process.stderr.write(`[Hook] run-with-flags error: ${err.message}\n`); + process.exit(0); + }); +} + +module.exports = { runHook }; diff --git a/tests/hooks/plugin-hook-bootstrap.test.js b/tests/hooks/plugin-hook-bootstrap.test.js index 3b3153efb..55d1af505 100644 --- a/tests/hooks/plugin-hook-bootstrap.test.js +++ b/tests/hooks/plugin-hook-bootstrap.test.js @@ -169,6 +169,33 @@ process.stdout.write(JSON.stringify({ } })) passed++; else failed++; + if (test('node mode runs run-with-flags.js in-process instead of spawning a second node', () => { + const root = createTempDir(); + try { + writeFile(root, path.join('scripts', 'hooks', 'run-with-flags.js'), ` +module.exports.runHook = async (hookId, relScriptPath, profilesCsv, input) => ({ + stdout: JSON.stringify({ pid: process.pid, hookId, relScriptPath, profilesCsv, raw: input.raw }), + exitCode: 2, +}); +`); + + const result = run( + ['node', path.join('scripts', 'hooks', 'run-with-flags.js'), 'pre:test', 'scripts/hooks/x.js', 'standard'], + { root, input: 'payload' } + ); + const parsed = JSON.parse(result.stdout); + + assert.strictEqual(result.status, 2, result.stderr); + assert.strictEqual(parsed.pid, result.pid); + assert.deepStrictEqual( + [parsed.hookId, parsed.relScriptPath, parsed.profilesCsv, parsed.raw], + ['pre:test', 'scripts/hooks/x.js', 'standard', 'payload'] + ); + } finally { + cleanup(root); + } + })) passed++; else failed++; + if (test('node mode emits empty stdout when child exits cleanly without stdout', () => { const root = createTempDir(); try {