Options
All
  • Public
  • Public/Protected
  • All
Menu

Class Dir

the Dir Class is used to help you work with folders

Hierarchy

  • Dir

Index

Constructors

constructor

  • new Dir(...args: string[]): Dir
  • note

    the path you pass will passed to path.join

    Parameters

    • Rest ...args: string[]

      the paths the Dir Class constructor

    Returns Dir

Properties

[Symbol.toStringTag]

[Symbol.toStringTag]: string = "Dir"

name

name: string

the name of the directory

parentDirectory

parentDirectory: string

the parent directory of the directory

path

path: string

the path of the directory

root

root: string

the root of the directory

Private Optional watcher

watcher: FSWatcher

Accessors

createdAt

  • get createdAt(): any
  • The timestamp indicating when the file have been created

    Returns any

lastAccessed

  • get lastAccessed(): any
  • The timestamp indicating the last time this file was accessed.

    Returns any

lastChanged

  • get lastChanged(): any
  • The timestamp indicating the last time this file status was changed.

    Returns any

lastModified

  • get lastModified(): any
  • The timestamp indicating the last time this file was modified.

    Returns any

size

  • get size(): any

Methods

copyTo

  • copyTo(destination: string, rename?: null | string, isRelative?: undefined | false | true): Dir
  • copy the directory to another location

    example
    // copy directory to "/home/some_dir"
    dir.copyTo("/home/some_dir")
    // copy directory to "/home/some_dir" an rename the directory's copy name to "new-name"
    dir.copyTo("/home/some_dir", "new_name")
    // copy directory to "../some_dir" (path is resolved passed on folder's location)
    dir.copyTo("../some_dir", null, true)

    Parameters

    • destination: string

      the destination folder

    • Optional rename: null | string

      rename the folder copy

    • Optional isRelative: undefined | false | true

      if true resolves the destination path based on the dir path

    Returns Dir

create

  • create(): this
  • creates the directory (if it doesn't exits)

    example
    dir.create();

    Returns this

createDir

  • createDir(dirname: string, createParents?: undefined | false | true): Dir
  • create a directory inside the directory

    example
    const subDir = dir.createDir("hello");
    subDir.createFile("hello_world.txt");
    // ...
    const subDir2 = dir.createDir("foo/bar/hello", true);
    subDir2.createFile("hello_world.txt");
    // ...

    Parameters

    • dirname: string

      the name of the directory

    • Optional createParents: undefined | false | true

      create parent directories if doesn't exits

    Returns Dir

createFile

  • createFile(filename: string, createParents?: undefined | false | true): any
  • create a file inside the directory

    note

    use ONLY forward slash of filename

    example
    const file = dir.createFile("hello_world.txt");
    file.write("hello world");
    // ...
    const file2 = dir.createFile("some_dir/hello_world.txt", true);
    file2.write("hello world");
    //...

    Parameters

    • filename: string

      the file you want to create

    • Optional createParents: undefined | false | true

      create parent directories if doesn't exits

    Returns any

delete

  • delete(): this
  • deletes the directory even if it's not empty

    Returns this

deleteMatchDir

  • deleteMatchDir(regex: RegExp): this
  • deletes every directory the matches the regex in the dir and sub dir

    example
    dir.deleteMatchDir(/node_modules/) // delete every node_modules in the dir

    Parameters

    • regex: RegExp

      the regex

    Returns this

deleteMatchFile

  • deleteMatchFile(regex: RegExp): this
  • delete every file the matches the regex passed in

    example
    dir.deleteMatchFile(/.*\.js$/); // delete every js file

    Parameters

    • regex: RegExp

      the regex

    Returns this

deleteMath

  • deleteMath(regex: RegExp): this
  • delete every thing (where it's a file or a folder) that matches the regex passed in

    example
    dir.deleteMatch(/some/); // delete every thing called some

    Parameters

    • regex: RegExp

      the regex

    Returns this

exists

  • exists(): any
  • returns true if the directory exists

    Returns any

exits

  • exits(): any
  • deprecated

    returns true if the directory exists

    Returns any

forEach

  • loops through every thing inside the directory

    example
    // console logs every thing's path (file or dir)
    dir.forEach(thing => console.log(thing.path));
    // console logs every thing's path (file or dir) recursively
    dir.forEach(thing => console.log(thing.path), { recursive: true });

    Parameters

    Returns this

forEachDir

  • loops through every directory inside the directory

    example
    // console logs every directory's path
    dir.forEachDir(dir => console.log(dir.path))
    // console logs every directory's path
    dir.forEachDir(dir => console.log(dir.path), { recursive: true })

    Parameters

    • callback: (dir: Dir) => void

      the callback

        • (dir: Dir): void
        • Parameters

          Returns void

    • Default value options: DirForeachOptions = {}

      options

    Returns this

forEachFile

  • loops through every file inside the directory

    example
    // console logs every file's path
    dir.forEachFile(file => console.log(file.path));
    // console logs every file's path
    dir.forEachFile(file => console.log(file.path), { recursive: true });

    Parameters

    • callback: (file: File) => void

      the callback

        • (file: File): void
        • Parameters

          Returns void

    • Default value options: DirForeachOptions = {}

      options

    Returns this

getDir

  • getDir(path: string): Dir
  • gets a folder inside the directory

    example
    myDir.getDir("myChildDir").read() //...

    Parameters

    • path: string

    Returns Dir

getFile

  • getFile(path: string): any
  • gets a file inside the directory

    example
    myDir.getFile("myFile.txt").read() //...

    Parameters

    • path: string

    Returns any

lstat

  • lstat(): any

moveTo

  • moveTo(destination: string, rename?: null | string, isRelative?: undefined | false | true): this
  • move the directory to another location

    example
    // move directory to "/home/some_dir"
    dir.moveTo("/home/some_dir")
    // move directory to "/home/some_dir" an rename the directory to "new-name"
    dir.moveTo("/home/some_dir", "new_name")
    // move directory to "../some_dir" (path is resolved passed on folder's location)
    dir.moveTo("../some_dir", null, true)

    Parameters

    • destination: string

      the destination folder

    • Optional rename: null | string

      rename the folder

    • Optional isRelative: undefined | false | true

      if true resolves the destination path based on the dir path

    Returns this

read

  • read(): any
  • reads the directory

    example
    console.log(dir.read()) // => ["hello_world.txt", "file2.txt"]

    Returns any

readResolve

  • readResolve(): any
  • reads te directory and convert it to File and Dir objects

    example
    dir.readResolve().forEach(console.log);

    Returns any

rename

  • rename(newName: string): this
  • rename the directory

    example
    dir.rename("newName");

    Parameters

    • newName: string

      the newName

    Returns this

stat

  • stat(): any

unwatch

  • unwatch(): this

watch

  • watches the directory

    example
    dir.watch({}, (e, path) => {
       if (e === "update") console.log(`${path} have been updated`);
       if(e === "remove") console.log(`${path} have been removed`);
    })

    Parameters

    Returns any

Static tmpDir

  • tmpDir(): Dir
  • create a dir in the tmp directory

    example
    const dir = Dir.tmpDir();
    console.log(dir.path) // => "'/tmp/tmp-132576-Rlxque0XmU45'"

    Returns Dir

Generated using TypeDoc