import { isUndefined, getPathDirname } from './utils.ts';
export class Reader {
public static async read(path: string): Promise<string> { if (isUndefined(path)) return '[]';
if (await exists(path) === false) { await ensureFile(path, '[]'); return '[]'; }
const content: string = await Deno.readTextFile(path); return content; }
public static readSync(path: string): string { if (isUndefined(path)) return '[]';
if (existsSync(path) === false) { ensureFileSync(path, '[]'); return '[]'; }
const content: string = Deno.readTextFileSync(path); return content; }}
async function exists(path: string): Promise<boolean> { try { await Deno.lstat(path); return true; } catch (error) { if (error instanceof Deno.errors.NotFound) return false; throw error; }}
function existsSync(path: string): boolean { try { Deno.lstatSync(path); return true; } catch (error) { if (error instanceof Deno.errors.NotFound) return false; throw error; }}
async function ensureFile(path: string, data: string = ''): Promise<void> { try { const info = await Deno.lstat(path); if (!info.isFile) throw new Error('Invalid file specified'); } catch (error) { if (error instanceof Deno.errors.NotFound) { const dirname: string = getPathDirname(path); await ensureDir(dirname); await Deno.writeTextFile(path, data); return; }
throw error; }}
function ensureFileSync(path: string, data: string = ''): void { try { const info = Deno.lstatSync(path); if (!info.isFile) throw new Error('Invalid file specified'); } catch (error) { if (error instanceof Deno.errors.NotFound) { const dirname: string = getPathDirname(path); ensureDirSync(dirname); Deno.writeTextFileSync(path, data); return; }
throw error; }}
async function ensureDir(path: string): Promise<void> { try { const info: Deno.FileInfo = await Deno.lstat(path); if (!info.isDirectory) throw new Error('Invalid directory specified'); } catch (error) { if (error instanceof Deno.errors.NotFound) { await Deno.mkdir(path, { recursive: true }); return; }
throw error; }}
function ensureDirSync(path: string): void { try { const info: Deno.FileInfo = Deno.lstatSync(path); if (!info.isDirectory) throw new Error('Invalid directory specified'); } catch (error) { if (error instanceof Deno.errors.NotFound) { Deno.mkdirSync(path, { recursive: true }); return; }
throw error; }}