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-fileUsage
Builders
new File(path): create aFileobject corresponding to the thepath.pathcan be relative or asbolute.
Exemple : new File("src/File.js")
parent(): returns aFileobject representing the parent file of the current file ornullif there is no parent file.child(path): returns aFilethat has a relativepathto the currentFile.getRelativeFile(parentFile: File): returns aFilethat 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 aPromisecontaining the content of the file as a String read with providedencoding. Defaultencodingisutf-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 positionindexin the file. Previous data at this position will be erased. Returns aPromise.readAtPosition(start: integer, size: integer): returns aPromisecontaining data starting at positionstart, of sizesize.
Navigation
list(): returns aPromisecontaining the list of the children files (directory and simple files)listMatch(RegExp): returns aPromisecontaining the list of the children files whose name matches theRegExpcrawl(testFunction: File => boolean (optionnal)): returns aPromisethat contains all descendant files (children, grand-children, ...) of the current file. Returning files and traversed directories must matchtestFunctionwhich is a function that receives aFileand must return aboolean. IftestFunctionis not provided, all files are returned.
Informations
exists(): returns aPromisecontaining abooleanthat istrueif the file exists.name(): returns aStringthat is the name of the file. The file does not have to exist.isFile(): returnstruethe.name()of the file contains a.