Skip to content

Instantly share code, notes, and snippets.

@joseluisq
Created June 2, 2018 16:09
Show Gist options
  • Save joseluisq/b7812907094f595b7062483ea8effeff to your computer and use it in GitHub Desktop.
Save joseluisq/b7812907094f595b7062483ea8effeff to your computer and use it in GitHub Desktop.
mkdir -p in node.js
var path = require('path');
var fs = require('fs');
module.exports = function mkdirP (p, mode, f) {
var cb = f || function () {};
if (p.charAt(0) != '/') { cb('Relative path: ' + p); return }
var ps = path.normalize(p).split('/');
path.exists(p, function (exists) {
if (exists) cb(null);
else mkdirP(ps.slice(0,-1).join('/'), mode, function (err) {
if (err && err.errno != process.EEXIST) cb(err)
else fs.mkdir(p, mode, cb);
});
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment