New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

batchdir

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

batchdir

Batch create directories or delete them.

  • 0.1.0
  • latest
  • npm
  • Socket score

Version published
Weekly downloads
0
decreased by-100%
Maintainers
1
Weekly downloads
 
Created
Source

build status

Node.js - batchdir

Batch create directories or delete them.

Why?

I got tired of checking whether a group of directories exist before I create or make them. Even worse, when you have to check three directories:

var fs = require('fs')
  , mkdirp = require('mkdirp');

var dirs = ['/tmp/a', '/tmp/b', '/tmp/c'];
var i = 0;

function again(callback) {
  if (i < dirs.length) {
    fs.exists(dirs[i], function(itDoes) {
      if (!itDoes)
        fs.mkdir(dirs[i], function(err) {
          if (err) {
            callback(err); //<--- error return
          } else {
            i += 1;
            again(callback);
          }
        })
      else
        i += 1;
        again(callback);  
    });
  } else {
    callback(); //<--- done
  }
}

again(function(err){
  if (err) 
    console.log('We have an error.')
  else
    console.log('Done.');
});

Yuck.

Why not the following:

var batchdir = require('batchdir');

var dirs = ['/tmp/a', '/tmp/b', '/tmp/c'];
batchdirs(dirs).mkdirs(function(err) {
  if (err) 
    console.log('We have an error.')
  else
    console.log('Done.');
})

Much cleaner.

Installation

npm install batchdir

Methods

batchdirs()

Contsructor function.

Args:

  1. Array or strings.
batchdirs(['d1', 'd2'])
//or
batchdirs('d1', 'd2')

create() / mkdirs() / mkdir() / make()

Creates the directory(ies) if they don't exist. Will make full path like mkdir -p.

Args:

  1. Callback function containing an Error object if one existed.
batchdirs(dirs).create(function(err) { });

remove() / delete() / rmrf()

Deletes directory(ies) if they do exist. Will delete all of the contents like rm -rf.

Args:

  1. Callback function containing an Error object if one existed.
batchdirs(dirs).remove(function(err) { });

Author

JP Richardson (@jprichardson) read my coding blog Procbits.

If you use Git with others, you should checkout Gitpilot to make collaboration with Git simple using a different GUI. We would love your feedback.

License

(MIT License)

Copyright 2012, JP Richardson jprichardson@gmail.com

Keywords

FAQs

Package last updated on 06 Sep 2012

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc