Options
All
  • Public
  • Public/Protected
  • All
Menu

Class File

the File Class is used to help you work with files

Hierarchy

  • File

Index

Constructors

constructor

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

    the path you pass will passed to path.join

    example
    const file = new File(__dirname, "./some.txt");
    file.write("hello world");
    // ...

    Parameters

    • Rest ...args: string[]

      the paths

    Returns File

Properties

[Symbol.toStringTag]

[Symbol.toStringTag]: string = "File"

base

base: string

the name with the extension

Optional defaultContent

defaultContent: string | BufferType

the default content of the file written when you call .create()

directory

directory: string

the directory of the file

extension

extension: string

the extension of the file

Optional fd

fd: undefined | number

name

name: string

the name of the file without the extension

path

path: string

the path of the file

root

root: string

the root of the file

Optional validator

validator: undefined | ((this: File) => Error[] | void)

a function to validate the file content

Private Optional watcher

watcher: FSWatcher

the fs watcher

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

append

  • append some data to the file

    example
    file.append("hello").append("world").read() // => hello world

    Parameters

    Returns this

chmod

  • chmod(mode: number): this
  • changes the mode of the file

    warning

    chmod does not work on windows on deno and will produce an error

    example
    file.chmod(0o400 + 0o200 + 0o100); // gives the owner read, write and execute permissions

    Parameters

    • mode: number

      the mode

    Returns this

close

  • close(): this

copyTo

  • copyTo(destination: string, rename?: null | string, isRelative?: undefined | false | true): File
  • copy the file to the destination

    example
    // copy to absolute path
    const newFile = file.copyTo("/home/some_dir");
    // copy to a path relative to file path
    const newFile = file.copyTo("../some_dir", null, true);
    // copy and rename
    const newFile = file.copyTo("/home/some_dir", "newName.txt");
    const newFile = file.copyTo("../some_dir", "newName.txt", true);
    
    newFile.write("hello world");

    Parameters

    • destination: string

      the destination to copy the file to

    • Optional rename: null | string

      rename the file to another name

    • Optional isRelative: undefined | false | true

      resolves the destination path based the file path

    Returns File

create

  • create(): this
  • creates the file

    note

    defaultContent is written only if the file doesn't exist or file exists and empty

    Returns this

delete

  • delete(): void
  • delete the file

    example
    file.delete();
    fs.existsSync(file.path) // => false

    Returns void

exists

  • exists(): any

exits

  • exits(): any
  • deprecated

    returns true if the file exists

    Returns any

getIndex

  • getIndex(splitter: string, index: number): any
  • splits the file content and get the item with index passed

    example
    file.write("hello\nworld")
    file.getIndex("\n", 1) // => "world"

    Parameters

    • splitter: string

      the splitter string

    • index: number

      the index

    Returns any

getIndexBetween

  • getIndexBetween(splitter: string, start: number, end?: undefined | number): any
  • splits the file content and get the items between start and end (excluding end)

    example
    file.write("hello\nworld\nfoo\nbar")
    file.getIndexBetween("\n", 0, 3) // => ["hello", "world", "foo"]

    Parameters

    • splitter: string

      the splitter

    • start: number

      the start index

    • Optional end: undefined | number

      the end index

    Returns any

json

  • json<T>(): T
  • reads the file as json

    example
    file.write('{"hello": "world"}')
    file.json() // => { hello: "world" }

    Type parameters

    • T: obj<unknown> | unknown[]

    Returns T

link

  • link(newPath: string): this
  • Creates hard link newPath pointing to file's path

    Parameters

    • newPath: string

    Returns this

lstat

  • lstat(): any

moveTo

  • moveTo(destination: string, rename?: null | string, isRelative?: undefined | false | true): this
  • moves the file to destination

    example
    // move to absolute path
    file.moveTo("/home/some_dir");
    // move to a path relative to file path
    file.moveTo("./some_dir", null, true);
    // move and rename
    file.moveTo("/home/some_dir", "newName.txt");
    file.moveTo("./some_dir", "newName.txt", true);
    
    file.write("hello world");
    // ...

    Parameters

    • destination: string

      the destination to copy the file to

    • Optional rename: null | string

      rename the file to another name

    • Optional isRelative: undefined | false | true

      resolves the destination path based the file path

    Returns this

open

  • open(flags?: undefined | string): undefined | number

overwrite

  • overwrite(splitter: string, callback: (str: string, index: number) => string): this
  • overwrites the file by splitting it's content with the splitter passing the split data into a callback and replacing it with the result

    example
    file.write("hello\nworld");
    file.overwrite("\n", (str, i) => `${i}| ${str}`);
    file.read() // => "0| hello\n1| world"

    Parameters

    • splitter: string

      the string to split the file by

    • callback: (str: string, index: number) => string

      the callback

        • (str: string, index: number): string
        • Parameters

          • str: string
          • index: number

          Returns string

    Returns this

read

  • read(position?: undefined | number, length?: undefined | number, buffer?: BufferType | TypedArray | DataView, offset?: undefined | number): BufferType
  • reads the file

    note

    DO NOT pass buffer or offset on Deno will throw an error if passed

    example
    file.write("hello world");
    file.read() // => <Buffer 68 65 6c 6c 6f 20 77 6f 72 6c 64>
    // read file starting form index 6
    file.read(6).toString() // => "world"
    // read 3 bytes starting from index 6
    file.read(6, 3).toString() // => "wor"

    Parameters

    • Optional position: undefined | number

      Specifies where to begin reading from in the file

    • Optional length: undefined | number

      The number of bytes to read

    • Optional buffer: BufferType | TypedArray | DataView

      The buffer that the data will be written to

    • Optional offset: undefined | number

      The position in buffer to write the data to

    Returns BufferType

rename

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

    example
    file.rename("newName.txt");

    Parameters

    • newName: string

      the newName

    Returns this

splitBy

  • splitBy(separator: string | RegExp, limit?: undefined | number): any
  • split the file content into an array

    example
    file.write("hello\nworld");
    
    file.splitBy("\n"); // => ["hello", "world"]

    Parameters

    • separator: string | RegExp

      the string to split by

    • Optional limit: undefined | number

      A value used to limit the number of elements returned in the array

    Returns any

stat

  • stat(): any

symlink

  • symlink(newPath: string): this
  • Creates soft link newPath pointing to file's path

    Parameters

    • newPath: string

    Returns this

text

  • text(position?: undefined | number, length?: undefined | number): any
  • reads file as text

    example
    file.write("hello world");
    file.text() // => "hello world"
    // read file as text starting form index 6
    file.text(6) // => "world"
    // read 3 bytes as text starting from index 6
    file.text(6, 3) // => "wor"

    Parameters

    • Optional position: undefined | number

      Specifies where to begin reading from in the file

    • Optional length: undefined | number

      The number of bytes to read

    Returns any

truncate

  • truncate(offset?: undefined | number): this
  • clears file starting from index offset (0 by default)

    example
    file.write("hello world")
    file.truncate()
    file.read() // => ""
    file.write("hello world")
    file.truncate(6)
    file.read() // => "world"

    Parameters

    • Optional offset: undefined | number

      index where to start deleting

    Returns this

unwatch

  • unwatch(): this

valid

  • valid(): boolean
  • safer from validate (cause it will return true if there's no "validator")

    example
    // validate a json file
    file.validator = function () {
       JSON.parse(this.read.toString())
    }
    if (!file.valid()) console.log("file isn't valid");

    Returns boolean

validate

  • validates the file using the validator

    note

    if an error happens in file.validator file.validate will handel it an return an array of errors

    example
    // validate a json file
    file.validator = function () {
       JSON.parse(this.read.toString())
    }
    const errors = file.validate();
    if (errors.length) console.log("file isn't valid")

    Returns Error[]

watch

  • watch(listener?: undefined | ((this: File, e: string, stat?: Stats, path?: undefined | string) => void)): this
  • watches the file Notes: in deno stats is always undefined but path is defined

    example
    file.watch(function (e, stat) {
       console.log(`the file size is: ${stat.size}`);
    });

    Parameters

    • Optional listener: undefined | ((this: File, e: string, stat?: Stats, path?: undefined | string) => void)

      the function the will be called when the file changes

    Returns this

write

  • write(data: BufferType | string | obj<unknown>, position?: undefined | number, length?: undefined | number, offset?: undefined | number): this
  • write some data into the file

    note

    if you pass an object it will be automatically convert to json

    example
    // writing strings
    file.write("hello world");
    // writing buffer
    file.write(Buffer.from("hello world"));
    // writing js object (will be converted to json)
    file.write({ hello: "world" });
    // writing from position
    file.write("hello world");
    file.write("123", 2);
    file.read() // => "he123 world"
    // writing with position and length
    file.write("hello world");
    file.write("12345", 2, 3);
    file.read() // => "he123 world"
    // writing with position and length and offset
    file.write("hello world");
    file.write("12345", 2, 3, 2);
    file.read() // => "he345 world"

    Parameters

    • data: BufferType | string | obj<unknown>

      the data to write

    • Optional position: undefined | number

      Specifies where to begin writing to the file

    • Optional length: undefined | number

      The number of bytes to write

    • Optional offset: undefined | number

      The position in buffer to start writing from it to the file

    Returns this

Static tmpFile

  • create a file in the tmp directory with a random name very useful for testing

    Returns File

Generated using TypeDoc