Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- async function copyFile(src, dest) {
- const readStream = fs.createReadStream(src)
- const writeStream = fs.createWriteStream(dest, { flags: 'wx' })
- const errors = ['EISDIR', 'EACCES','EEXIST']
- readStream.on('error', err => {
- if (err.code === 'EISDIR') {
- console.log(`Source ${src} is a directory`)
- }
- if (err.code === 'EACCES') {
- console.log(`Can not read the source ${src} due to lack of permissions`)
- }
- if (err.code === 'ENOENT') {
- console.log(`Source file ${src} is absent`)
- }
- writeStream.destroy()
- fs.remove(dest).catch(err => console.log(err))
- })
- writeStream.on('error', err => {
- if (err.code === 'EISDIR') {
- console.log(`Source ${src} is a directory`)
- }
- if (err.code === 'EACCES') {
- console.log(`Can not write to destination ${dest} due to lack of permissions`)
- }
- if (err.code === 'EEXIST') {
- console.log(`Destination file ${src} already present`)
- }
- readStream.destroy()
- if (!errors.indexOf(err.code) > -1) {
- fs.remove(dest).catch(err => console.log(err))
- }
- })
- readStream.pipe(writeStream)
- }
Add Comment
Please, Sign In to add comment