1.3.7 • Published 5 months ago
@hoge1e3/sfile v1.3.7
@hoge1e3/sfile
node.js fs Comprehensive File Wrapper
Build
npm install
npm run buildTest
npm run testIf "All test Passed." is shown, the test is succeeded.
Example
In node.js, first get FS object by await getNodeFS().
Then, FS.get() returns a File Object which can wraps fs library.
import {getNodeFS} from "@hoge1e3/sfile";
async function main() {
const FS=await getNodeFS();
var mydir=FS.get("./mydir/");// directory
mydir.mkdir();
var myfile=mydir.rel("myfile.txt"); // File /mydir/myfile.txt
myfile.text("my content"); // write file
console.log(myfile.text()); // read file
for (let f of mydir.listFiles()) { // list files in mydir
console.log(f.name());
}
}
main();Quick Reference of File Object
- File Object(class
SFile) can be get by callingFS.get(path). - In the methods listed below,
fanddis a File Object which represents a file and directory respectively.fdcan be either file or directory. - If filepath or filename represents directory,
/MUST be present at last of filepath or filename
f.text(str)writestrto the filef.text()returns file content of the file in stringf.obj(o)write objectoin JSON to the filef.obj()returns the file content as object. The content of the file should be written in JSONf.bytes(b)write ArrayBuffer/Buffer(node) to the filef.bytes()returns file content as ArrayBuffer(browser)/Buffer(node)f.getBytes({binType:ArrayBuffer})returns file content as ArrayBuffer(both in browser and node)d.each(func)iterates over the directory by passing each File Object tofuncd.recursive(func)iterates over all files in the directory and its subdirectory by passing each File Object tofuncd.recursive()returns Generator which iterates over all files in the directory and its subdirectory.d.listFiles()returns array of File Objects which represents all files in the directoryd.ls()returns array of names of all files in the directoryd.rel(relPath)returns new File Object specified by the relative pathfd.relPath(base)returns the relative path of the file from thebaseFile Object.fd.up()returns new File Object which is parent of the file/directory.fd.sibling(name)same asfd.up().rel(name).fd.path()returns the full path of the file/directory.fd.name()returns the name of the file/directory.fd.ext()returns the extention of the file/directory.fd.truncExt(e)returns the name removinge(wheneis omitted,eequals tofd.ext())f.lastUpdate()return the file's time stamp as the number corrensponding to the value of Date.getTimef.rm()removes the filed.rm()removes the directory if it is empty.d.rm({r:true})removes the directory and all subfiles/subdirectories.fd.exists()checks whether the file/directory exists.fd.isDir()checks whether the File Object is directory.f.copyFrom(src)copies from src(File Object) to the file.- If
srcis a directory. It is same ascp -R src/* f/*in unix command. NOTcp src/ f/.
- If
f.copyTo(dst)copies the file to dst(File Object).- It is same as
cp -R f/* dst/*, NOTcp f/ dst/.
- It is same as
f.moveFrom(src)moves from src(File Object) to the file.f.moveTo(dst)moves the file to dst(File Object).d.mkdir()creates the directory.
1.3.7
5 months ago
1.3.6
6 months ago
1.3.4
7 months ago
1.3.3
7 months ago
1.3.2
8 months ago
1.3.0
8 months ago
1.2.9
8 months ago
1.2.8
8 months ago
1.2.0
8 months ago
1.1.9
8 months ago
1.1.8
8 months ago
1.1.7
8 months ago
1.1.6
8 months ago
1.1.5
8 months ago
1.1.4
9 months ago
1.1.3
9 months ago
1.1.2
9 months ago
1.1.1
10 months ago
1.1.0
10 months ago
1.0.9
10 months ago
1.0.8
10 months ago
1.0.7
10 months ago
1.0.6
10 months ago
1.0.5
10 months ago
1.0.4
10 months ago
1.0.3
10 months ago
1.0.2
10 months ago
1.0.1
10 months ago
1.0.0
10 months ago