Filesystem wrapper for node.js. Provides a promisified API, along with a consistent interface accross node.js versions. All changes to the node.js fs API are tracked and accounted for in:
bfile will wrap older implementations to modernize them. Supports node@8.0.0 and up.
const fs = require('bfile');
(async () => {
await fs.writeFile('./foobar', 'Hello world!');
console.log(await fs.readFile('./foobar'));
for await (const [file] of fs.walk('.'))
console.log(`Found file: ${file}`);
})();
In addition to the default FS API, bfile provides some extra helpers.
fs.copy(src, [options])
(async) - Recursively copy file or directory todest
.fs.copySync(src, dest, [filter(path, stat)])
- Synchronousfs.copy
.fs.empty(path, [mode])
(async) - Ensure an empty directory atpath
.fs.emptySync(path, [mode])
- Synchronousfs.empty
.fs.exists(path, [mode])
(async) - A fixed version offs.exists
. Basically a wrapper aroundfs.access
which returns false onENOENT
orEACCESS
. Acceptsfs.access
flags as the second argument.fs.existsSync(path, [mode])
- Synchronousfs.exists
.fs.lstatTry(path, [options])
(async) - A version offs.lstat
which returnsnull
onENOENT
orEACCES
.fs.lstatTrySync(path, [options])
- Synchronousfs.lstatTry
.fs.move(src, dest)
(async) - Try to rename file. Recursively copies across devices, deleting the original, if necessary.fs.moveSync(path, [mode])
- Synchronousfs.move
.fs.outputFile(path, data, [options])
(async) - Output file while ensuring the preceding directory structure exists. Basically amkdirp
wrapper aroundwriteFile()
.fs.outputFileSync(path, data, [options])
- Synchronousfs.outputFile
.fs.mkdirp(path, [mode])
(async) - Alias tofs.mkdir(path, { recursive: true })
.fs.mkdirpSync(path, [mode])
- Synchronousfs.mkdirp
.fs.readJSON(path, [options])
(async) - Read a JSON file. Returns parsed JSON.fs.readJSONSync(path, [options])
- Synchronousfs.readJSON
.fs.remove(path, [options])
(async) - Recursively removepath
.fs.removeSync(path, [options])
- Synchronousfs.rimraf
.fs.statTry(path, [options])
(async) - A version offs.stat
which returnsnull
onENOENT
orEACCES
.fs.statTrySync(path, [options])
- Synchronousfs.statTry
.fs.stats(path, [options])
(async) - A stat function which will attempt to callfs.lstat
iffs.stat
fails withENOENT
orEACCES
(depending on options). This is useful for detecting broken symlinks and getting their appropriate stat object. Accepts options in the form of{ follow: [boolean], bigint: [boolean] }
.fs.statsSync(path, [options])
- Synchronousfs.stats
.fs.statsTry(path, [options])
(async) - A version offs.stats
which returnsnull
onENOENT
orEACCES
.fs.statsTrySync(path, [options])
- Synchronousfs.statsTry
.fs.traverse(paths, [options], callback)
(async) - Callback version offs.walk
.fs.traverseSync(paths, [options], callback)
- Synchronousfs.traverse
.fs.walk(paths, [options])
- An async iterator which recursively walks the target path/paths. Returns entries in the form of[path, stat, depth]
. Note thatstat
may benull
in the event of anEACCES
,EPERM
, orELOOP
ifoptions.throws
is false.fs.walkSync(paths, [options])
- Synchronousfs.walk
.fs.writeJSON(path, json, [options])
(async) - Write a JSON file (stringifiesjson
).fs.writeJSONSync(path, json, [options])
- Synchronousfs.writeJSON
.
flags
(number) - A bitfield to be passed tofs.copyFile{,Sync}
as flags (default:0
).filter(path, stat, depth)
(function) - A callback to filter determine which files are copied (default:null
).follow
(boolean) - Whether to follow symlinks forsrc
(default:false
).overwrite
(boolean) - Whether to overwrite existing files at the destination (default:false
).timestamps
(boolean) - Whether to preserve file timestamps (default:false
).
Options are the standard fs.readFile
options with some extras:
reviver(key, value)
(function) - Reviver function for JSON parsing (default:null
).
Options may also be a function as an alias for the reviver
option.
filter(path, stat, depth)
(function) - A callback to filter determine which files are removed (default:null
).maxRetries
(number) - Number of retries forEBUSY
,EMFILE
,ENFILE
,ENOTEMPTY
, orEPERM
(default:3
).retryDelay
(number) - Number of milliseconds to wait in between retries.
Options may also be a function as an alias for the filter
option.
fs.stats
and fs.statsSync
accept an object with properties:
follow
(boolean) - Whether to attempt callingfs.stat
beforefs.lstat
. If false, behavior is identical tofs.lstat
(default:true
).bigint
(boolean) - Whether to useBigInt
s on thefs.Stats
struct (default:false
).
Options may also be a boolean as an alias for the follow
option.
fs.traverse
, fs.traverseSync
, fs.walk
, and fs.walkSync
accept an object
with properties:
bigint
(boolean) - Whether to useBigInt
s on thefs.Stats
struct (default:false
).dirs
(boolean) - Whether to return directories in the iterated results (default:false
).files
(boolean) - Whether to return non-directory files in the iterated results (default:false
).filter(path, stat, depth)
(function) - A callback to filter determine which directories are entered and which files are returned. Note thatstat
may benull
ifoptions.throws
is false (default:null
).follow
(boolean) - Whether to follow symlinks. Note that the walking functions are smart enough to avoid recursive symlink loops (default:true
).maxDepth
(number) - Maximum depth to traverse. For reference,paths
are depth0
. Set to-1
for no limit (default:-1
).throws
(boolean) - Whether to throw on stat failure (default:false
).
Options are the standard fs.writeFile
options with some extras:
replacer(key, value)
(function) - Replacer function for JSON stringification (default:null
).spaces
(number) - Number of spaces to indent by (default:2
).eol
(string) - Line ending to use for the output text (default:\n
).
Options may also be a function as an alias for the reviver
option, or a
number for the spaces
option.
fs.stats
and the walking functions allow you to detect broken symlinks easily
when the follow
option is on:
const stat = await fs.statsTry('./foobar');
if (!stat) // ENOENT or EACCES
throw new Error('File not found.');
if (stat.isSymbolicLink()) // A symlink we couldn't resolve
throw new Error('Broken symlink detected.');
If you contribute code to this project, you are implicitly allowing your code
to be distributed under the MIT license. You are also implicitly verifying that
all code is your original work. </legalese>
- Copyright (c) 2014-2019, Christopher Jeffrey (MIT License).
See LICENSE for more info.