From 0eb80108139ae512157b729748ba747877275ef7 Mon Sep 17 00:00:00 2001 From: Denis Kononenko Date: Fri, 23 Feb 2024 09:24:58 +0200 Subject: [PATCH] fix solution --- src/app.js | 36 +++-------------------------------- src/modules/customCopyFile.js | 35 ++++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 33 deletions(-) create mode 100644 src/modules/customCopyFile.js diff --git a/src/app.js b/src/app.js index 9101241..2750a30 100644 --- a/src/app.js +++ b/src/app.js @@ -1,37 +1,7 @@ 'use strict'; -const fs = require('fs'); -const path = require('path'); +const { customCopyFile } = require('./modules/customCopyFile'); -const customCopyFile = () => { - const inputArgs = process.argv.slice(2); +const [sourceFile, destinationFile] = process.argv.slice(2); - if (inputArgs.length !== 2) { - process.stderr.write('should be 2 params'); - - return; - } - - const [sourceFile, destinationFile] = inputArgs; - - if (sourceFile === destinationFile) { - return; - } - - const sourcePath = path.join(__dirname, '..', sourceFile); - const destinationPath = path.join(__dirname, '..', destinationFile); - - try { - fs.copyFileSync(sourcePath, destinationPath); - } catch (err) { - if (err.code === 'EISDIR' || err.code === 'EPERM') { - process.stderr.write('source or destination is a directory'); - } - - if (err.code === 'ENOENT') { - process.stderr.write('non-existent source file'); - } - } -}; - -customCopyFile(); +customCopyFile(sourceFile, destinationFile); diff --git a/src/modules/customCopyFile.js b/src/modules/customCopyFile.js new file mode 100644 index 0000000..679afe8 --- /dev/null +++ b/src/modules/customCopyFile.js @@ -0,0 +1,35 @@ +'use strict'; + +const fs = require('fs'); +const path = require('path'); + +const customCopyFile = (sourceFile, destinationFile) => { + if (sourceFile === undefined || destinationFile === undefined) { + process.stderr.write('should be 2 params'); + + return; + } + + if (sourceFile === destinationFile) { + return; + } + + const sourcePath = path.join(__dirname, '..', '..', sourceFile); + const destinationPath = path.join(__dirname, '..', '..', destinationFile); + + try { + fs.copyFileSync(sourcePath, destinationPath); + } catch (err) { + if (err.code === 'EISDIR' || err.code === 'EPERM') { + process.stderr.write('source or destination is a directory'); + } + + if (err.code === 'ENOENT') { + process.stderr.write('non-existent source file'); + } + } +}; + +module.exports = { + customCopyFile, +};