kevinbeaty / fs-promise

[DEPRECATED] Use mz or fs-extra^3 with Promise support

Home Page:https://www.npmjs.com/package/fs-extra

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Explicit `fs` implementation

tomek-he-him opened this issue · comments

Hi, I’m looking at your library and want to share my fresh first impressions.

😄 Great idea! Looks more lightweight than the monolithic q-io.

😕 But one thing isn’t clear to me. How do you determine whether you use fs, graceful-fs, fs-extra or something else? It feels a bit magical to me – I’d feel safer if this were explicit:

const fs = require('fs-promise')(require('fs-extra'));

Thanks!

It is a bit "magical". It prefers fs-extra first (if installed), then graceful-fs (if installed), then falls back to the standard fs. You can think of it like feature-detection in the browser.

Last I looked, fs-extra does the same thing by preferring graceful-fs and falls back to fs. Since graceful-fs includes the same functionality as fs, and fs is a subset of fs-extra, the API does not change (although you get the additional functionality of fs-extra and/or graceful-fs if they are installed.)

The same thing happens with loading a promise implementation (by way of any-promise). You must load a preferred Promise implementation for this library to work. So if you wanted to be explicit, you'd have to provide an fs and a Promise implementation.

const fs = require('fs-promise')(require('fs-extra'), require('promise'));

So really it's a tradeoff of "magic" vs "boilerplate" (arguable terms either way). There are arguments either way. Personally, I prefer the "magic" in this case.

// magic, bug Just Works(tm)
const fsp = require('fs-promise')

Does that help?

Yeah, thanks for the explanation.

I do prefer “boilerplate” to “magic”, as that’s no tremendous amount of boilerplate.

But I get your point. And I feel less disoriented now that I’ve read how any-promise works. How about pasting what you’ve just written about fs into the readme? That would help others as well :)

BTW, I’m staying with your lib :) Thanks for the fine piece of code :)

I agree, it's not all that much boilerplate. Really, my biggest hesitation is that it would be a breaking change, and I don't see enough benefit. I think if I were to change the loading to be explicit, I would probably just add a hard dependency to fs-extra and an explicit Promise implementation (probably promise as it was before)... But I'm just going to leave it alone for now.

(By the way, I looked again, and graceful-fs is now an explicit dependency of fs-extra. So that point is moot.)

Thanks again!