mirror of
https://github.com/getcompanion-ai/co-mono.git
synced 2026-04-21 07:02:04 +00:00
packages
This commit is contained in:
parent
863135d429
commit
43337449e3
88 changed files with 18387 additions and 11 deletions
35
packages/pi-teams/src/utils/hooks.ts
Normal file
35
packages/pi-teams/src/utils/hooks.ts
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
import { execFile } from "node:child_process";
|
||||
import { promisify } from "node:util";
|
||||
import fs from "node:fs";
|
||||
import path from "node:path";
|
||||
|
||||
const execFileAsync = promisify(execFile);
|
||||
|
||||
/**
|
||||
* Runs a hook script asynchronously if it exists.
|
||||
* Hooks are located in .pi/team-hooks/{hookName}.sh relative to the CWD.
|
||||
*
|
||||
* @param teamName The name of the team.
|
||||
* @param hookName The name of the hook to run (e.g., 'task_completed').
|
||||
* @param payload The payload to pass to the hook script as the first argument.
|
||||
* @returns true if the hook doesn't exist or executes successfully; false otherwise.
|
||||
*/
|
||||
export async function runHook(teamName: string, hookName: string, payload: any): Promise<boolean> {
|
||||
const hookPath = path.join(process.cwd(), ".pi", "team-hooks", `${hookName}.sh`);
|
||||
|
||||
if (!fs.existsSync(hookPath)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
try {
|
||||
const payloadStr = JSON.stringify(payload);
|
||||
// Use execFile: More secure (no shell interpolation) and asynchronous
|
||||
await execFileAsync(hookPath, [payloadStr], {
|
||||
env: { ...process.env, PI_TEAM: teamName },
|
||||
});
|
||||
return true;
|
||||
} catch (error) {
|
||||
console.error(`Hook ${hookName} failed:`, error);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue