Class FileUtils

A utility class for file system operations.

Methods

  • Gets the file name from a path.

    Parameters

    • filePath: string

      The file path.

    • withExtension: boolean = false

      Whether to include the file extension (default: false).

    Returns string

    The file name.

    import {FileUtils} from 'nodejs-shared';

    FileUtils.basename('path/to/file.txt');// file
    FileUtils.basename('path/to/file.txt', true);// file.txt
  • Changes the owner and/or group of a file or directory.

    Parameters

    • filePath: string

      The path of the file or directory.

    • username: string

      The new owner's username.

    • OptionalgroupName: string

      The new group's name. If undefined, the group will not be changed.

    Returns void

    If an error occurs during ownership change or user/group lookup.

  • Changes file or directory permissions.

    Parameters

    • filePath: string

      The path of the file or directory.

    • mode: number = 0o755

      The new permissions (default: 0o755).

    Returns void

    import {FileUtils} from 'nodejs-shared';

    FileUtils.changePermission('path/to/file.txt', 0o755);

    If an error occurs during permission change.

  • Copies a file or directory recursively.

    Parameters

    • sourcePath: string

      The path of the source file or directory.

    • destinationPath: string

      The path of the destination.

    Returns void

    If an error occurs during copying.

  • Deletes a directory recursively.

    Parameters

    • directoryPath: string

      The path of the directory to delete.

    Returns void

    import {FileUtils} from 'nodejs-shared';

    FileUtils.deleteDirectory('path/to/my/dir');

    If an error occurs during directory deletion.

  • Deletes a file.

    Parameters

    • filePath: string

      The path of the file to delete.

    Returns void

    import {FileUtils} from 'nodejs-shared';

    FileUtils.deleteFile('path/to/file.txt');

    If an error occurs during file deletion.

  • Checks if a file or directory exists.

    Parameters

    • filePath: string

      The path of the file or directory.

    Returns boolean

    true if the file or directory exists, false otherwise.

    import {FileUtils} from 'nodejs-shared';

    if (FileUtils.exists('path/to/file.txt'))
    ;
  • Gets the file extension.

    Parameters

    • filePath: string

      The path of the file.

    Returns undefined | string

    The file extension, or undefined if there is no extension.

    import {FileUtils} from 'nodejs-shared';

    FileUtils.getExtension('path/to/file.txt');// txt
  • Gets the file modification time in Unix time.

    Parameters

    • filePath: string

      The path of the file.

    Returns number

    The last modified Unix time of the file.

    import {FileUtils} from 'nodejs-shared';

    FileUtils.getFilemtime('path/to/file.txt');

    If an error occurs during stat retrieval.

  • Gets the path to the temporary directory. Uses the TMPDIR environment variable if present, otherwise uses the system's temporary directory.

    Returns string

    The path to the temporary directory.

    import {FileUtils} from 'nodejs-shared';

    FileUtils.getTmpDirectory();// /tmp
  • Gets a temporary file path.

    Parameters

    • Optionalextension: string

      The file extension (optional).

    Returns string

    The temporary file path.

    import {FileUtils} from 'nodejs-shared';

    // Get a temporary file path with a random name.
    FileUtils.getTmpPath();// /tmp/a1b2c3d4e5f6

    // Get a temporary file path with a specified extension.
    FileUtils.getTmpPath('txt');// /tmp/f7g8h9i0j1k2.txt
  • Finds files matching a pattern using the glob npm package internally.

    Parameters

    • pattern: string

      The glob pattern to match.

    • options: GlobOptions = {}

      Optional glob options (see glob.GlobOptions).

    Returns string[]

    An array of absolute paths of the matched files.

    import {FileUtils} from 'nodejs-shared';

    // Find all files in the specified directory.
    FileUtils.glob('path/to/*.*');
    // [
    // 'path/to/file.txt',
    // 'path/to/another-file.txt',
    // ]

    // Find all files, including those in subdirectories.
    FileUtils.glob('path/to/**/*.*');
    // [
    // 'path/to/my/dir/file.txt',
    // 'path/to/file.txt',
    // ]

    // Find only files with the .jpg extension.
    FileUtils.glob('path/to/**/*.jpg');
    // [
    // 'path/to/my/dir/image.jpg',
    // 'path/to/image.jpg',
    // ]

    // Find only files with the .png or .jpg extension.
    FileUtils.glob('path/to/**/*.+(png|jpg)'); // Note the corrected path
    // [
    // 'path/to/my/dir/image.jpg',
    // 'path/to/my/dir/image.png',
    // 'path/to/image.jpg',
    // ]
  • Checks if a string is a base64 encoded string.

    Parameters

    • str: string

      The string to check.

    Returns boolean

    true if the string is base64 encoded, false otherwise.

    import {FileUtils} from 'nodejs-shared';

    if (FileUtils.isBase64('iVBORw0KGgoAAAANSUhE'))
    ;
  • Checks if a path is a directory.

    Parameters

    • directoryPath: string

      The path to check.

    Returns boolean

    true if the path is a directory, false otherwise.

    import {FileUtils} from 'nodejs-shared';

    if (FileUtils.isDirectory('path/to/my/dir'))
    ;

    If the path does not exist.

  • Checks if a path is a file.

    Parameters

    • filePath: string

      The path to check.

    Returns boolean

    true if the path is a file, false otherwise.

    import {FileUtils} from 'nodejs-shared';

    if (FileUtils.isFile('path/to/file.txt'))
    ;
  • Checks if a string is a valid file system path. This method checks for invalid characters and empty strings after trimming whitespace. It does not check if the path actually exists.

    Parameters

    • str: string

      The string to check.

    Returns boolean

    true if the string is a valid file system path, false otherwise.

    import {FileUtils} from 'nodejs-shared';

    if (FileUtils.isPath('path/to/file.txt'))
    ;
  • Makes a directory recursively.

    Parameters

    • directoryPath: string

      The path of the directory to create.

    • options: MakeDirectoryOptions = {}

      Options for directory creation.

    Returns void

    import {FileUtils} from 'nodejs-shared';

    // Creates a directory.
    FileUtils.makeDirectory('path/to/my/dir');

    // Creates a directory with file system options.
    FileUtils.makeDirectory('path/to/my/dir', {mode: 0o644, owner: {username: 'nginx', groupName: 'nginx'}});

    If an error occurs during directory creation or changing ownership.

  • Creates a temporary directory. Uses the TMPDIR environment variable if present, otherwise uses the system's temporary directory.

    Parameters

    Returns string

    The path of the created temporary directory.

    import {FileUtils} from 'nodejs-shared';

    FileUtils.makeTmpDirectory();// =>/tmp/3msbsb19l6c0a8pi/

    If an error occurs during directory creation.

  • Reads the content of a file as a base64 string.

    Parameters

    • filePath: string

      The path of the file to read.

    Returns string

    The content of the file as a base64 string.

    import {FileUtils} from 'nodejs-shared';

    const base64 = FileUtils.readAsBase64('path/to/image.jpg');

    If an error occurs during file reading.

  • Reads the content of a media file and returns it as a data URL string.

    Parameters

    • filePath: string

      The path of the media file to read.

    Returns string

    The data URL string.

    import {FileUtils} from 'nodejs-shared';

    const dataUrl = FileUtils.readAsDataUrl('path/to/image.jpg');

    If an error occurs during file reading or data URL generation.

  • Reads the content of a JSON file and parses it as an object.

    Type Parameters

    • T

    Parameters

    • filePath: string

      The path of the JSON file to read.

    Returns T

    The parsed JSON object.

    import {FileUtils} from 'nodejs-shared';

    const object = FileUtils.readAsJson('path/to/data.json');

    If an error occurs during file reading or JSON parsing.

  • Reads the content of a file as a string.

    Parameters

    • filePath: string

      The path of the file to read.

    Returns string

    The content of the file as a string.

    import {FileUtils} from 'nodejs-shared';

    const str = FileUtils.readAsString('path/to/file.text');

    If an error occurs during file reading.

  • Renames a file or directory.

    Parameters

    • sourcePath: string

      The original path of the file or directory.

    • destinationPath: string

      The new path of the file or directory.

    Returns void

    import {FileUtils} from 'nodejs-shared';

    FileUtils.rename('path/to/file.txt', 'path/to/file2.txt');
    FileUtils.rename('path/to/my/dir, 'path/to/my/dir2');

    If an error occurs during renaming.

  • Writes content to a file. Creates the directory if it doesn't exist.

    Parameters

    • filePath: string

      The path of the file to write to.

    • content: string | Buffer = ''

      The content to write to the file. Defaults to an empty string.

    • options: WriteOptions = {}

      Optional settings for file writing, including standard fs.WriteFileOptions.

    Returns void

    // Write a string to a file.
    FileUtils.write('path/to/another-file.txt', 'Hello, world!');

    // Write a Buffer to a file.
    const buffer = Buffer.from('Hello, world!');
    FileUtils.write('path/to/file.txt', buffer);

    // Write with file system options.
    FileUtils.write('path/to/file.txt', buffer, {mode: 0o644, owner: {username: 'nginx', groupName: 'nginx'}});

    If an error occurs during file writing, directory creation, or permission/owner changes.