Class AioFileOperation

Hierarchy (View Summary)

Constructors

Properties

_content: any = ''
_root: string
ext: string = ''
filename: string = ''
filepath: undefined | string = ''
outputPath: string = ''

Accessors

  • get content(): any
  • Returns any

  • set content(value: any): void
  • Parameters

    • value: any

    Returns void

Methods

  • Extracts the file extension from a given filepath and returns it in lowercase.

    Parameters

    • filepath: string

      The path of the file.

    Returns string

    The file extension in lowercase.


    _getExtension('/path/to/my/file.txt') // returns '.txt'
  • Returns the filename without the extension from a given filepath.

    Parameters

    • filepath: string

      The filepath to extract the filename from.

    Returns string

    The filename without the extension.


    _getFilename('/path/to/my/file.txt') // returns 'file'
  • Checks if a directory exists. If it doesn't, it creates the directory and any necessary parent directories.

    Parameters

    • dir: string

      The path of the directory to check or create.

    Returns Promise<void>

    • A promise that resolves when the directory exists or has been created.
    • Throws an error if there's a problem creating the directory.

    const fileOp = new AioFileOperation();
    await fileOp.isDirectoryExists('/path/to/new/directory');
    // If '/path/to/new/directory' doesn't exist, it will be created.
  • Opens a file at the specified path and returns a file handle.

    Parameters

    • filePath: string

      The path of the file to open.

    Returns Promise<FileHandle>

    • A promise that resolves to a FileHandle object for the opened file.
    • Throws an error if the file cannot be opened.

    const fileOp = new AioFileOperation();
    const fileHandle = await fileOp.open('/path/to/file.txt');
  • Reads a file synchronously and returns its content as a string.

    Parameters

    • path: string

      The path to the file to be read.

    Returns Promise<string>

    • A promise that resolves to the content of the file as a string. *

    const fileOp = new AioFileOperation();
    const lines = await fileOp.read('/path/to/large/file.txt');
    console.log(lines); // Output: ['Line 1', 'Line 2', 'Line 3', ...]
  • Removes a file if it exists.

    Parameters

    • filePath: string

      The path to the file to be removed.

    Returns Promise<void>

    • A promise that resolves when the file has been removed. *

    const fileOp = new AioFileOperation();
    await fileOp.remove('/path/to/large/file.txt');
  • Reads a file line by line and returns an array of strings, where each string is a line from the file.

    This method opens a file, reads it line by line, and stores each line as a separate string in an array. It uses an asynchronous iterator to efficiently handle large files without loading the entire content into memory at once.

    Parameters

    • filePath: string

      The path to the file to be scanned.

    Returns Promise<string[]>

    • A promise that resolves to an array of strings, where each string is a line from the file.

    const fileOp = new AioFileOperation();
    const lines = await fileOp.scan('/path/to/large/file.txt');
    console.log(lines); // Output: ['Line 1', 'Line 2', 'Line 3', ...]
  • Searches for files within a specified directory, optionally filtering by file extension.

    This method recursively scans a directory and its subdirectories for files. It can filter the results based on a specified file extension or an array of extensions. If no extension is provided, it returns all files. It also has an option to include directories in the results.

    Parameters

    • directory: string

      The path to the directory to search.

    • ext: undefined | string | string[]

      A single file extension or an array of file extensions to filter by. If undefined, no extension filtering is applied.

    • OptionalshowDir: boolean = false

      If true, directories will be included in the results. Defaults to false.

    Returns Promise<undefined | Dirent[]>

    • A promise that resolves to an array of Dirent objects representing the found files.
    • Throws an error if there is a problem accessing the directory, except for 'ENOENT' errors.

    const fileOp = new AioFileOperation();
    // Search for all .txt files in the 'mydir' directory
    const txtFiles = await fileOp.search('mydir', '.txt');
    // Search for all .txt and .md files in the 'mydir' directory
    const txtAndMdFiles = await fileOp.search('mydir', ['.txt', '.md']);
    // Search for all files and directories in the 'mydir' directory
    const allFilesAndDirs = await fileOp.search('mydir', undefined, true);
  • Writes an array of strings to a file, overwriting any existing content.

    Parameters

    • outputFilePath: string

      The path to the file to be written.

    • content: string[]

      The array of strings to be written to the file. *

    Returns Promise<void>


    const fileOp = new AioFileOperation();
    const dataText = ['Line 1', 'Line 2', 'Line 3', ...]
    const lines = await fileOp.write('/path/to/large/file.txt', dataText);