From 6f3ee35c41daf695dd8cd9729ce458b8eb2b2ebc Mon Sep 17 00:00:00 2001 From: Vlad Pashko Date: Fri, 1 Mar 2024 11:05:19 +0200 Subject: [PATCH] solution --- src/app.js | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/src/app.js b/src/app.js index ad9a93a..b0044fb 100644 --- a/src/app.js +++ b/src/app.js @@ -1 +1,45 @@ +/* eslint-disable no-console */ 'use strict'; + +const fs = require('fs'); +const path = require('path'); + +const [, , sourceFile, destinationFile] = process.argv; + +function copyFile() { + if (!sourceFile || !destinationFile) { + console.error('Usage: node app.js '); + + return; + } + + if (path.resolve(sourceFile) === path.resolve(destinationFile)) { + console.log('Source and destination are the same. Nothing to copy.'); + + return; + } + + fs.readFile(sourceFile, (err, data) => { + if (err) { + console.error('Error reading source file:', err); + + return; + } + + fs.writeFile(destinationFile, data, (error) => { + if (error) { + console.error('Error writing to destination file:', err); + + return; + } + + console.log(`File copied from ${sourceFile} to ${destinationFile}`); + }); + }); +} + +copyFile(); + +module.exports = { + copyFile: copyFile, +};