Created
December 29, 2015 21:10
-
-
Save christophemarois/4a3c931b300e19238f13 to your computer and use it in GitHub Desktop.
Create folder or use existing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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