-
Notifications
You must be signed in to change notification settings - Fork 65
Expand file tree
/
Copy pathsingle-output.ts
More file actions
55 lines (51 loc) · 1.84 KB
/
single-output.ts
File metadata and controls
55 lines (51 loc) · 1.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import * as fs from "node:fs";
import * as path from "node:path";
export function resolveSingleOutputPath(
output: string | false | undefined,
runtimeCwd: string,
requestedCwd?: string,
): string | undefined {
if (typeof output !== "string" || !output) return undefined;
if (path.isAbsolute(output)) return output;
const baseCwd = requestedCwd
? (path.isAbsolute(requestedCwd) ? requestedCwd : path.resolve(runtimeCwd, requestedCwd))
: runtimeCwd;
return path.resolve(baseCwd, output);
}
export function injectSingleOutputInstruction(task: string, outputPath: string | undefined): string {
if (!outputPath) return task;
return `${task}\n\n---\n**Output:** Write your findings to: ${outputPath}`;
}
export function persistSingleOutput(
outputPath: string | undefined,
fullOutput: string,
): { savedPath?: string; error?: string } {
if (!outputPath) return {};
try {
fs.mkdirSync(path.dirname(outputPath), { recursive: true });
fs.writeFileSync(outputPath, fullOutput, "utf-8");
return { savedPath: outputPath };
} catch (err) {
return { error: err instanceof Error ? err.message : String(err) };
}
}
export function finalizeSingleOutput(params: {
fullOutput: string;
truncatedOutput?: string;
outputPath?: string;
exitCode: number;
}): { displayOutput: string; savedPath?: string; saveError?: string } {
let displayOutput = params.truncatedOutput || params.fullOutput;
if (params.outputPath && params.exitCode === 0) {
const save = persistSingleOutput(params.outputPath, params.fullOutput);
if (save.savedPath) {
displayOutput += `\n\n📄 Output saved to: ${save.savedPath}`;
return { displayOutput, savedPath: save.savedPath };
}
if (save.error) {
displayOutput += `\n\n⚠️ Failed to save output to: ${params.outputPath}\n${save.error}`;
return { displayOutput, saveError: save.error };
}
}
return { displayOutput };
}