What is recursive-fs?
The recursive-fs npm package provides utilities for performing recursive file system operations such as copying and removing directories and their contents.
What are recursive-fs's main functionalities?
Recursive Copy
This feature allows you to recursively copy the contents of one directory to another. The code sample demonstrates how to use the `copy` method to copy all files and subdirectories from 'sourceDir' to 'destDir'.
const rfs = require('recursive-fs');
rfs.copy('sourceDir', 'destDir', function(err) {
if (err) {
console.error('Error during copy:', err);
} else {
console.log('Copy completed successfully');
}
});
Recursive Remove
This feature allows you to recursively remove a directory and all of its contents. The code sample demonstrates how to use the `remove` method to delete 'targetDir' and all files and subdirectories within it.
const rfs = require('recursive-fs');
rfs.remove('targetDir', function(err) {
if (err) {
console.error('Error during remove:', err);
} else {
console.log('Remove completed successfully');
}
});
Other packages similar to recursive-fs
fs-extra
fs-extra is a popular package that extends the native Node.js fs module with additional methods, including recursive file system operations. It provides similar functionality to recursive-fs, such as copying and removing directories recursively, but also includes many other useful file system utilities.
rimraf
rimraf is a package specifically designed for deep deletion of directories. It provides a simple and efficient way to recursively remove directories and their contents, similar to the remove functionality in recursive-fs.
ncp
ncp (Node Copy) is a package focused on copying files and directories. It supports recursive copying and provides similar functionality to the copy feature in recursive-fs. However, it is more specialized and does not include other file system operations.
#Install
$ npm install recursive-fs
#Recursive
##readdirr
var path = require('path');
var recursive = require('recursive-fs');
var root = path.resolve(process.argv[2]);
recursive.readdirr(root, function (err, dirs, files) {
if (err) {
console.log(err);
} else {
console.log('DONE!');
}
});
##rmdirr
var path = require('path');
var recursive = require('recursive-fs');
var root = path.resolve(process.argv[2]);
recursive.rmdirr(root, function (err) {
if (err) {
console.log(err);
} else {
console.log('DONE!');
}
});
##cpdirr
var path = require('path');
var recursive = require('recursive-fs');
var spath = path.resolve(process.argv[2]),
tpath = path.resolve(process.argv[3]);
recursive.cpdirr(spath, tpath, function (err) {
if (err) {
console.log(err);
} else {
console.log('DONE!');
}
});
##Tests
$ mocha