isomorphic-git / lightning-fs

A lean and fast 'fs' for the browser
MIT License
493 stars 48 forks source link

Missing fs.existsSync #128

Open Pomax opened 1 month ago

Pomax commented 1 month ago

It looks like the fairly critical fs.existsSync is missing from the set of available functions, which makes otherwise "simple if check" code a "try/catch with dangling let variables" mess. Could .existsSync be added?

jcubic commented 1 month ago

fs.exists is deprecated in Node.js, so I don't see a reason for adding this.

jcubic commented 1 month ago

You can create this function very easy if you need it:

async function exists(path) {
  try {
    await fs.stat(path);
    return true;
  } catch(e) {
    return false;
  }
}

or

function exists(path) {
  return fs.stat(path)
    .then(() => true)
    .catch(() => false);
}

no need for dangling let.

Pomax commented 1 month ago

Sure, but existsSync is not (not sure why I wrote exists without that, all my code uses the sync version... updated the title and initial comment).

I know it's easy enough to shim, but that shouldn't be necessary.

jcubic commented 1 month ago

I don't think that you can implement existsSync with lightingFS that use promise based API.

But if you want to contribute and implement it (even that I think it's not possible) go ahead.