Skip to content

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 ​

ts
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 ​

ts
new NVEFileSystem(opts?: NVEFileSystemOptions, dirents?: Record<string, NVEDirent>): NVEFileSystem;

Parameters ​

ParameterType
opts?NVEFileSystemOptions
dirents?Record<string, NVEDirent>

Returns ​

NVEFileSystem

Accessors ​

asTree ​

Get Signature ​

ts
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 ​
ts
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 dirents
Returns ​

NVEFileTree

An instance of NVEFileTree representing the file system.

This property is reactive and updates automatically when the file system changes.


dirty ​

Get Signature ​

ts
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 ​
ts
if (fs.dirty) {
  // Prompt user to save changes
}
Returns ​

boolean

true if any dirent is dirty, otherwise false.

Methods ​

commit() ​

ts
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 ​

ts
const success = await fs.commit();
if (!success) {
  // Handle commit failure
}

copyDirent() ​

ts
copyDirent(
   currentName: string,
   currentParentPath: string,
   newName: string,
   newParentPath: string): NVEDirent;

Copies a file or directory to a new name and/or parent path.

Parameters ​

ParameterTypeDescription
currentNamestringThe current name of the entry.
currentParentPathstringThe current parent directory path.
newNamestringThe new name for the entry.
newParentPathstringThe new parent directory path.

Returns ​

NVEDirent

The new NVEDirent instance at the new location.

Throws ​

If the source entry does not exist.

Throws ​

If the destination already exists.


createDirent() ​

ts
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 ​

ParameterTypeDescription
namestringThe name of the file or directory.
parentPathstringThe parent directory path.
content?stringThe file contents (ignored for directories).

Returns ​

NVEDirent

The created NVEDirent instance.

Throws ​


dispose() ​

ts
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() ​

ts
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 ​

ParameterTypeDescription
uriUriThe Monaco Uri representing the file or directory to retrieve.

Returns ​

NVEDirent

The NVEDirent corresponding to the given URI. If the entry does not exist, a new one is created.

Example ​

ts
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 exist

getSyncUris() ​

ts
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() ​

ts
glob(pattern: string | string[]): NVEDirent[];

Returns all entries matching a glob pattern or array of patterns.

Parameters ​

ParameterTypeDescription
patternstring | string[]A glob pattern or array of patterns to match entry paths.

Returns ​

NVEDirent[]

An array of matching NVEDirent instances.


moveDirent() ​

ts
moveDirent(
   currentName: string,
   currentParentPath: string,
   newName: string,
   newParentPath: string): NVEDirent;

Moves a file or directory to a new name and/or parent path.

Parameters ​

ParameterTypeDescription
currentNamestringThe current name of the entry.
currentParentPathstringThe current parent directory path.
newNamestringThe new name for the entry.
newParentPathstringThe new parent directory path.

Returns ​

NVEDirent

The new NVEDirent instance at the new location.

Throws ​

If the source entry does not exist.

Throws ​

If the destination already exists.


off() ​

ts
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 ​

ParameterTypeDescription
eventKey<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() ​

ts
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 ​

ParameterTypeDescription
eventKey<K, NVEFileSystemEvents>The event name to listen for (e.g., 'created', 'moved', 'unlinked', 'disposed').
listenerListener<K, NVEFileSystemEvents>The callback function to invoke when the event is emitted.

Returns ​

void


once() ​

ts
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 ​

ParameterTypeDescription
eventKey<K, NVEFileSystemEvents>The event name to listen for (e.g., 'created', 'moved', 'unlinked', 'disposed').
listenerListener<K, NVEFileSystemEvents>The callback function to invoke when the event is emitted.

Returns ​

void


readDir() ​

ts
readDir(): NVEDirent[];

Returns all file and directory entries in the file system.

Returns ​

NVEDirent[]

An array of all NVEDirent instances.


readDirent() ​

ts
readDirent(name: string, parentPath: string): NVEDirent | undefined;

Reads a file or directory entry by name and parent path.

Parameters ​

ParameterTypeDescription
namestringThe name of the entry.
parentPathstringThe parent directory path.

Returns ​

NVEDirent | undefined

The NVEDirent instance, or undefined if not found.


renameDirent() ​

ts
renameDirent(
   currentName: string,
   currentParentPath: string,
   newName: string,
   newParentPath: string): NVEDirent;

Alias for moveDirent

Parameters ​

ParameterType
currentNamestring
currentParentPathstring
newNamestring
newParentPathstring

Returns ​

NVEDirent


toSerialized() ​

ts
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() ​

ts
unlinkDirent(name: string, parentPath: string): void;

Unlinks (deletes) a file or directory entry by name and parent path.

Parameters ​

ParameterTypeDescription
namestringThe name of the entry to delete.
parentPathstringThe parent directory path.

Returns ​

void


fromSerialized() ​

ts
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 ​

ParameterTypeDescription
serializedstringThe base64-encoded string to deserialize.
opts?NVEFileSystemOptions-

Returns ​

Promise<NVEFileSystem>

The reconstructed NVEFileSystem instance.

Throws ​

If the input is invalid or cannot be deserialized.