Skip to content

Instantly share code, notes, and snippets.

@christophemarois
Created December 29, 2015 21:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save christophemarois/4a3c931b300e19238f13 to your computer and use it in GitHub Desktop.
Save christophemarois/4a3c931b300e19238f13 to your computer and use it in GitHub Desktop.
Create folder or use existing
function mkdirpSync (dirpath) {
var fs = require('fs');
var parts = dirpath.split(path.sep);
for( var i = 1; i <= parts.length; i++ ) {
try {
fs.mkdirSync(path.join.apply(null, parts.slice(0, i)));
} catch(e) {
if ( e.code != 'EEXIST' ) throw e;
}
}
}
mkdirpSync('apple/orange');
mkdirpSync('apple/orange/grape');
mkdirpSync('apple');
// Will create:
// apple
// | orange
// | grape
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment