Simply copy a file. If an error occurs after the destination file has been opened for writing, copy-file will attempt to remove the destination. Correctly copies symlinks.
npm i @cloudcmd/copy-file
src<string>- source filename to copydest<string>- destination filename of the copy operationstreams<array>- file processing streams (optional)
const copyFile = require('@cloudcmd/copy-file');
const ok = () => 'ok';
const error = (e) => e.message;
copyFile('./package.json', './package2.json')
.then(ok)
.catch(error)
.then(console.log);You can use preprocessing streams of copied file:
const copyFile = require('@cloudcmd/copy-file');
const zlib = require('zlib');
const gzip = zlib.createGzip();
const ok = () => 'ok';
const error = (e) => e.message;
copyFile('./package.json', './package2.json', [gzip])
.then(ok)
.catch(error)
.then(console.log);You can use copyFile with async-await:
const tryToCatch = require('try-to-catch');
const copyFile = require('@cloudcmd/copy-file');
(async () => {
const [e] = await tryToCatch(copyFile, './package.json', './package2.json');
if (!e)
return;
console.error(e.message);
})();- fs-copy-file - Node.js
v8.5.0fs.copyFileponyfill - fs-copy-file-sync - Node.js
v8.5.0fs.copyFileponyfill - copy-symlink - Copy symlink because
fs.copyFilecan't.
MIT