Class

File

File(path)

used to read and write files in json format
Constructor

# new File(path)

Parameters:
Name Type Description
path String the path of the json file you want to use
Properties:
Name Type Description
_file String the file to be used

View Source File/index.ts, line 5

  • module:fs
when the path given is a directory it will throw an Error
when the path given is a non existant file it will throw an Error
Example
//succeeds
const file = new File("./config.json")
const file = new File("./.configrc") //.configrc is in json format
// fails
const file = new File("./")
// fails
const file = new File("./doesnt-exist.json")

Requires

  • module:fs

Methods

# read() → {Object}

reads and converts the file contents to json

View Source File/index.ts, line 32

if JSON is unable to parse the file an Error is thrown
if file is not readable(permissions error) an Error is thrown
- the file contents converted to json
Object
Example
const file = new File("./.configrc") //.configrc is in json format
const content = file.read()
console.log(content)
// {
//		"sort":"asc",
//		"date":"09/02/1998",
// }

# write(newData) → {Void}

writes the given content to a file after convertion
Parameters:
Name Type Description
newData String | Object the new data to be written

View Source File/index.ts, line 57

Void
Example
const file = new File("./.configrc")
file.write({
		"sort":"asc",
		"date":"09/02/1998",
})