localfs
    Preparing search index...

    Class LocalFS

    A simple FileSystemDirectoryHandle wrapper to easily navigate a local directory with POSIX paths, as well as other functionality for convenience purposes.

    All methods accept paths as both string, and string[] format (acquired through path.toArray())

    Index

    Constructors

    • // acquire a FileSystemDirectoryHandle
      const handle = await window.showDirectoryPicker();
      const fs = new LocalFS(handle);

      Parameters

      • dirHandle: FileSystemDirectoryHandle

      Returns LocalFS

    Properties

    rootHandle: FileSystemDirectoryHandle
    current: { handle: FileSystemDirectoryHandle; path: string[] }

    Methods

    • Get a FileSystemDirectoryHandle

      Parameters

      • path: string | string[]
      • create: boolean = false

        If true, it will recursively create directories

      Returns Promise<FileSystemDirectoryHandle>

    • Get a FileSystemFileHandle

      Parameters

      • path: string | string[]
      • create: boolean = false

      Returns Promise<FileSystemFileHandle>

    • Resolves the absolute path of given path or FileSystemHandle

      Parameters

      • path: string | string[] | FileSystemHandle

      Returns Promise<string>

    • Changes the current working directory

      Parameters

      • path: string | string[]

      Returns Promise<FileSystemDirectoryHandle>

    • Returns the current working directory

      Returns string

    • Lists all entries of the specified directory

      const fs = new LocalFS(handle);
      const allFiles = await fs.ls("/");
      const jsFiles = await fs.ls("/", (name, handle)=>name.endsWith(".js"));
      const hasPermission = await fs.ls("/", async (name, handle)=>await handle.queryPermission() === "granted");

      Parameters

      • path: string | string[] | FileSystemDirectoryHandle
      • Optionalfilter: (name: string, handle: FileSystemHandle) => boolean | Promise<boolean>

        a function to filter out entries, can be asynchronous

      Returns Promise<[string, FileSystemHandle][]>

    • Overwrites, writes, or appends content to a file.
      This method opens a write stream and immediately closes it after writing.
      For continuous or large-scale writing, consider writing a buffered writer for better performance.

      const fs = new LocalFS(handle);

      // overwrite entire file
      await fs.write("/file.txt", "Hello World!!!");

      // append to end of file
      await fs.write("/file.txt", " append this.", {append: true});

      // refer to (https://developer.mozilla.org/en-US/docs/Web/API/FileSystemWritableFileStream/write#parameters) for clarification
      await fs.write("/file.txt", {
      type: "write",
      position: 6,
      data: "LocalFS"
      }, {append: false, keepExistingData: true});

      Parameters

      • path: string | string[] | FileSystemFileHandle
      • content: FileSystemWriteChunkType

        refer to FileSystemWriteChunkType MDN Docs for clarification

      • Optionaloptions: { append?: boolean } & FileSystemCreateWritableOptions
        • Optionalappend?: boolean

          If true, the content will be appended to the end of the file

        • append

          If true, the content will be appended to the end of the file

      Returns Promise<void>

    • Copy file to destination.
      If a file already exists in its destination, it will be overwritten.

      const fs = new LocalFS(handle);
      await fs.copyFile("/file.txt", "/folder/fileCopy.txt");

      Parameters

      • path: string | string[] | FileSystemFileHandle
      • destination: string | string[] | FileSystemFileHandle

      Returns Promise<void>

    • Copy directory to destination.
      If a directory already exists in its destination, it will be merged and overwritten.

      const fs = new LocalFS(handle);
      await fs.copyDir("/folder", "/path/to/folder");

      Parameters

      • path: string | string[] | FileSystemDirectoryHandle
      • destination: string | string[] | FileSystemDirectoryHandle

      Returns Promise<void>