senecajs / seneca-jsonfile-store

Node.js Seneca data storage module that uses JSON files.
MIT License
13 stars 11 forks source link

fix ensurefolder #8

Closed kamil-mech closed 9 years ago

kamil-mech commented 9 years ago

ensurefolder tries to fs.mkdir and returns error if any occurs. The problem is when fs.mkdir returns error stating that the folder already exists. ensurefolder sends that out as an error, which causes a snowballing effect.

As ensurefolder's role is to make sure folder exists, it shouldn't really return an error saying a folder exists, right?

geek commented 9 years ago

@kamil-mech looks good. Can you provide a test when this condition occurs?

kamil-mech commented 9 years ago

I suggest applying this solution instead. I have tested and it works. What You think?

https://github.com/senecajs/seneca-level-store/commit/c15fbdea94a24a78e25637e2b114b78e6c14cdb7

  /**
   * Return if the folder exists, create otherwise.
   */
  function ensurefolder(folder, cb){
    fs.stat(folder, function(err, stat) {
      if (!err && stat.isDirectory()) {
        return cb();
      }
      fs.mkdir(folder, function(err) {
        if (err && err.code === 'EEXIST') {
          err = null;
        }
        cb(err);
      });
    });
  }
geek commented 9 years ago

looks good to me

kamil-mech commented 9 years ago

Ready now. Feel free to merge.

kamil-mech commented 9 years ago

May You please push the changes to npm? Thank You