45 lines
1.2 KiB
TypeScript
45 lines
1.2 KiB
TypeScript
import * as fs from "node:fs/promises";
|
|
import * as path from "node:path";
|
|
import { resolvePath } from "wow-core";
|
|
|
|
export interface ContextFileEntry {
|
|
rel: string;
|
|
content: string;
|
|
}
|
|
|
|
export function resolveContextPath(input: string, cwd: string): string {
|
|
return resolvePath(input, cwd);
|
|
}
|
|
|
|
export async function scanDir(
|
|
dir: string,
|
|
out: ContextFileEntry[],
|
|
): Promise<void> {
|
|
try {
|
|
const entries = await fs.readdir(dir, { withFileTypes: true });
|
|
for (const entry of entries.sort((a, b) => a.name.localeCompare(b.name))) {
|
|
if (!entry.isFile() || !entry.name.endsWith(".md")) continue;
|
|
const filePath = path.join(dir, entry.name);
|
|
const content = await fs.readFile(filePath, "utf-8");
|
|
out.push({ rel: filePath, content: content.trim() });
|
|
}
|
|
} catch {
|
|
// Missing/unreadable context directories are ignored.
|
|
}
|
|
}
|
|
|
|
export async function tryRead(
|
|
absPath: string,
|
|
relHint: string,
|
|
out: ContextFileEntry[],
|
|
): Promise<void> {
|
|
try {
|
|
const stat = await fs.stat(absPath);
|
|
if (!stat.isFile()) return;
|
|
const content = await fs.readFile(absPath, "utf-8");
|
|
out.push({ rel: relHint, content: content.trim() });
|
|
} catch {
|
|
// Missing/unreadable context files are ignored.
|
|
}
|
|
}
|