Files
blitzkit/packages/scripts/src/core/vfs/abstract.ts
T

79 lines
1.9 KiB
TypeScript
Raw Normal View History

2025-11-29 15:16:09 -06:00
import { readDVPL } from "@blitzkit/core";
2026-04-03 11:47:58 -05:00
import { Options, parse as parseCSV } from "csv-parse/sync";
2025-11-29 15:16:09 -06:00
import { XMLParser } from "fast-xml-parser";
import { normalize } from "path/posix";
2026-04-03 11:47:58 -05:00
import { parse as parseYaml } from "yaml";
2025-11-29 15:16:09 -06:00
2025-11-29 14:34:06 -06:00
export abstract class AbstractVFS {
2025-11-29 15:16:09 -06:00
textDecoder = new TextDecoder();
private xmlParser = new XMLParser();
abstract init(): Promise<typeof this>;
abstract dispose(): void;
2026-04-01 21:34:44 -05:00
abstract has(path: string): Promise<boolean>;
2025-11-29 15:16:09 -06:00
abstract raw(path: string): Promise<Uint8Array>;
2026-04-01 21:34:44 -05:00
async resolve(path: string) {
2025-11-29 15:16:09 -06:00
const normalized = normalize(path);
2026-04-01 21:34:44 -05:00
if (await this.has(normalized)) return normalized;
2025-11-29 15:16:09 -06:00
2025-12-16 12:35:56 -06:00
const dvplPath = `${normalized}.dvpl`;
2026-04-01 21:34:44 -05:00
if (await this.has(dvplPath)) return dvplPath;
2025-11-29 15:16:09 -06:00
return null;
}
2026-04-01 21:34:44 -05:00
async assert(path: string) {
const resolved = await this.resolve(path);
2025-11-29 15:16:09 -06:00
2025-11-29 15:22:13 -06:00
if (resolved === null) throw new Error(`File not found: ${path}`);
2025-11-29 15:16:09 -06:00
return resolved;
}
async file(file: string) {
2026-04-01 21:34:44 -05:00
const resolved = await this.assert(file);
2025-11-29 15:16:09 -06:00
const raw = await this.raw(resolved);
let buffer = raw;
if (resolved.endsWith(".dvpl")) {
buffer = new Uint8Array(readDVPL(Buffer.from(raw)));
}
return buffer;
}
2026-04-01 21:34:44 -05:00
abstract dir(path: string): Promise<string[]>;
2025-11-29 15:16:09 -06:00
async text(path: string) {
const file = await this.file(path);
return this.textDecoder.decode(file);
}
async yaml<Type>(path: string) {
const file = await this.text(path);
2026-04-03 11:47:58 -05:00
return parseYaml(file) as Type;
2025-11-29 15:16:09 -06:00
}
async xml<Type>(path: string) {
const file = await this.text(path);
return this.xmlParser.parse(file) as Type;
}
2026-04-03 11:47:58 -05:00
async _csv(text: string, options: Options = {}) {
const parsed = parseCSV(text, options);
return parsed;
}
async csv(path: string, options: Options = {}) {
const text = await this.text(path);
return await this._csv(text, options);
}
2025-11-29 15:16:09 -06:00
[Symbol.dispose]() {
this.dispose();
}
2025-11-29 14:34:06 -06:00
}