Options
All
  • Public
  • Public/Protected
  • All
Menu

Module node

Index

Promise Functions

Const appendFile

  • appendFile(filePath: string): (data: string) => Promise<void>
  • A side-effectful file operation that serves as a less destructive alternative to writeFile. I.e., Appends (rather than overwriting) data to existing data of a file at filePath (i.e., creates the file if it doesn't already exist) to finally return a void Promise if successful, else rejects.

    memberof

    node

    since

    0.1.0

    see

    readFile writeFile

    example
    const existingFile = 'path/to/existing/file';
    await readFile(existingFile); //=> 'foo'
    await appendFile(existingFile)('bar'); //=> void (file is appended to)
    await readFile(existingFile); //=> 'foobar'
    
    const nonExistantFile = 'path/to/non-existant/file';
    await readFile(nonExistantFile); //=> 'foo'
    await appendFile(nonExistantFile)('bar'); //=> void (file is created & appended to)
    await readFile(nonExistantFile); //=> 'foo'
    

    Parameters

    • filePath: string

      string path of destination file to append to.

    Returns (data: string) => Promise<void>

    Finally returns a void Promise if successful after writing data to a file at filePath, else rejects.

      • (data: string): Promise<void>
      • Parameters

        • data: string

          string data to append.

        Returns Promise<void>

Const createDir

  • createDir(filePath: string): Promise<void>
  • A side-effectful file operation that returns a void Promise if successful after creating a previously non-existant directory at filePath, else rejects.

    memberof

    node

    since

    0.1.0

    see

    ensureDirExists

    example
    const nonExistantDir = 'path/to/non-existant/dir';
    await pathExists(nonExistantDir); //=> false
    await createDir(nonExistantDir); //=> void
    await pathExists(nonExistantDir); //=> true
    
    const existingDir = 'path/to/existing/file';
    await pathExists(existingDir); //=> true
    await createDir(existingDir); //=> Error
    await pathExists(existingDir); //=> true
    

    Parameters

    • filePath: string

      string path of directory to create.

    Returns Promise<void>

    A void Promise if successful after creating a previously non-existant directory at filePath, else rejects.

Const deleteFile

  • deleteFile(filePath: string): Promise<void>
  • A side-effectful file operation that returns a void Promise if successful after deleting a file at filePath, else rejects.

    memberof

    node

    since

    0.1.0

    see

    appendFile pathExists readFile writeFile

    example
    const existingFile = 'path/to/existing/file';
    await fileExists(existingFile); //=> true
    await deleteFile(existingFile)('foo'); //=> void
    await fileExists(existingFile); //=> false
    
    const nonExistantFile = 'path/to/non-existant/file';
    await fileExists(existingFile); //=> false
    await deleteFile(nonExistantFile); //=> Error
    

    Parameters

    • filePath: string

      string path of file to delete.

    Returns Promise<void>

    A void Promise if successful after deleting a file at filePath, else rejects

Const dirAllNames

  • dirAllNames(entryType?: DirEntryType): (filePath: string) => Promise<string[]>
  • Finally returns a Promise fulfilling to a string array of names for all files with a type matching entryType in an existing directory at filePath, else rejects.

    memberof

    node

    since

    0.1.0

    see

    dirNames dirFilenames isDir isFile

    example
    await dirAllNames()('path/to/non-existing/dir'); //=> Error
    await dirAllNames()('path/to/existing/dir'); //=> `string` array of names
    await dirAllNames(DirEntryType.dir)('path/to/existing/dir'); //=> `string` array of dir names
    await dirAllNames(DirEntryType.file)('path/to/existing/dir'); //=> `string` array of file names
    

    Parameters

    • entryType: DirEntryType = ...

      DirEntryType indicating the the entry type (e.g., directory, file , etc.) of names that should be returned (optional, defaults to DirEntryType.all).

    Returns (filePath: string) => Promise<string[]>

    Finally returns a Promise fulfilling to a string array of names for all files with a type matching entryType in the existing directory at filePath, else rejects.

      • (filePath: string): Promise<string[]>
      • Parameters

        • filePath: string

          string path of directory test.

        Returns Promise<string[]>

Const dirFilenames

  • dirFilenames(filePath: string): Promise<string[]>
  • File-specific alternative to dirAllNames (which returns file and directory names). I.e., returns a Promise fulfilling to a string array of names for all non-directory files in the existing directory at filePath, else rejects.

    memberof

    node

    since

    0.1.0

    see

    dirAllNames dirNames isFile

    example
    await dirFilenames('path/to/existing/dir'); //=> `string` array of filenames
    await dirFilenames('path/to/non-existing/dir'); //=> Error
    

    Parameters

    • filePath: string

      string path of directory test.

    Returns Promise<string[]>

    a Promise fulfilling to a string array of names for all files in the existing directory at filePath, else rejects.

Const dirNames

  • dirNames(filePath: string): Promise<string[]>
  • Directory-specific alternative to dirAllNames (which returns file and directory names). I.e., returns a Promise fulfilling to a string array of names for all directory files in the existing directory at filePath, else rejects.

    memberof

    node

    since

    0.1.0

    see

    dirAllNames dirFilenames isDir

    example
    await dirNames('path/to/existing/dir'); //=> `string` array of filenames
    await dirNames('path/to/non-existing/dir'); //=> Error
    

    Parameters

    • filePath: string

      string path of directory test.

    Returns Promise<string[]>

    a Promise fulfilling to a string array of names for all files in the existing directory at filePath, else rejects.

Const ensureDirExists

  • ensureDirExists(filePath: string): Promise<void>
  • A potentially side-effectful file operation that serves as a convenient alternative to createDir to handle potentially pre-existing directories by leaving the existing directory & its content unchanged. I.e., finally returns a void Promise if successful after creating a directory at filePath (or performs no operation if the directory already exists), else rejects.

    memberof

    node

    since

    0.1.0

    see

    createDir

    example
    const nonExistantDir = 'path/to/non-existant/dir';
    await pathExists(nonExistantDir); //=> false
    await createDir(nonExistantDir); //=> void (identical to `createDir`)
    await pathExists(nonExistantDir); //=> true
    
    const existingDir = 'path/to/existing/dir';
    await pathExists(existingDir); //=> true
    await createDir(existingDir); //=> void (`existingDir` & its contents remain unchanged)
    await pathExists(existingDir); //=> true
    

    Parameters

    • filePath: string

      string path of directory to create.

    Returns Promise<void>

    Finally returns a void Promise if successful after creating a directory at filePath, else rejects.

Const isDir

  • isDir(filePath: string): Promise<boolean>
  • A directory-specific alternative to pathExists (which returns true for existing file and directory paths). I.e., Returns a Promise fulfilling to true if a directory file exists at filePath, else fulfills to false.

    memberof

    node

    since

    0.1.0

    see

    isFile pathExists

    example
    // Files.
    await isDir('path/to/existing/file'); //=> false
    await isDir('path/to/non-existing/file'); //=> false
    
    // Directories.
    await isDir('path/to/existing/dir'); //=> true
    await isDir('path/to/non-existing/dir'); //=> false
    

    Parameters

    • filePath: string

      string path of file test.

    Returns Promise<boolean>

    A Promise fulfilling to true if a directory file exists at filePath, else fulfills to false.

Const isFile

  • isFile(filePath: string): Promise<boolean>
  • A file-specific alternative to pathExists (which returns true for existing file and directory paths). I.e., Returns a Promise fulfilling to true if a non-directory file exists at filePath, else fulfills to false.

    memberof

    node

    since

    0.1.0

    see

    isDir pathExists

    example
    // Files.
    await isFile('path/to/existing/file'); //=> true
    await isFile('path/to/non-existing/file'); //=> false
    
    // Directories.
    await isFile('path/to/existing/dir'); //=> false
    await isFile('path/to/non-existing/dir'); //=> false
    

    Parameters

    • filePath: string

      string path of file test.

    Returns Promise<boolean>

    A Promise resolving to true if a non-directory file exists at filePath.

Const pathDirname

  • pathDirname(filePath?: string): undefined | string
  • Returns the directory name of filePath if found, else returns undefined.

    memberof

    node

    since

    0.1.0

    example
    pathDirname()(); //=> undefined
    pathDirname()(''); //=> undefined
    pathDirname()('foo\\name-of-dir\\file'); //=> 'name-of-dir'
    pathDirname('/')('foo/name-of-dir\\file'); //=> 'name-of-dir'
    pathDirname('/')('./../foo\\name-of-dir/file'); //=> 'name-of-dir'
    

    Parameters

    • filePath: string = ''

      string path of file to test (optional, defaults to '').

    Returns undefined | string

    Returns the directory name of filePath if found, else returns undefined.

Const pathExists

  • pathExists(filePath: string): Promise<boolean>
  • Returns a Promise-contained true if a file or directory at filePath exists, else returns false.

    memberof

    node

    since

    0.1.0

    see

    deleteFile readFile

    example
    // Files.
    await pathExists('path/to/existing/file'); //=> true
    await pathExists('path/to/non-existant/file'); //=> false
    
    // Directories.
    await pathExists('path/to/existing/dir'); //=> true
    await pathExists('path/to/non-existant/dir'); //=> false
    

    Parameters

    • filePath: string

      string path of file to test.

    Returns Promise<boolean>

    Promise-contained true if a file at filePath exists, else returns false.

Const readFile

  • readFile(filePath: string): Promise<string>
  • Returns Promise-contained string content if a file at filePath exists, else rejects.

    memberof

    node

    since

    0.1.0

    see

    pathExists writeFile

    example
    await readFile('path/to/existing/file'); //=> v where v is file's content
    await readFile('path/to/non-existant/file'); //=> Error
    

    Parameters

    • filePath: string

      string path of file to read.

    Returns Promise<string>

    Promise-contained string content a file at filePath exists, else rejects.

Const readJson

  • readJson(filePath: string): Promise<Record<string, any>>
  • Convenience function similar to readFile but focuses on reading JSON file content before returning Promise-contained object if a JSON file at filePath exists, else rejects.

    memberof

    node

    since

    1.0.0

    see

    readFile objectToString

    example
    await readJson('path/to/existing/file.json'); //=> v where v is file's JSON content as an object
    await readJson('path/to/existing/file.txt'); //=> Error
    await readJson('path/to/non-existant/file'); //=> Error
    

    Parameters

    • filePath: string

      string path of JSON file to read.

    Returns Promise<Record<string, any>>

    Promise-contained string content a file at filePath exists, else rejects.

Const writeFile

  • writeFile(filePath: string): (data: string) => Promise<void>
  • A side-effectful file operation that finally returns a void Promise if successful after writing (i.e., overwrites all existing file data) data to a file at filePath (i.e., creates the file if it doesn't already exist prior to writing), else rejects.

    memberof

    node

    since

    0.1.0

    see

    appendFile readFile

    example
    const existingFile = 'path/to/existing/file';
    await writeFile(existingFile)('foo'); //=> void (file is written to)
    await readFile(existingFile); //=> 'foo'
    
    const nonExistantFile = 'path/to/non-existant/file';
    await writeFile(nonExistantFile)('foo'); //=> void (file is created & written to)
    await readFile(nonExistantFile); //=> 'foo'
    

    Parameters

    • filePath: string

      string path of destination file to write to.

    Returns (data: string) => Promise<void>

    Finally returns a void Promise if successful after writing data to a file at filePath, else rejects.

      • (data: string): Promise<void>
      • Parameters

        • data: string

          string data to write.

        Returns Promise<void>

Const writeJson

  • writeJson(filePath: string): (data: Record<string, any>) => Promise<void>
  • A side-effectful convenience function similar to writeFile but focuses on writing a data object to a JSON file at filePath before returning a void Promise if successful, else rejects.

    memberof

    node

    since

    1.0.0

    see

    writeFile objectToString readJson

    example
    const existingJsonFile = 'path/to/existing/file.json';
    await writeJson(existingJsonFile)({ foo: 'bar'}); //=> void (file is written to)
    await readJson(existingJsonFile); //=> { foo: 'bar'}
    
    const nonExistantJsonFile = 'path/to/non-existant/file.json';
    await writeJson(nonExistantJsonFile)({ foo: 'bar'}); //=> void (file is created & written to)
    await readJson(nonExistantJsonFile); //=> { foo: 'bar'}
    

    Parameters

    • filePath: string

      string path of JSON file to write.

    Returns (data: Record<string, any>) => Promise<void>

    Finally returns a void Promise if successful after writing data to a file at filePath, else rejects.

      • (data: Record<string, any>): Promise<void>
      • Parameters

        • data: Record<string, any>

          object to write.

        Returns Promise<void>

String Functions

Const pathJoin

  • pathJoin(filePath: string): (...names: string[]) => string
  • Finally returns a string containing filePath and all names delimited by the platform-specific path separator (\\ on Windows or / on Unix).

    memberof

    node

    since

    0.1.0

    see

    pathDirname

    example
    pathJoin('')(); //=> ''
    pathJoin('c:\\foo')('bar', 'baz.qux'); //=> 'c:\\foo\\bar\\baz.qux' (on Windows)
    pathJoin('c:\\b/foo')('bar', 'baz.qux'); //=> 'c:\\b\\foo\\bar\\baz.qux' (on Windows)
    pathJoin('./../foo')('bar', 'baz.qux'); //=> '\\..\\foo\\bar\\baz.qux' (on Windows)
    

    Parameters

    • filePath: string

      string path of file to join onto (optional, defaults to '').

    Returns (...names: string[]) => string

    Finally returns a string containing filePath and all names delimited by the platform-specific path separator.

      • (...names: string[]): string
      • Parameters

        • Rest ...names: string[]

          string array of names to join.

        Returns string