Class: NVEFileSystem
NVEFileSystem provides a reactive, in-memory file system for use with NHT Vue Editors.
This class manages a collection of NVEDirent file and directory entries, supporting creation, reading, moving, copying, unlinking, and globbing of files and directories. It is designed for browser-based playgrounds, REPLs, and virtual file system scenarios.
All operations are reactive and safe for use in Vue applications. Disposing a dirent will automatically remove it from the file system and clean up associated Monaco models.
Example
const fs = new NVEFileSystem();
fs.createDirent("App.vue", "/src", "<template>...</template>");
const file = fs.readDirent("App.vue", "/src");
file?.content; // '<template>...</template>'
fs.unlinkDirent("App.vue", "/src");Constructors
Constructor
new NVEFileSystem(opts?: NVEFileSystemOptions, dirents?: Record<string, NVEDirent>): NVEFileSystem;Parameters
| Parameter | Type |
|---|---|
opts? | NVEFileSystemOptions |
dirents? | Record<string, NVEDirent> |
Returns
NVEFileSystem
Accessors
asTree
Get Signature
get asTree(): NVEFileTree;Returns the file system as a reactive tree structure. This is useful for rendering the file system in a tree view or similar UI components. The tree structure is built from the current state of the file system and reflects all directories and files.
Example
const tree = fs.asTree;
// Use tree in a component or for rendering
console.log(tree.tree); // Access the full tree structure
console.log(tree.flat); // Access the flat map of direntsReturns
An instance of NVEFileTree representing the file system.
This property is reactive and updates automatically when the file system changes.
dirty
Get Signature
get dirty(): boolean;Returns true if any file or directory entry in the file system is dirty (has unsaved changes).
This property is reactive and can be used to track whether the file system has uncommitted changes. It checks the dirty state of all NVEDirent entries.
Example
if (fs.dirty) {
// Prompt user to save changes
}Returns
boolean
true if any dirent is dirty, otherwise false.
Methods
commit()
commit(): Promise<boolean>;Commits all changes in the file system.
This method will invoke the file system-level commit handler (onCommit) if provided, or fall back to committing each dirent individually using onDirentCommit if only that is provided.
If neither commit handler is defined, an error is thrown.
Returns
Promise<boolean>
A Promise resolving to true if the commit was successful, or false if it failed.
Throws
If no commit handler is defined.
Example
const success = await fs.commit();
if (!success) {
// Handle commit failure
}copyDirent()
copyDirent(
currentName: string,
currentParentPath: string,
newName: string,
newParentPath: string): NVEDirent;Copies a file or directory to a new name and/or parent path.
Parameters
| Parameter | Type | Description |
|---|---|---|
currentName | string | The current name of the entry. |
currentParentPath | string | The current parent directory path. |
newName | string | The new name for the entry. |
newParentPath | string | The new parent directory path. |
Returns
The new NVEDirent instance at the new location.
Throws
If the source entry does not exist.
Throws
If the destination already exists.
createDirent()
createDirent(
name: string,
parentPath: string,
content?: string): NVEDirent;Creates a new file or directory entry in the file system. Throws if an entry with the same name and parentPath already exists.
Parameters
| Parameter | Type | Description |
|---|---|---|
name | string | The name of the file or directory. |
parentPath | string | The parent directory path. |
content? | string | The file contents (ignored for directories). |
Returns
The created NVEDirent instance.
Throws
dispose()
dispose(): void;Disposes of all file and directory entries in the file system, cleaning up resources and models. After calling this method, the file system will be empty and all NVEDirent instances will be disposed.
Returns
void
getByUri()
getByUri(uri: Uri): NVEDirent;Retrieves a NVEDirent by its Monaco Uri.
If the file or directory does not exist at the given URI, it will be created as an empty file entry. This is useful for resolving or lazily creating files based on Monaco's URI objects, such as when opening files from the editor or language service.
Parameters
| Parameter | Type | Description |
|---|---|---|
uri | Uri | The Monaco Uri representing the file or directory to retrieve. |
Returns
The NVEDirent corresponding to the given URI. If the entry does not exist, a new one is created.
Example
const uri = monaco.Uri.parse("file:///src/App.vue");
const dirent = fs.getByUri(uri);
// dirent is the NVEDirent for /src/App.vue, created if it did not existgetSyncUris()
getSyncUris(): Uri[];Returns all file URIs in the file system that should be synchronized with language services.
This is used to provide the list of open/sync'd files to Volar's language service integration callbacks:
- volar.activateMarkers ("vue-markers-owner" callback)
- volar.activateAutoInsertion (callback)
- volar.registerProviders (callback)
These callbacks expect an array of Monaco Uri objects representing the files to be considered for diagnostics, auto-insertion, and language features. Only files (not directories) are included.
Returns
Uri[]
An array of Monaco Uri instances for all files in the file system.
glob()
glob(pattern: string | string[]): NVEDirent[];Returns all entries matching a glob pattern or array of patterns.
Parameters
| Parameter | Type | Description |
|---|---|---|
pattern | string | string[] | A glob pattern or array of patterns to match entry paths. |
Returns
An array of matching NVEDirent instances.
moveDirent()
moveDirent(
currentName: string,
currentParentPath: string,
newName: string,
newParentPath: string): NVEDirent;Moves a file or directory to a new name and/or parent path.
Parameters
| Parameter | Type | Description |
|---|---|---|
currentName | string | The current name of the entry. |
currentParentPath | string | The current parent directory path. |
newName | string | The new name for the entry. |
newParentPath | string | The new parent directory path. |
Returns
The new NVEDirent instance at the new location.
Throws
If the source entry does not exist.
Throws
If the destination already exists.
off()
off<K>(event: Key<K, NVEFileSystemEvents>, listener?: Listener<K, NVEFileSystemEvents>): void;Removes an event listener for a file system event.
Type Parameters
| Type Parameter |
|---|
K |
Parameters
| Parameter | Type | Description |
|---|---|---|
event | Key<K, NVEFileSystemEvents> | The event name to remove the listener from. |
listener? | Listener<K, NVEFileSystemEvents> | The callback function to remove. If omitted, all listeners for the event are removed. |
Returns
void
on()
on<K>(event: Key<K, NVEFileSystemEvents>, listener: Listener<K, NVEFileSystemEvents>): void;Registers an event listener for a file system event.
Type Parameters
| Type Parameter |
|---|
K |
Parameters
| Parameter | Type | Description |
|---|---|---|
event | Key<K, NVEFileSystemEvents> | The event name to listen for (e.g., 'created', 'moved', 'unlinked', 'disposed'). |
listener | Listener<K, NVEFileSystemEvents> | The callback function to invoke when the event is emitted. |
Returns
void
once()
once<K>(event: Key<K, NVEFileSystemEvents>, listener: Listener<K, NVEFileSystemEvents>): void;Registers a one-time event listener for a file system event. The listener is removed after the first invocation.
Type Parameters
| Type Parameter |
|---|
K |
Parameters
| Parameter | Type | Description |
|---|---|---|
event | Key<K, NVEFileSystemEvents> | The event name to listen for (e.g., 'created', 'moved', 'unlinked', 'disposed'). |
listener | Listener<K, NVEFileSystemEvents> | The callback function to invoke when the event is emitted. |
Returns
void
readDir()
readDir(): NVEDirent[];Returns all file and directory entries in the file system.
Returns
An array of all NVEDirent instances.
readDirent()
readDirent(name: string, parentPath: string): NVEDirent | undefined;Reads a file or directory entry by name and parent path.
Parameters
| Parameter | Type | Description |
|---|---|---|
name | string | The name of the entry. |
parentPath | string | The parent directory path. |
Returns
NVEDirent | undefined
The NVEDirent instance, or undefined if not found.
renameDirent()
renameDirent(
currentName: string,
currentParentPath: string,
newName: string,
newParentPath: string): NVEDirent;Alias for moveDirent
Parameters
| Parameter | Type |
|---|---|
currentName | string |
currentParentPath | string |
newName | string |
newParentPath | string |
Returns
toSerialized()
toSerialized(): Promise<string>;Serializes the entire file system to a compact, base64-encoded string.
Each file/directory path and its corresponding NVEDirent are encoded and stored in a single object, which is then compressed and prefixed for identification. This is suitable for storage, sharing, or persistence of the file system state.
Returns
Promise<string>
A base64-encoded string representing the serialized file system.
unlinkDirent()
unlinkDirent(name: string, parentPath: string): void;Unlinks (deletes) a file or directory entry by name and parent path.
Parameters
| Parameter | Type | Description |
|---|---|---|
name | string | The name of the entry to delete. |
parentPath | string | The parent directory path. |
Returns
void
fromSerialized()
static fromSerialized(serialized: string, opts?: NVEFileSystemOptions): Promise<NVEFileSystem>;Deserializes a NVEFileSystem instance from a string produced by toSerialized.
Validates the input and reconstructs the file system, restoring all file and directory entries.
Parameters
| Parameter | Type | Description |
|---|---|---|
serialized | string | The base64-encoded string to deserialize. |
opts? | NVEFileSystemOptions | - |
Returns
Promise<NVEFileSystem>
The reconstructed NVEFileSystem instance.
Throws
If the input is invalid or cannot be deserialized.