2.0.4 • Published 5 years ago
empty-folder v2.0.4
Description
empty-folder is a module that removes (asynchronously) the contents of the specified directory with all of its subfolders and subfiles and (optionally) the specified directory itself.
- Any bugs found? Give me to know on dev.rafalko@gmail.com or on GitHub
- The
v1.*.*documentation [link] - The
v2.*.*new features:- the
removeargument has been added. - the
callbackfunction returns the Object argument witherror,removedandfailedproperty rather than Error|null argument. - the
empty-folderfunction execution does not stop at the first failed element. It tries to remove as many elements as possible. Then firescallbackfunction with the results.
- the
Installation
npm install empty-folder
const empty = require('empty-folder');Usage
empty(path,remove,callback)
const empty = require('empty-folder');
empty('./dist', false, (o)=>{
if(o.error) console.error(o.error);
//console.log(o.removed);
//console.log(o.failed);
});path String
- It should indicate the path (directory) which children elements should be removed.
remove Boolean
- If
false, it only empties thepathdirectory. - If
true, it both empties thepathdirectory and removes thepathfolder itself as well.
callback Function
- It is fired once when all folders and files are (un)successfully removed.
- The
callbackfunction is called with one Object argument of the following properties:error: Error error, if thepathdoes not exist or is inaccessible or if at least one child could not be removed. Otherwisenullremoved: The Array list of absolute paths of all successfully removed files and folders of thepathdirectory.failed: The Array list of absolute paths of allpathdirectory children that could not be removed.
How it works under the hood
- takes the paths of all (sub)files and (sub)folders
- removes all (sub)files with native nodejs
fs.unlink - removes all (sub)folders in order (beginning with the most rooted) with native nodejs
fs.rmdir - if some of the files or folders cannot be removed, it is pushed into Array
failedlist.