54 lines
1.5 KiB
TypeScript
54 lines
1.5 KiB
TypeScript
/**
|
|
* Generic CLI runner for Officer tools.
|
|
* Usage: bun run /path/to/seed/tools/run.ts <tool-dir> '<json-params>'
|
|
*
|
|
* Imports the tool's index.ts, calls execute() with the parsed params,
|
|
* and prints the result as JSON.
|
|
*/
|
|
import { join } from 'node:path';
|
|
|
|
const [toolDir, paramsJson] = process.argv.slice(2);
|
|
|
|
if (!toolDir || !paramsJson) {
|
|
console.error('Usage: bun run run.ts <tool-directory> \'{"param":"value"}\'');
|
|
process.exit(1);
|
|
}
|
|
|
|
const entryFile = join(toolDir, 'index.ts');
|
|
|
|
const mod = await import(entryFile);
|
|
const executeFn = mod.execute ?? mod.default?.execute;
|
|
|
|
if (typeof executeFn !== 'function') {
|
|
console.error(`No execute function found in ${entryFile}`);
|
|
process.exit(1);
|
|
}
|
|
|
|
let params: Record<string, unknown>;
|
|
try {
|
|
params = JSON.parse(paramsJson);
|
|
} catch {
|
|
console.error(`Invalid JSON params: ${paramsJson}`);
|
|
process.exit(1);
|
|
}
|
|
|
|
const onUpdate = (partial: { content: Array<{ type: string; text: string }> }) => {
|
|
for (const block of partial.content) {
|
|
if (block.type === 'text') console.log(block.text);
|
|
}
|
|
};
|
|
|
|
try {
|
|
const result = await executeFn('cli', params, undefined, onUpdate);
|
|
if (result?.isError) {
|
|
const text = result.content?.map((c: { text: string }) => c.text).join('\n') ?? 'Tool error';
|
|
console.error(text);
|
|
process.exit(1);
|
|
}
|
|
const text = result?.content?.map((c: { text: string }) => c.text).join('\n') ?? '';
|
|
if (text) console.log(text);
|
|
} catch (err) {
|
|
console.error(String(err));
|
|
process.exit(1);
|
|
}
|