rauricoste-file v1.0.0
Sumary
This project aims to simplify access to files in nodejs
. It uses
Promises
whenever possible.
It has been buit upon fs
.
It is supposed to work on Windows (you can use /
and they will be replaced
by \\
if you are a Windows user).
Install
Through npm :
npm install --save rauricoste-file
Usage
Builders
new File(path)
: create aFile
object corresponding to the thepath
.path
can be relative or asbolute.
Exemple : new File("src/File.js")
parent()
: returns aFile
object representing the parent file of the current file ornull
if there is no parent file.child(path)
: returns aFile
that has a relativepath
to the currentFile
.getRelativeFile(parentFile: File)
: returns aFile
that is a relative file regardingparentFile
.
Manipulation
moveTo(otherFile: File)
: move current file to anotherFile
. Returns aPromise
.copy(destination file: File)
: copy the file to thedestination path
. Returns aPromise
.delete()
: deletes the file. returns aPromise
.deleteChildren()
: deletes all this file's direct children. Returns aPromise
.mkdir()
: creates current file as a directory. Returns aPromise
.mkdirs()
: creates all this file's parents as directories and this file as a directory. Returns aPromise
.
Read / Write
read(encoding = "utf-8")
: returns aPromise
containing the content of the file as a String read with providedencoding
. Defaultencoding
isutf-8
.write(string or buffer)
: replace the file content with the data. Default encoding isutf-8
. Returns aPromise
.append(string or buffer)
: add data at the end of this file. Returns aPromise
.writeAtPosition(index: integer, string or buffer)
: writes data at positionindex
in the file. Previous data at this position will be erased. Returns aPromise
.readAtPosition(start: integer, size: integer)
: returns aPromise
containing data starting at positionstart
, of sizesize
.
Navigation
list()
: returns aPromise
containing the list of the children files (directory and simple files)listMatch(RegExp)
: returns aPromise
containing the list of the children files whose name matches theRegExp
crawl(testFunction: File => boolean (optionnal))
: returns aPromise
that contains all descendant files (children, grand-children, ...) of the current file. Returning files and traversed directories must matchtestFunction
which is a function that receives aFile
and must return aboolean
. IftestFunction
is not provided, all files are returned.
Informations
exists()
: returns aPromise
containing aboolean
that istrue
if the file exists.name()
: returns aString
that is the name of the file. The file does not have to exist.isFile()
: returnstrue
the.name()
of the file contains a.