Skip to content

Latest commit

 

History

History
65 lines (43 loc) · 1.76 KB

README.md

File metadata and controls

65 lines (43 loc) · 1.76 KB

rmf

npm version Build Status Coverage Status

Remove a file or symbolic link without throwing an error when the target does not exist, as UNIX rm -f does

const {unlink} = require('fs').promises;
const rmf = require('rmf');

// file.txt does not exist

(async () => {
  await unlink('file.txt');
  // will be rejected with Error: ENOENT: no such file or directory, unlink 'file.txt'

  await rmf('file.txt');
  //=> won't be rejected
})();

This library helps removing temporary files generated by test scripts, as they would not exist before the first execution.

Installation

Use npm.

npm install rmf

API

const rmf = require('rmf');

rmf(filePath)

filePath: string Buffer Uint8Array URL
Return: Promise<boolean>

It removes a file or symbolic link.

When it successfully remove the target, the Promise will be resolved with true. When the target doesn't exist, the Promise will be resolved with false.

When the operation fails with another reason, for example the target is a directory, the Promise will be rejected.

(async () => {
  // A file a.jpg exists, but b.jpg doesn't

  await rmf('a.jpg'); //=> true
  await rmf('b.jpg'); //=> false

  await rmf(process.cwd());
  // rejected with Error: EPERM: operation not permitted, unlink '/Users/shinnn/'
})();

License

ISC License © 2018 Shinnosuke Watanabe