Narazaka / fso

fso - FileSystemObject is a objective fs interface like Pathname(ruby).
2 stars 1 forks source link
filesystem filesystem-library filesystems pathname

fso - FileSystemObject

npm npm license npm download total npm download by month dependencies Status devDependencies Status

Travis Build Status AppVeyor Build Status codecov.io Code Climate Codacy Badge Greenkeeper badge

fso - FileSystemObject is a objective fs interface like Pathname(ruby).

Installation

npm install fso

Usage

node.js:

var fso = require('fso').default;
var FileSystemObject = require('fso').FileSystemObject;

ES2015:

import fso, {FileSystemObject} from 'fso';

API

API Documents (with type annotations)

objective methods

constructor

var abc = new FileSystemObject("a", "b", "c");
abc.toString() === "a/b/c"

new / join

dir = fso.new('dir');
a = dir.new('a.txt');
a.writeFileSync('aaaaaa');

parent

dir = fso.new('dir');
p = dir.parent();
dir = p.new('dir');

children, childrenSync

dir = fso.new('dir');
files = dir.childrenSync();

childrenAll, childrenAllSync

dir = fso.new('dir');
files = dir.childrenAllSync();

filteredChildren, filteredChildrenSync, filteredChildrenAll, filteredChildrenAllSync

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()));

path

path / toString

fooPath = fso.new('foo').path;
fooPath = fso.new('foo').toString();

'fs' API

Supports all the node.js 'fs' APIs that needs no first path argument.

method(...args, callback), methodSync(...args) and promise = method(...args)

one file args

fs.truncateSync('/path/to/file', 0);
// is
file = fso.new('/path/to/file');
file.truncateSync(0);

two file args

fs.rename('/path/to/file', 'newfile', callback);
// is
file = fso.new('/path/to/file');
file.remane('newfile', callback);

fd args

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();

convenient additional

mkdirAll

fso.new('long/deep/path/to').mkdirAll().then(...);

mkdirp (= mkdirAll)

mkpath (= mkdirAll)

readdirAll

dir.readdirAllSync()
// results
['a.txt', 'aa', 'aa/a.txt', 'aa/b', 'aa/b/c.txt']

rmAll

await dir.rmAll('junk');

rmtree (= rmAll)

mergeDirectory

target.mergeDirectory(source);

copy:

target.rmAllSync();
target.mergeDirectorySync(source);

filteredMergeDirectory

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);

isChildOf

dir.new("foo/bar").isChildOf(dir)

isParentOf

dir.isParentOf(dir.new("foo/bar"))

path methods

(property) delimiter

fso.delimiter; // ":" or ";"

(property) sep

fso.sep; // "/" or "\\"

(static) format

var entry = FileSystemObject.format({...});

parse

var parsedObject = fso.new("a").parse();

normalize

fso.new("a").normalize(); // same as fso.new("a")

basename

fso.new("a/b/c").basename(); // same as new FileSystemObject("c")

dirname

same as parent()

extname

fso.new("a/b/c.txt").extname(); // ".txt"

isAbsolute

fso.new("a/b/c.txt").isAbsolute() === true

relative

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")

resolve

fso.new("a/b/c").resolve("/") // same as new FileSystemObject("/a/b/c")

License

This is released under Zlib License.