@tobshub/browser-file-system
Version:
File system for your browser
173 lines (171 loc) • 5.94 kB
TypeScript
interface FSNode {
/**
* The name of a node
*
* Should be unique across its direct siblings
*/
name: string;
/**
* The type of a node
*
* Can either be a Directory or a File
* */
type: "dir" | "file";
}
type LeanBrowserFSDir = FSNode & {
type: "dir";
children: (LeanBrowserFSDir | LeanBrowserFSFile)[];
content?: null;
};
type LeanBrowserFSFile = FSNode & {
type: "file";
children?: null;
content: string | undefined;
};
/** Hydrated Node with methods */
type BrowserFSNode = BrowserFSFile | BrowserFSDir;
/** Lean Node without methods */
type LeanBrowserFSNode = LeanBrowserFSDir | LeanBrowserFSFile;
/** Creates a Browser File System instance */
declare class BrowserFS {
private readonly key;
private pathTo;
name: string;
type: "root";
children: LeanBrowserFSNode[];
private storage;
constructor(key: string, storage?: "indexeddb" | "localstorage");
/** Initialize BrowserFS or load existing data from storage */
init(): Promise<void>;
/** Save the current file system in the storage */
save(): Promise<void>;
/**
* Takes a path (relative or absolute) to an item in the file system and returns an array of the absolute path to the input
* @returns an array containing moves to a specific directory
*/
private normalisePath;
/** @returns the current active directory path */
getCurrentPath(): string;
/**
* Takes a path to an item in the file system and returns that item if it is found
*
* Returns null if the item isn't found
* @param pathTo - a relative or absolute path to an item in the file system
* @returns the item at the end of the path or null if the item is not found
*/
getItemAtPath(pathTo?: string): {
item: BrowserFSNode | BrowserFS | null;
path: string[];
};
/**
* Takes a path to an item in the file system and returns that item if it is found
*
* Returns null if the item isn't found
* @param pathTo - a relative or absolute path to an item in the file system
* @returns the item at the end of the path or null if the item is not found
*/
private getRawItemAtPath;
/**
* Takes a path to an directory in the file system and sets the `this.pathTo` to the absolute path of that directory
*
* Fails if the path points to a file or does not exist
* @param path - the relative or absolute path to the directory in the file system
* */
setCurrentDir(path: string): void;
/**
* Adds the children to the item at the provided path
*
* Throws an error if the item at the path does not exist or is a file
*
* @param path - a relative or absolute path to a directory in the file system
* @param children - an array of children to add to the item at the specified path
*/
addChildren(path: string, children: (LeanBrowserFSDir | LeanBrowserFSFile)[]): Promise<void>;
/**
* Removes an item from the file system
*
* Throws an error if the item's parent does not exist
*
* @param pathTo - a relative or absolute path to an item in the file system
*/
removeItem(pathTo: string): Promise<void>;
/**
* Renames item at the given path
*
* Throws an error if the item is a direct or indirect parent of the current path
* Throws an error if the item is the root node or the BrowserFS instance
*
* @param {String} pathTo the path the item to rename
* @param {String} newName the new name to give the item
* */
renameItem(pathTo: string, newName: string): Promise<void>;
/**
* Move an item at `pathTo` to `newParentPath`
*
* Copies the item and leaves the original by default
*
* The name of the item stays the same unless `newName` is provided
* @param {String} pathTo the path to the the item
* @param {String} newParentPath the parent path of the new location
* @param {Object} options (optional) options to change the type of move, e.g. full move/copy
* */
moveItem(pathTo: string, newParentPath: string, options?: {
moveType?: "move" | "copy";
}): Promise<void>;
}
/**
* A node, in the BrowserFS instance, that can have children
*/
declare class BrowserFSDir implements LeanBrowserFSDir {
name: string;
private root;
type: "dir";
children: LeanBrowserFSNode[];
constructor(name: string, children: (LeanBrowserFSDir | LeanBrowserFSFile)[], root: BrowserFS);
/**
* Adds the children to the item at the provided path
*
* Throws an error if the item at the path does not exist or is a file
*
* @param path - a relative or absolute path to a directory in the file system
* @param children - an array of children to add to the item at the specified path
*/
addChildren(children: (LeanBrowserFSDir | LeanBrowserFSFile)[]): Promise<void>;
/**
* Calls the parent save function
*
* Stops when the root save function is called
* */
private save;
}
/**
* A node, in the BrowserFS instance, that can have content
*/
declare class BrowserFSFile implements LeanBrowserFSFile {
name: string;
content: string | undefined;
private root;
type: "file";
children: null;
constructor(name: string, content: string | undefined, root: BrowserFS);
/**
* Writes (or overwrites) the content on the node
*
* @param content set the content of the node
* @returns the new content
*/
write(content: string): Promise<string>;
/**
* Reads the content of the node
*
* @returns the content of the node
*/
read(): string | undefined;
/**
* Calls the parent save function
*
* Stops when the root save function is called
* */
private save;
}
export { LeanBrowserFSNode, BrowserFS as default };