-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathfileUtils.ts
More file actions
66 lines (58 loc) · 1.52 KB
/
fileUtils.ts
File metadata and controls
66 lines (58 loc) · 1.52 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
56
57
58
59
60
61
62
63
64
65
66
import { mkdir, writeFile } from "fs/promises";
import { join } from "path";
const RUNS_DIR = "runs";
export interface PipelineOutput {
essay: string;
review: string;
revision: string;
}
/**
* Ensures the runs directory exists, creating it if necessary.
*/
async function ensureRunsDirectory(): Promise<void> {
try {
await mkdir(RUNS_DIR, { recursive: true });
} catch (error) {
// Directory might already exist, which is fine
if ((error as NodeJS.ErrnoException).code !== "EEXIST") {
throw error;
}
}
}
/**
* Generates a timestamp string for filenames.
*/
function getTimestamp(): string {
const now = new Date();
return now.toISOString().replace(/[:.]/g, "-").slice(0, -5);
}
/**
* Writes the pipeline outputs to markdown files in the runs directory.
*/
export async function writePipelineOutputs(
outputs: PipelineOutput
): Promise<void> {
await ensureRunsDirectory();
const timestamp = getTimestamp();
const files = [
{
path: join(RUNS_DIR, `${timestamp}-essay.md`),
content: `# Original Essay\n\n${outputs.essay}`,
},
{
path: join(RUNS_DIR, `${timestamp}-review.md`),
content: `# Review Feedback\n\n${outputs.review}`,
},
{
path: join(RUNS_DIR, `${timestamp}-revision.md`),
content: `# Revised Essay\n\n${outputs.revision}`,
},
];
for (const file of files) {
await writeFile(file.path, file.content, "utf-8");
}
console.log(`\n✓ Files written:`);
files.forEach((file) => {
console.log(` - ${file.path}`);
});
}