Skip to content

Instantly share code, notes, and snippets.

@reu reu/mkdirp.js
Last active Apr 14, 2017

Embed
What would you like to do?
const fs = require("fs");
const path = require("path");
const promisify = fn => (...args) =>
new Promise((resolve, reject) =>
fn(...args, (err, res) => err === null ? resolve(res) : reject(err))
);
const mkdir = promisify(fs.mkdir);
const stat = promisify(fs.stat);
const mkdirp = (dir, opts) =>
mkdir(dir, opts)
.then(() => dir)
.catch(err =>
err.code == "ENOENT"
? mkdirp(path.dirname(dir), opts).then(() => mkdirp(dir, opts))
: stat(dir).then(() => dir)
);
module.exports = mkdirp;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.