smart-file-copy-or-move

Rename or move a file creating needed directories

Usage no npm install needed!

<script type="module">
  import smartFileCopyOrMove from 'https://cdn.skypack.dev/smart-file-copy-or-move';
</script>

README

smart-file-copy-or-move

Build Status npm version Coverage Status

Asynchronous file rename/move/copy creating needed directories.

Use fs.rename() or fs.copyFile() with fs.unlink() depending on the options provided.

Creates needed directories via fs.mkdir() when initial call fails due to 'ENOENT' error (meaning no directory).

Will generate the target based on the basename of source and options.cwd.

Install

npm install smart-file-copy-or-move

Usage

const fileop = require('smart-file-copy-or-move')

fileop({
  // `true` means delete the source afterwards.
  // `false`, the default, means it's a copy; don't delete source.
  move: true,

  // `true` means ignore if target already exists.
  // `false`, the default, means only copy to target if it doesn't exist.
  // when `false` and target exists then an 'EEXIST' error occurs.
  overwrite: true,

  // required: string path to the source file.
  source: 'some.file',

  // optional: string path to the target file.
  // it defaults to the same basename as `source` in `options.cwd`.
  target: 'new.file',

  // optional: defaults to `process.cwd()`.
  cwd: 'some/other/dir',

  // optional: these default to those in the fs module.
  // may be overwritten (helpful in testing).
  mkdir: yourMkdir,
  rename: yourRename,
  copyFile: yourCopyFile,
  unlink: yourUnlink,

  // required: callback function accepts error.
  done: error => {
    // ...
  }
})

MIT License