markmv - v1.41.7
    Preparing search index...

    Variable FileUtilsConst

    FileUtils: {
        copyFile: (
            sourcePath: string,
            destinationPath: string,
            options?: Readonly<FileCopyOptions>,
        ) => Promise<void>;
        createBackup: (filePath: string, suffix?: string) => Promise<string>;
        deleteFile: (filePath: string) => Promise<void>;
        ensureDirectory: (dirPath: string) => Promise<void>;
        exists: (path: string) => Promise<boolean>;
        filesEqual: (path1: string, path2: string) => Promise<boolean>;
        findMarkdownFiles: (
            dirPath: string,
            recursive?: boolean,
        ) => Promise<string[]>;
        getFileSize: (filePath: string) => Promise<number>;
        getRelativePath: (fromFile: string, toFile: string) => string;
        getStats: (path: string) => Promise<FileStats>;
        isReadable: (path: string) => Promise<boolean>;
        isWritable: (path: string) => Promise<boolean>;
        listFiles: (
            dirPath: string,
            options?: {
                extensions?: string[];
                includeDirectories?: boolean;
                recursive?: boolean;
            },
        ) => Promise<string[]>;
        moveFile: (
            sourcePath: string,
            destinationPath: string,
            options?: Readonly<FileMoveOptions>,
        ) => Promise<void>;
        readTextFile: (filePath: string) => Promise<string>;
        sanitizeFilename: (filename: string) => string;
        writeTextFile: (
            filePath: string,
            content: string,
            options?: Readonly<{ createDirectories?: boolean }>,
        ) => Promise<void>;
    } = ...

    Utility namespace for common file system operations.

    Provides a comprehensive set of functions for file and directory manipulation, with proper error handling and cross-platform compatibility. All methods are async and use Node.js promises-based file system APIs.

    Type Declaration

    • copyFile: (
          sourcePath: string,
          destinationPath: string,
          options?: Readonly<FileCopyOptions>,
      ) => Promise<void>

      Copies a file. Throws when the destination exists unless overwrite is set, creates the destination's parent directories by default, and can preserve the source timestamps.

    • createBackup: (filePath: string, suffix?: string) => Promise<string>

      Copies a file to its own path plus a suffix (.backup by default) and resolves to the backup's path. Throws when that backup already exists.

    • deleteFile: (filePath: string) => Promise<void>

      Deletes a file. Resolves without error when the path does not exist.

    • ensureDirectory: (dirPath: string) => Promise<void>

      Creates a directory and any missing parents. Resolves without error when the directory already exists.

    • exists: (path: string) => Promise<boolean>

      Resolves to true when the path exists, and false when it does not or cannot be accessed.

    • filesEqual: (path1: string, path2: string) => Promise<boolean>

      Resolves to true when both files decode to identical text, and false when they differ or either cannot be read.

    • findMarkdownFiles: (dirPath: string, recursive?: boolean) => Promise<string[]>

      Lists the markdown files (extensions .md, .markdown, .mdown, .mkd and .mdx) in a directory, recursing into subdirectories unless recursive is false.

    • getFileSize: (filePath: string) => Promise<number>

      Resolves to the size of a file in bytes.

    • getRelativePath: (fromFile: string, toFile: string) => string

      Computes the path to toFile relative to the directory containing fromFile, as used for a link written inside fromFile.

    • getStats: (path: string) => Promise<FileStats>

      Reads the size, type and timestamps of a path into a FileStats. Rejects when the path does not exist.

    • isReadable: (path: string) => Promise<boolean>

      Resolves to true when the current process has read permission on the path, and false when it lacks it or the path does not exist.

    • isWritable: (path: string) => Promise<boolean>

      Resolves to true when the current process has write permission on the path, and false when it lacks it or the path does not exist.

    • listFiles: (
          dirPath: string,
          options?: {
              extensions?: string[];
              includeDirectories?: boolean;
              recursive?: boolean;
          },
      ) => Promise<string[]>

      Lists the files in a directory as joined paths, optionally recursing, filtering by extension and including directories.

    • moveFile: (
          sourcePath: string,
          destinationPath: string,
          options?: Readonly<FileMoveOptions>,
      ) => Promise<void>

      Moves a file after validating both paths and checking the source exists. Throws when the destination exists unless overwrite is set, optionally backing the old destination up first. Renames in place, falling back to copy and delete when the move crosses file systems.

    • readTextFile: (filePath: string) => Promise<string>

      Reads a whole file and decodes it as UTF-8. No other encoding is detected.

    • sanitizeFilename: (filename: string) => string

      Replaces characters that are invalid in file names (<>:"/\|?*) and whitespace with hyphens, collapses repeated hyphens and trims hyphens from both ends.

    • writeTextFile: (
          filePath: string,
          content: string,
          options?: Readonly<{ createDirectories?: boolean }>,
      ) => Promise<void>

      Writes a string to a file as UTF-8, replacing any existing content. Optionally creates the parent directories first.

    // Check if file exists
    const exists = await FileUtils.exists('document.md');

    // Read file content
    const content = await FileUtils.readTextFile('document.md');

    // Write new content
    await FileUtils.writeTextFile('output.md', content, { createDirectories: true });

    // Find markdown files
    const files = await FileUtils.findMarkdownFiles('./docs', true);