Last active
October 31, 2020 00:35
-
-
Save bpedro/742162 to your computer and use it in GitHub Desktop.
nodejs implementation of recursive directory creation (https://brunopedro.com/2010/12/15/recursive-directory-nodejs/)
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
var fs = require('fs'); | |
/** | |
* Offers functionality similar to mkdir -p | |
* | |
* Asynchronous operation. No arguments other than a possible exception | |
* are given to the completion callback. | |
*/ | |
function mkdir_p(path, mode, callback, position) { | |
mode = mode || 0777; | |
position = position || 0; | |
parts = require('path').normalize(path).split('/'); | |
if (position >= parts.length) { | |
if (callback) { | |
return callback(); | |
} else { | |
return true; | |
} | |
} | |
var directory = parts.slice(0, position + 1).join('/'); | |
fs.stat(directory, function(err) { | |
if (err === null) { | |
mkdir_p(path, mode, callback, position + 1); | |
} else { | |
fs.mkdir(directory, mode, function (err) { | |
if (err) { | |
if (callback) { | |
return callback(err); | |
} else { | |
throw err; | |
} | |
} else { | |
mkdir_p(path, mode, callback, position + 1); | |
} | |
}) | |
} | |
}) | |
} |
Small code block using fs.mkdirSync().
Original gist here.
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 ;
}
}
function mkdirs(dirPath, mode, callback) {
//Call the standard fs.mkdir
fs.mkdir(dirPath, mode, function(error) {
//When it fail in this way, do the custom steps
if (error && error.errno === 34) {
//Create all the parents recursively
mkdirs(path.dirname(dirPath), mode, callback);
//And then the directory
mkdirs(dirPath, mode, callback);
}
//Manually run the callback since we used our own callback to do all these
!error&&callback && callback(error);
});
};
another implementation using the latest fs
& path
function mkdir(dir) {
// we explicitly don't use `path.sep` to have it platform independent;
var sep = '/';
var segments = dir.split(sep);
var current = '';
var i = 0;
while (i < segments.length) {
current = current + sep + segments[i];
try {
fs.statSync(current);
} catch (e) {
fs.mkdirSync(current);
}
i++;
}
}
const path = require('path');
const fs = require('fs');
const mkdirp = dir => path
.resolve(dir)
.split(path.sep)
.reduce((acc, cur) => {
const currentPath = path.normalize(acc + path.sep + cur);
try {
fs.statSync(currentPath);
} catch (e) {
if (e.code === 'ENOENT') {
fs.mkdirSync(currentPath);
} else {
throw e;
}
}
return currentPath;
}, '');
const path = require("path");
const fs = require("fs");
function mkdirp(directory) {
if (!path.isAbsolute(directory)) return;
let parent = path.join(directory, "..");
if (parent !== path.join("/") && !fs.existsSync(parent)) mkdirp(parent);
if (!fs.existsSync(directory)) fs.mkdirSync(directory);
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
My CoffeeScript code.
I was using Your code for fast prototype, here is my fully functional (I think) coffeescript code.