Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mkdir: support for nodejs 10.x options.recursive (mkdir -p) #62

Open
carlos22 opened this issue Nov 11, 2018 · 3 comments
Open

mkdir: support for nodejs 10.x options.recursive (mkdir -p) #62

carlos22 opened this issue Nov 11, 2018 · 3 comments

Comments

@carlos22
Copy link

Create a folder with all parents (like mkdir -p on unix). This would be very useful and be in line with the current fs api. See: nodejs/node#21875

@carlos22
Copy link
Author

carlos22 commented Nov 11, 2018

// based on https://github.com/substack/node-mkdirp/blob/master/index.js
function mkdirPwebdav (p, opts, f, made) {
    if (typeof opts === 'function') {
        f = opts;
        opts = {};
    }
    else if (!opts || typeof opts !== 'object') {
        opts = { mode: opts };
    }
    
    var mode = opts.mode; // unused
    var xfs = opts.fs || fs;
    
    if (!made) made = null;
    
    var cb = f || function () {};
    
    xfs.mkdir(p, function (er) {
        if (!er) {
            made = made || p;
            return cb(null, made);
        }
        switch (er.status) {
            case 409:
                mkdirP(path.dirname(p), opts, function (er, made) {
                    if (er) cb(er, made);
                    else mkdirP(p, opts, cb, made);
                });
                break;

            // In the case of any other error, just see if there's a dir
            // there already.  If so, then hooray!  If not, then something
            // is borked.
            default:
                xfs.stat(p, function (er2, stat) {
                    // if the stat fails, then that's super weird.
                    // let the original error be the failure reason.
                    if (er2 || !stat.isDirectory()) cb(er, made)
                    else cb(null, made);
                });
                break;
        }
    });
}

Call with: mkdirPwebdav('/path/to/create', {fs : wfs}, cb)

@tansaku
Copy link
Contributor

tansaku commented Nov 20, 2018

I'd like this :-)

fs-extra calls it ensureDir

https://github.com/jprichardson/node-fs-extra/blob/master/docs/ensureDir.md

@perry-mitchell
Copy link
Owner

Sounds good to me! Happy to receive a PR for this.

It's good that webdav by default uses the recursive -> false (MKCOL) default behaviour that node implements. Not sure how best to do this but I guess it'll be a lot of stat and mkdir requests back to back.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants