als-path-tree
v1.2.0
Published
A Node.js library for managing a virtual file system structure, offering functionalities for file and directory operations with a shared storage system.
Downloads
15
Maintainers
Readme
als-path-tree
als-path-tree
is a Node.js library for managing a virtual file system structure. It provides functionality for creating, navigating, and managing files and directories in a virtual environment, while tracking the overall size.
Change log for v1.2
- PathTree.objList
- Some fixes with size
Installation
npm install als-path-tree
Usage
First, import the PathTree
class from the library:
const PathTree = require('als-path-tree');
Create a new instance of PathTree
:
const tree = new PathTree();
When specifying a directory in the constructor, the created PathTree
instance operates only within that directory. If files and directories with similar paths are used in other instances, the effect applies to all instances due to the shared storage.
Add files to the tree:
tree.addFile('path/to/file.txt', 'fileContents');
Check if a file exists:
if (tree.exists('path/to/file.txt')) {
console.log('File exists');
}
Retrieve a file:
const file = tree.get('path/to/file.txt');
List files in a directory:
const files = tree.listFiles('path/to/directory');
Remove a file or directory:
tree.remove('path/to/file.txt');
Reset the entire tree:
PathTree.reset();
API Reference
constructor(dir = '')
: Initializes a new Path Tree.addFile(path, file)
: Adds a file at the specified path.exists(path)
: Checks if a file or directory exists at the specified path.get(path)
: Retrieves a file or directory from the specified path.listFiles(dir,directory=this.root,files = [])
: Lists all files in the specified directory.remove(path)
: Removes a file or directory from the specified path, including any empty directories in the path.PathTree.reset()
: Resets the PathTree, clearing all data.PathTree.store
: Object with all nested structure.PathTree.size
: Actual size for all objects and directoriesPathTree.objList
: WeekSet object to include common objects for not counting their size
All paths utilize normalize
, allowing for the use of both forward and backward slashes in paths.