Package Exports
- fs-extended
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (fs-extended) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
fs-extended
Node.js module that extends the native fs with a lot of convenient methods.
If you miss a method, and there is more than 1 person in the world that would use it, create an issue!.
Dependencies
None.
Changelog
Upholds the Semantic Versioning Specification.
Installation
npm install fs-extendedUsage
var fs = require('fs-extended');
fs.listFiles('foo', { recursive: 1 }, function (err, files) {
console.log(err, files);
});Methods
All methods from native fs module are available.
fs.createFile(path, data, [mode], [callback]);
Creates a new, or overrides an existing file. Creates any missing parent directories.
- path
StringPath to a file. - data
StringContents of a new file. - [mode]
ObjectFile mode. Defaults to0666. - [callback]
FunctionReceives arguments:- err
MixedError object on error,nullotherwise.
- err
fs.createFileSync(path, data, [mode]);
Synchronous fs.createFile();
fs.ensureFile(path, [mode], [callback]);
Ensures file exists. If it does, it'll only ensure file mode (when passed). If it doesn't, it'll create an empty file.
Creates any missing parent directories.
- path
StringPath to a file. - [mode]
ObjectFile mode. Defaults to0666. - [callback]
FunctionReceives arguments:- err
MixedError object on error,nullotherwise.
- err
fs.ensureFileSync(path, [mode]);
Synchronous fs.ensureFile();
fs.copyFile(oldPath, newPath, [callback]);
Copies a file from one location to another. Creates any missing destination directories, and works between
different partitions/filesystems. Also makes sure that file mode is preserved.
- oldPath
StringPath to original file. - newPath
StringDestination path. - [callback]
FunctionReceives arguments:- err
MixedError object on error,nullotherwise.
- err
fs.copyFileSync(oldPath, newPath);
Synchronous fs.copyFile();
fs.moveFile(oldPath, newPath, [callback]);
Moves a file from one location to another. Creates any missing destination directories, and works between
different partitions/filesystems. Also makes sure that file mode is preserved.
- oldPath
StringPath to original file. - newPath
StringDestination path. - [callback]
FunctionReceives arguments:- err
MixedError object on error,nullotherwise.
- err
fs.moveFileSync(oldPath, newPath);
Synchronous fs.moveFile();
fs.emptyFile(file, [callback]);
Deletes all contents of a file. When file doesn't exist, it is created with a default mode 0666.
A mere alias of fs.truncate(file, 0, callback) with an optional callback for API consistency.
- file
StringPath to a file. - [callback]
FunctionReceives arguments:- err
MixedError object on error,nullotherwise.
- err
fs.emptyFileSync(file, [callback]);
Synchronous fs.emptyFile();
fs.deleteFile(file, [callback]);
Deletes a file. Doesn't throw an error When file doesn't exist.
A mere alias of fs.unlink(file, callback) with ignored ENOENT error and an optional callback for API consistency.
- file
StringPath to a file. - [callback]
FunctionReceives arguments:- err
MixedError object on error,nullotherwise.
- err
fs.deleteFileSync(file, [callback]);
Synchronous fs.deleteFile();
fs.createDir(dir, [mode], [callback]);
Creates a directory, and any missing parent directories. If directory exists, it only ensures mode (when passed).
- dir
StringPath to a new directory. - [mode]
ObjectDirectory mode. Defaults to0777. - [callback]
FunctionReceives arguments:- err
MixedError object on error,nullotherwise.
- err
fs.createDirSync(dir, [mode]);
Synchronous fs.createDir();
fs.ensureDir(oldPath, newPath, [callback]);
Alias of fs.createDir() for API consistency.
fs.ensureDirSync(oldPath, newPath);
Alias of fs.createDirSync() for API consistency.
fs.copyDir(oldPath, newPath, [callback]);
Copies a directory and everything in it from one location to another. Creates any missing destination directories, and
works between different partitions/filesystems. Also makes sure that mode of all items is preserved.
- oldPath
StringPath to original directory. - newPath
StringDestination path. - [callback]
FunctionReceives arguments:- err
MixedError object on error,nullotherwise.
- err
fs.copyDirSync(oldPath, newPath);
Synchronous fs.copyDir();
fs.moveDir(oldPath, newPath, [callback]);
Moves a directory and everything in it from one location to another. Creates any missing destination directories, and
works between different partitions/filesystems. Also makes sure that mode of all items is preserved.
- oldPath
StringPath to original directory. - newPath
StringDestination path. - [callback]
FunctionReceives arguments:- err
MixedError object on error,nullotherwise.
- err
fs.moveDirSync(oldPath, newPath);
Synchronous fs.moveDir();
fs.emptyDir(dir, [callback]);
Deletes everything inside a directory, but keeps the directory itself.
- dir
StringPath to directory. - [callback]
FunctionReceives arguments:- err
MixedError object on error,nullotherwise.
- err
fs.emptyDirSync(dir);
Synchronous fs.emptyDir();
fs.deleteDir(dir, [callback]);
Deletes a directory and everything inside it.
- dir
StringPath to directory. - [callback]
FunctionReceives arguments:- err
MixedError object on error,nullotherwise.
- err
fs.deleteDirSync(dir);
Synchronous fs.deleteDir();
fs.copy(oldPath, newPath, [callback]);
Copy based on a type of the original item. Bridges to fs.copyFile() when oldPath is a file, or fs.copyDir() when
it's a directory.
- oldPath
StringPath to a file or a directory. - newPath
StringDestination path. - [callback]
FunctionReceives arguments:- err
MixedError object on error,nullotherwise.
- err
fs.copySync(oldPath, newPath);
Synchronous fs.copy();
fs.move(oldPath, newPath, [callback]);
Move based on a type of the original item. Bridges to fs.moveFile() when oldPath is a file, or fs.moveDir() when
it's a directory.
- oldPath
StringPath to a file or a directory. - newPath
StringDestination path. - [callback]
FunctionReceives arguments:- err
MixedError object on error,nullotherwise.
- err
fs.moveSync(oldPath, newPath);
Synchronous fs.move();
fs.empty(target, [callback]);
Empty based on a type of the original item. Bridges to fs.emptyFile() when target is a file, or fs.emptyDir() when
it's a directory.
- target
StringPath to a file or a directory. - [callback]
FunctionReceives arguments:- err
MixedError object on error,nullotherwise.
- err
fs.emptySync(target);
Synchronous fs.empty();
fs.delete(target, [callback]);
Delete based on a type of the original item. Bridges to fs.deleteFile() when target is a file, or fs.deleteDir()
when it's a directory.
- target
StringPath to a file or a directory. - [callback]
FunctionReceives arguments:- err
MixedError object on error,nullotherwise.
- err
fs.deleteSync(target);
Synchronous fs.delete();
fs.listAll(dir, [options], [callback]);
List all items inside a directory. Supports filtering and recursive listing.
- dir
StringPath to a directory. - [options]
ObjectObject with options:- recursive
BooleanList items recursively, expanding all child directories. Defaults tofalse. - prependDir
BooleanPrependdirpath before every item in final array. Defaults tofalse. - filter
FunctionFunction to filter items. Should returntrueorfalse. Receives arguments:- itemPath
StringFull path to an item. - stat
ObjectItem'sfs.Statsobject.
- itemPath
- map
FunctionFunction to map final list items. Receives arguments:- itemPath
StringFull path to an item. - stat
ObjectItem'sfs.Statsobject.
- itemPath
- recursive
- [callback]
FunctionReceives arguments:- err
MixedError object on error,nullotherwise. - list
ArrayList of items inside a directory.
- err
Examples
Filtering - effectively turn fs.listAll() into fs.listFiles():
function filter(itemPath, stat) {
return stat.isFile();
}
fs.listAll(dir, { filter: filter }, function (err, files) {
console.log(err); // possible exception
console.log(files); // array of all files inside a directory
})Mapping - change the style of a final list:
function map(itemPath, stat) {
return {
path: itemPath,
name: path.basename(itemPath),
ext: path.extname(itemPath),
isFile: stat.isFile(),
isDirectory: stat.isDirectory(),
};
}
fs.listAll(dir, { map: map }, function (err, files) {
console.log(err); // possible exception
console.log(files); // array of file object descriptors returned by map()
// Example
files[0].path; // 1st file's path
files[0].ext; // 1st file's extension
})fs.listAllSync(dir, [options]);
Synchronous fs.listAll();
fs.listFiles(dir, [options], callback);
List all files inside a directory. Supports filtering and recursive listing.
- dir
StringPath to a directory. - [options]
ObjectObject with options:- recursive
BooleanList files recursively, expanding all child directories. Defaults tofalse. - prependDir
BooleanPrependdirpath before every item in final array. Defaults tofalse. - filter
FunctionFunction to filter items. Should returntrueorfalse. Receives arguments:- filePath
StringFull path to a file. - stat
ObjectFile'sfs.Statsobject.
- filePath
- map
FunctionFunction to map final list items. Receives arguments:- filePath
StringFull path to a file. - stat
ObjectFile'sfs.Statsobject.
- filePath
- recursive
- callback
FunctionReceives arguments:- err
MixedError object on error,nullotherwise. - files
ArrayList of files inside a directory.
- err
Examples
See fs.listAll() examples.
fs.listFilesSync(dir, [options]);
Synchronous fs.listFiles();
fs.listDirs(dir, [options], callback);
List all directories inside a directory. Supports filtering and recursive listing.
- dir
StringPath to a directory. - [options]
ObjectObject with options:- recursive
BooleanList directories recursively, expanding all child directories. Defaults tofalse. - prependDir
BooleanPrependdirpath before every directory in final array. Defaults tofalse. - filter
FunctionFunction to filter items. Function should returntrueorfalse. Receives arguments:- dirPath
StringFull path to a directory. - stat
ObjectDirectory'sfs.Statsobject.
- dirPath
- map
FunctionFunction to map final list items. Receives arguments:- dirPath
StringFull path to a directory. - stat
ObjectDirectory'sfs.Statsobject.
- dirPath
- recursive
- callback
FunctionReceives arguments:- err
MixedError object on error,nullotherwise. - dirs
ArrayList of directories inside a directory.
- err
Examples
See fs.listAll() examples.
fs.listDirsSync(dir, [options]);
Synchronous fs.listDirs();
fs.uniquePath(path, [no], callback);
Generates a unique path that won't override any other file.
If path doesn't exist, it is simply returned. Otherwise it will insert "-N" suffix between the file name and its extension (if there is any) until it finds a path that doesn't exist yet.
Be aware of Race condition!
- path
StringPath to be uniquefied. - [no]
IntegerStarting number index. Defaults to2. - callback
FunctionReceives arguments:- uniquePath
StringUnique version of the original path.
- uniquePath
Example
With a directory structure:
dir
├─ foo
├─ bar.txt
├─ bar-2.txt
├─ bar-3.txt
└─ baz.tar.gzThese are the outputs:
fs.uniquePath('dir/foo', function (uniquePath) {
uniquePath; // => dir/foo-2
});fs.uniquePath('dir/bar.txt', function (uniquePath) {
uniquePath; // => dir/bar-4.txt
});fs.uniquePath('dir/baz.tar.gz', function (uniquePath) {
uniquePath; // => dir/baz-2.tar.gz
});fs.uniquePath('dir/unique', function (uniquePath) {
uniquePath; // => dir/unique
});fs.uniquePathSync(path, [no]);
Synchronous fs.uniquePath();
fs.writeJSON(file, data, [indentation], [callback]);
Format data in JSON and write into a file. Creates destination directory if it doesn't exist yet.
- file
StringPath to a destination file. - data
MixedData to be formated in JSON. - [indentation]
MixedNumber of spaces, or a direct representation of a single indentation level, like '\t'. Defaults to no indentation. - [callback]
FunctionReceives arguments:- err
MixedError object on error,nullotherwise.
- err
Alias: fs.writeJson()
fs.writeJSONSync(file, data, [indentation]);
Synchronous fs.writeJSON();
Alias: fs.writeJsonSync()
fs.readJSON(file, callback);
Encode data in JSON format and write into a file. Creates destination directory if it doesn't exist yet.
- file
StringPath to a JSON file. - callback
FunctionReceives arguments:- err
MixedError object on error,nullotherwise. - data
MixedData from JSON file.
- err
Alias: fs.readJson()
fs.readJSONSync(file);
Synchronous fs.readJSON();
Alias: fs.readJsonSync()

