ray-fs v2.2.1
ray-fs
A library with easy to use, mind blowing file-system tools for NodeJS. Based on FS! with chainable methods, sync first approach, and amazing UX.
Installation
To install with NPM use:
npm i ray-fs --saveThen attach it to your file using:
const fs = require('ray-fs');
// use code hereUsage
The ray-fs library has the following methods and properties: 1. .value: To return the output of the previous method in the chain.
For example:-
fs
.lsDir() // gets the list of Directories
.value // returns the list of Directories- .logVal(): To
console.log()the output of the previous method in chain. For example:-
fs
.lsFile() // gets the list of Files
.logVal() // logs the list of Files.cd(dirName): To change the "working-directory" to
dirName. A virtual version of shell'scd, it does not allow a movement to directories deeper then depth=1..ls(): To get a list of all the Files in the "working-directory".
.exists(fileURL): To check if a FS item (like a File) exists at
fileURL..isFile(fileURL): To check if
fileURLis a File or not! (a Directory maybe).isDir(dirURL): To check if
dirURLis a Direcotry or not!.lsMatches(filterFunction): To get a list of all items in the "working-directory" that matches
filterFunction.
fs
// To get a list of all items in the "working-directory" that have names ending in ".js"
.lsMatches(item => /.js$/.test(item))
.value // To return that listBetter written as:
fs
// To get a list of all Javascript Files in the "working-directory"
.lsMatches(item => fs.isFile(item).value && /.js$/.test(item))
.value // To return that list.lsDir(): To get a list of all Directories in the "working-directory".
.lsFile(): To get a list of all Files in the "working-directory".
.version(): To get the current version of
ray-fs..write(fileURL, content): To write the
contentinto the file atfileURL..read(fileURL).value: To read the content of the file at
fileURL. The path used must be relative from the root directory of your project, e.g. "./README.md"..writeJSON(fileURL, json): To write the
jsoninto the file atfileURL..readJSON(fileURL).value: To read/import the JSON content of the file at
fileURL..updateJSON(file, callback): TO update the json object in a .json file. For example:
const fs = require('ray-fs');
const file = "june.json";
fs
.updateJSON(file, (json) => {
json.oldProp = "replace old prop's value with new value";
json.newProp = "adds a new prop with a new value";
return json;
});.readArray(fileURL).value: To read/import the content of the file at
fileURLas an Array of lines of content..push(fileURL, content): To add the
contentbelow the existing content of the file atfileURL..rm(url): To delete the item at
url..cp(url, destinationURL): To copy the item at
urltodestinationURL..mv(url, destinationURL): To move the item at
urltodestinationURL..mkdir(dirName): To create a new Directory named
dirName..logDir(): To log the name of the "working-directory".
.validFileName(fileName): To check if a file name complies with the file naming rules. (beta version)
.validDirName(dirName): To check if a directory name complies with the naming rules. (beta version)
.stream(responseBody, filePath, errorCallback, sucessCallback): To pipe a
response.bodyto afilePath..initDir(dirName): To check if a directory exists, if no then create it.
.initDirs(dirName1, dirName2, ...): To check if all of the provided directories exist, if any don't then create them.
.initFile(fileName, fileContent): To check if a file exists, if no then create it, then add the provided
fileContentto it. ThefileContentparemeter can be passed a JSONobjector aString.
Note: The documentation is incomplete, and will be completed soon.
Chaining Functions
Chain functions to make code more readable!
const fs = require('ray-fs');
fs
.mkdir('myProject')
.cd('myProject')
.write('myFile.txt', 'Hello World!')
.cp('myFile.txt', 'myGreeting.txt')
.read('myFile.txt')
.logVal()
.read('myGreeting.txt')
.logVal()The .value prop and the .
Tips
- Absolute URL's aren't allowed, instead use .cd() to change the "working-directory" first!
- Chain for the betterment of code, don't chain where it dosen't make sense.
- This library is not meant to replace
fs, it is meant to be an alternative way to write code that can also be written withfs. This library will allow you to write significaltly shorter and clearer code.
Comming Soon
- Async methods.
- color logs.