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

2.0 - refactor #8

Merged
merged 8 commits into from
Sep 4, 2019
Merged

2.0 - refactor #8

merged 8 commits into from
Sep 4, 2019

Conversation

jonschlinkert
Copy link
Owner

No description provided.

@jonschlinkert
Copy link
Owner Author

@doowb please review when you have a chance.

Copy link
Collaborator

@doowb doowb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks really good.

Most of my comments are either knit picks or documentation related.
One is about an extra parameter on write.stream. There should also be tests for write.stream that take options.

Other than that LGTM :shipit:

return writeFile.promise.apply(null, arguments);
}
const opts = { encoding: 'utf8', ...options };
const destpath = opts.increment ? incrementName(filepath) : filepath;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great idea! I like that you're doing the check here and incrementing here. It makes it simple later on to just do a file exists check.

* @name write
* @param {String} `filepath` file path.
* @param {String|Buffer|Uint8Array} `data` Data to write.
* @param {Object} `options` Options to pass to [fs.writeFile][writefile]
* @param {Function} `callback` (optional) If no callback is provided, a promise is returned.
* @api public
*/
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These comments could use some information on the return value. I noticed there's a result object with path and data returned. I think this is a good idea, especially when increment is used.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

index.js Outdated
* @param {String} `filepath` file path.
* @param {String|Buffer|Uint8Array} `data` Data to write.
* @param {Object} `options` Options to pass to [fs.writeFileSync][writefilesync]
* @return {undefined}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The return value is an object with path and data.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

index.js Outdated
* @api public
*/

writeFile.sync = function(filepath, data, options) {
write.stream = (filepath, contents, options) => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

contents isn't used in this method.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

writeFile.stream = function(filepath, options) {
mkdirp.sync(path.dirname(filepath), options);
return fs.createWriteStream(filepath, options);
const incrementName = destpath => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💯

index.js Outdated
// data is a buffer, we only need to call `.toString()` on
// the entire string if the condition is true.
if (String(data.slice(-1)) !== '\n') {
return data.toString() + '\n';
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be possible to append a newline to the buffer without doing .toString().

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice, good catch! I wasn't thinking about that. This is an awesome example of a place where we can make code much more performant with a really simple optimization. Awesome.

Copy link
Owner Author

@jonschlinkert jonschlinkert Sep 4, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed it to:

  if (String(data.slice(-1)) !== '\n') {
    if (typeof data === 'string') {
      return data + '\n';
    }
    return data.concat(Buffer.from('\n'));
  }

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should be possible with other things too, like front matter. I'll look into it.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@doowb I just saw your point below about the Buffer class. LMKWYT.

// if filepath !== destpath, that means the user has enabled
// "increment", which has already checked the file system and
// renamed the file to avoid conflicts, so we don't need to
// check again.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ I was wondering about this above.

index.js Outdated
};

const mkdir = (dirname, options) => {
return new Promise(res => fs.mkdir(dirname, options, () => res()));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is just a knit pick, but everywhere else you use a Promise like this, you use resolve. I only bring it up because it took be a split second to notice was res was.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agreed, fixed.

} catch (err) { /* do nothing */ }
};

const isBuffer = data => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice... I understand that my statement above about adding the newline to the buffer would only be possible if using the built-in Buffer class. But since you're doing this isBuffer check here, it seems intentional to leave Buffer out.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, damn. Good point. Hmm, we can either include the Buffer class, which - if I recall correctly - doesn't the native fs module need the buffer class anyway? Or we can just call .toString() if it's not a string already, before appending the \n.

Thoughts?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we leave Buffer in above, but we have an idea of what to do if people complain that they can't bundle write for some reason.

@jonschlinkert
Copy link
Owner Author

@doowb LMK if you think this is good to publish.

@doowb
Copy link
Collaborator

doowb commented Sep 4, 2019

LGTM!

@jonschlinkert
Copy link
Owner Author

Thanks @doowb!

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

Successfully merging this pull request may close these issues.

2 participants