UNPKG

@jsvfs/core

Version:

The JavaScript Virtual File System - an extensible engine for file operations.

127 lines 3.43 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Link = exports.File = exports.Root = exports.Folder = exports.ParentItem = void 0; class ItemBase { constructor(item) { this.adapter = item.adapter; this.type = item.type; this.path = item.path; this.name = item.name; this.contents = item.contents; this.parent = item.parent; this.committed = false; } } class ParentItem extends ItemBase { constructor(item) { super({ ...item, type: item.type === 'root' ? 'root' : 'folder' }); this.contents = new Map(); } list(long = false) { if (long) return Array.from(this.contents.values()); return Array.from(this.contents.keys()); } get(name) { return this.contents.get(name); } set(name, item) { item.parent = this; this.contents.set(name, item); } delete(name) { return this.contents.delete(name); } get size() { let result = 0; for (const item of this.contents.values()) { result += item.size; } return result; } get count() { return this.contents.size; } async commit() { // Don't commit again. if (this.committed) return; if (this.count > 0) { for (const item of this.contents.values()) { await item.commit(); } } else { await this.adapter.mkdir(this.path); } this.committed = true; } } exports.ParentItem = ParentItem; /** Represents a folder item in the file system. */ class Folder extends ParentItem { constructor(item) { super({ ...item, type: 'folder' }); } } exports.Folder = Folder; /** A special item; represents the top-level of the file system. */ class Root extends ParentItem { constructor(adapter) { super({ type: 'root', adapter }); this.contents = new Map(); this.path = '/'; this.name = 'ROOT'; } } exports.Root = Root; /** Represents a file item in the file system. */ class File extends ItemBase { constructor(item) { super({ ...item, type: 'file' }); } get size() { return this.contents.length; } async commit() { // Don't commit again. if (this.committed) return; await this.adapter.write(this.path, this.contents); this.committed = true; } } exports.File = File; /** Represents a link item in the file system; defaults to type 'softlink'. */ class Link extends ItemBase { constructor(item) { super({ ...item, type: item.type ?? 'softlink' }); } get size() { return 0; } async commit() { // Don't commit again. if (this.committed) return; // Ensure that the target exists before linking. if (!this.contents.committed) await this.contents.commit(); await this.adapter.link(this.path, this.contents.path, this.type); this.committed = true; } } exports.Link = Link; //# sourceMappingURL=Item.js.map