Skip to content

Instantly share code, notes, and snippets.

@antonioiksi
Created May 12, 2020 21:11
Show Gist options
  • Save antonioiksi/3064b58793f70f191e3c9f40f0df9108 to your computer and use it in GitHub Desktop.
Save antonioiksi/3064b58793f70f191e3c9f40f0df9108 to your computer and use it in GitHub Desktop.
create_dir_recursively
var fs = require('fs');
/**
* Splits whole path into segments and checks each segment for existence and recreates directory tree from the bottom.
* If since some segment tree doesn't exist it will be created in series.
* Existing directories will be skipped.
* @param {String} directory
*/
function mkdirSyncRecursive(directory) {
var path = directory.replace(/\/$/, '').split('/');
for (var i = 1; i <= path.length; i++) {
var segment = path.slice(0, i).join('/');
!fs.existsSync(segment) ? fs.mkdirSync(segment) : null ;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment