fso - FileSystemObject is a objective fs interface like Pathname(ruby).
npm install fso
node.js:
var fso = require('fso').default;
var FileSystemObject = require('fso').FileSystemObject;
ES2015:
import fso, {FileSystemObject} from 'fso';
API Documents (with type annotations)
var abc = new FileSystemObject("a", "b", "c");
abc.toString() === "a/b/c"
dir = fso.new('dir');
a = dir.new('a.txt');
a.writeFileSync('aaaaaa');
dir = fso.new('dir');
p = dir.parent();
dir = p.new('dir');
dir = fso.new('dir');
files = dir.childrenSync();
dir = fso.new('dir');
files = dir.childrenAllSync();
excepts = ["ignore_file", "path/to/ignore_directory"];
entries = target.filteredChildrenAllSync(excepts);
// Sync
excepts = (entry) => entry.isDirectorySync();
files = target.filteredChildrenAllSync(excepts); // childrenSync() minus directories
// Promise form condition also OK when Promise form call
excepts = (entry) => entry.isDirectory();
files = await target.filteredChildrenAll(excepts);
// callback form also OK when callback form call
excepts = (entry, callback) =>
entry.isDirectory(
(error, isDirectory) => callback(error, isDirectory)
);
target.filteredChildrenAll(excepts, (error, files) => console.log(files.toString()));
fooPath = fso.new('foo').path;
fooPath = fso.new('foo').toString();
Supports all the node.js 'fs' APIs that needs no first path argument.
method(...args, callback)
, methodSync(...args)
and promise = method(...args)
fs.truncateSync('/path/to/file', 0);
// is
file = fso.new('/path/to/file');
file.truncateSync(0);
fs.rename('/path/to/file', 'newfile', callback);
// is
file = fso.new('/path/to/file');
file.remane('newfile', callback);
fd = fs.openSync('/path/to/file');
fs.ftruncateSync(fd, 0);
fs.close(fd);
// is
file = fso.new('/path/to/file');
file.openSync();
file.ftruncateSync(0);
file.close();
fso.new('long/deep/path/to').mkdirAll().then(...);
dir.readdirAllSync()
// results
['a.txt', 'aa', 'aa/a.txt', 'aa/b', 'aa/b/c.txt']
await dir.rmAll('junk');
target.mergeDirectory(source);
copy:
target.rmAllSync();
target.mergeDirectorySync(source);
excepts = ["ignore_file", "path/to/ignore_directory"];
target.filteredMergeDirectorySync(source, excepts);
excepts = (entry) => entry.path === "ignore_file" || entry.path === "path/to/ignore_directory";
target.filteredMergeDirectorySync(source, excepts);
dir.new("foo/bar").isChildOf(dir)
dir.isParentOf(dir.new("foo/bar"))
fso.delimiter; // ":" or ";"
fso.sep; // "/" or "\\"
var entry = FileSystemObject.format({...});
var parsedObject = fso.new("a").parse();
fso.new("a").normalize(); // same as fso.new("a")
fso.new("a/b/c").basename(); // same as new FileSystemObject("c")
same as parent()
fso.new("a/b/c.txt").extname(); // ".txt"
fso.new("a/b/c.txt").isAbsolute() === true
fso.new("a/b/c").relative(fso.new("a/d")); // same as new FileSystemObject("../d")
fso.new("/a/b/c").relative("/a/d"); // same as new FileSystemObject("../d")
fso.new("a/b/c").resolve("/") // same as new FileSystemObject("/a/b/c")
This is released under Zlib License.