Project Files
src / utils / config.ts
import * as path from "path";
import * as fs from "fs";
/** Resolve a filePath against an optional workingDir (or process.cwd() as fallback) */
export function resolvePath(filePath: string, workingDir?: string): string {
if (path.isAbsolute(filePath)) return filePath;
return path.resolve(workingDir ?? process.cwd(), filePath);
}
/** Create a .bak copy before destructive operations */
export function backupFile(filePath: string): string {
const bakPath = filePath + ".bak";
fs.copyFileSync(filePath, bakPath);
return bakPath;
}
/** Ensure the directory for a file path exists */
export function ensureDir(filePath: string): void {
const dir = path.dirname(filePath);
if (!fs.existsSync(dir)) fs.mkdirSync(dir, { recursive: true });
}
/** Supported formats for reading */
export const READABLE_EXTS = [".docx", ".docm"];
export function checkReadable(filePath: string): string | null {
if (!fs.existsSync(filePath)) return `File not found: "${filePath}"`;
const ext = path.extname(filePath).toLowerCase();
if (!READABLE_EXTS.includes(ext)) {
return `Unsupported format "${ext}". Supported: ${READABLE_EXTS.join(", ")}`;
}
return null;
}