Project Files
src / utils / config.ts
import * as path from "path";
import * as fs from "fs";
/**
* Resolve a file path:
* - Absolute paths are used as-is
* - Relative paths are resolved against the LM Studio working directory
*/
export function resolvePath(filePath: string, workingDir: string): string {
return path.isAbsolute(filePath) ? filePath : path.resolve(workingDir, filePath);
}
/** Create a .bak copy before destructive operations */
export function backupFile(filePath: string): string {
const backupPath = filePath + ".bak";
fs.copyFileSync(filePath, backupPath);
return backupPath;
}
/** 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 = [".xlsx", ".xlsm", ".xls", ".xlsb", ".ods", ".csv", ".tsv"];
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;
}