UNPKG

@synet/patterns

Version:

Robust, battle-tested collection of stable patterns used in Synet packages

64 lines (56 loc) 1.72 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.UnitSystem = void 0; const unit_1 = require("./unit"); /** * Interface for file system operations * Generic over file type to allow for different file implementations export interface IUnitOptions { name?: string [x: string]: unknown } export interface IFileUnit { data: string; format: string; } /** * Basic file system implementation * The foundation that everything builds on */ class UnitSystem { constructor(filesystem, options) { this.filesystem = filesystem; this.options = options; this.name = options?.name || 'UnitSystem'; } async readFile(path) { const file = await this.filesystem.readFile(path); if (!file) { throw new Error(`File not found: ${path}`); } const unit = unit_1.Unit.fromJSON(file); if (unit.isFailure) { throw new Error(`Failed to create unit from file: ${unit.errorMessage}`); } return unit.value.data.toString(); } async writeFile(path, props) { const fileUnit = unit_1.Unit.create({ data: props.data, }); if (fileUnit.isFailure) { throw new Error(`Failed to create file unit: ${fileUnit.errorMessage}`); } if (fileUnit.isFailure) { throw new Error(`Failed to create unit from data: ${fileUnit.errorMessage}`); } this.filesystem.writeFile(path, fileUnit.value.toJSON()); } async deleteFile(path) { this.filesystem.deleteFile(path); } async exists(path) { return this.filesystem.exists(path); } } exports.UnitSystem = UnitSystem;