Git Product home page Git Product logo

empty-folder's Introduction

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 [email protected] or on GitHub
  • The v1.*.* documentation [link]
  • The v2.*.* new features:
    • the remove argument has been added.
    • the callback function returns the [Object] argument with error, removed and failed property rather than [Error|null] argument.
    • the empty-folder function execution does not stop at the first failed element. It tries to remove as many elements as possible. Then fires callback function with the results.

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 the path directory.
  • If true, it both empties the path directory and removes the path folder itself as well.

callback [Function]

  • It is fired once when all folders and files are (un)successfully removed.
  • The callback function is called with one [Object] argument of the following properties:
    • error: [Error] error, if the path does not exist or is inaccessible or if at least one child could not be removed. Otherwise null
    • removed: The [Array] list of absolute paths of all successfully removed files and folders of the path directory.
    • failed: The [Array] list of absolute paths of all path directory 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] failed list.

empty-folder's People

Contributors

devrafalko avatar

Stargazers

 avatar zgatrying avatar Mohammad Kermani avatar

Watchers

James Cloos avatar  avatar

Forkers

martialo12

empty-folder's Issues

Cannot empty folder because of type(stats,'Stats') is false

Hello!

I'm developing a program which converts CSV files in JSON files and I'm using your module to empty my output folder before creating a new batch of JSON files.

However, the module kept failing because of this condition : var isDir = type(stats,'Stats')&&stats.isDirectory();

I'm not sure what type stats is supposed to be, but it seems it's not 'Stats'. When I removed this part of the condition (leaving only stats.isDirectory()), the module started working fine.

Here's how I'm basically using the module :

const empty = require('empty-folder');
[...]
empty(outputRoot, false, o => {
  if (o.error) console.error(o.error);
  [...]
  createSingleFile(translations, 'en-US', outputRoot);
});

I'm using node v6.9.5, npm v5.6.0 and empty-folder v2.0.1.

Thanks for looking into this!

N.B : This same condition exists in the module list-contents, which is used by empty-folder and resulted in the same error (here and here).

err not defined

const empty = require('empty-folder');

empty('./dist', false, (o)=>{
if(o.error) console.error(err);
//console.log(o.removed);
//console.log(o.failed);
});

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.