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

How to tell whether deletion succeeded? #94

Open
whyboris opened this issue Feb 16, 2020 · 2 comments
Open

How to tell whether deletion succeeded? #94

whyboris opened this issue Feb 16, 2020 · 2 comments

Comments

@whyboris
Copy link

whyboris commented Feb 16, 2020

README claims trash() returns a Promise, but the expected type is void?

expectType<Promise<void>>(trash('/path/to/item1'));

So there is currently no way to tell whether the library succeeded in moving a file into the trash or whether it encountered an error?

trash('some-file.lol')
  .then((result) => {
    console.log(result); // always `undefined`
  })
  .catch((err) => {
    console.log(err); // never runs
  });

So, the library just sends a command to the OS and doesn't know what happens after?

When you tell the library to delete a non-existing file, it behaves the same way (no error, no notification).

This isn't a problem -- it's just that I'd love to have my app respond appropriately, not just assume that deletion occurred correctly 😓

I can manually get node to check whether the file is present to confirm it was deleted, but would be great if I didn't have to 😅

Please let me know if I'm misunderstanding something.

@sindresorhus
Copy link
Owner

sindresorhus commented Feb 16, 2020

It doesn't reject the promise if you give it an non-existent file as the input is a glob pattern, not a file path. So if running the glob patterns results in zero files, that might be expected or it might not. It's not for us to decide. We could try to detect whether the input is an actual file path or a glob, and if it's a file path and it doesn't exist, we could throw, but that is not easy to get right. We could maybe add a glob: false option where it would then fail loudly if some of the given file paths don't exist.

However, it does reject the promise if something wrong happened while moving the files to the trash.

The behavior should be better documented regardless though.

@whyboris
Copy link
Author

Thank you for the quick response. I handled it like this for my purposes (I'm only deleting 1 file at a time) 😁

  (async () => {
    await trash(fileToDelete);

    fs.access(fileToDelete, fs.F_OK, (err: any) => {
      if (err) {
        // my logic for handling a successful delete 
      }
    });

  })();

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

No branches or pull requests

2 participants