From 13234eb324238779150017248414f301ab0e9320 Mon Sep 17 00:00:00 2001 From: Ivan Ryabukha Date: Mon, 22 Sep 2025 16:32:05 +0300 Subject: [PATCH 1/3] task solution --- src/app.js | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 72 insertions(+), 1 deletion(-) diff --git a/src/app.js b/src/app.js index 0d15e7b..53fb903 100644 --- a/src/app.js +++ b/src/app.js @@ -1 +1,72 @@ -// write code here +const fs = require('fs'); +const path = require('path'); + +function move() { + if (process.argv.slice(2).length > 2) { + // eslint-disable-next-line no-console + console.error( + 'Wrong number of arguments is supplied, should pass 2 arguments', + ); + + return; + } + + const [sourceFile, destinationFile] = process.argv.slice(2); + + if (!sourceFile || !destinationFile) { + // eslint-disable-next-line no-console + console.error('One or two params is undefined'); + + return; + } + + if (!fs.existsSync(sourceFile)) { + // eslint-disable-next-line no-console + console.error('Non-existent source file'); + + return; + } + + const dirName = path.dirname(destinationFile); + + if (!fs.existsSync(dirName)) { + // eslint-disable-next-line no-console + console.error('Destination directory does not exist'); + + return; + } + + const normalizedSource = path.resolve(sourceFile); + const normalizedDest = path.resolve(destinationFile); + + if (normalizedSource === normalizedDest) { + return; + } + + if ( + fs.existsSync(normalizedDest) && + fs.statSync(normalizedDest).isDirectory() + ) { + const newFilePath = path.join(normalizedDest, path.basename(sourceFile)); + + fs.renameSync(sourceFile, newFilePath); + + return; + } + + if (destinationFile.endsWith(path.sep)) { + const newFilePath = path.join(normalizedDest, path.basename(sourceFile)); + + fs.renameSync(sourceFile, newFilePath); + + return; + } + + fs.renameSync(sourceFile, normalizedDest); +} + +move(); + +module.exports = { + move, +}; From 05aab03c4201cbceb6610d876a36a2579c015caa Mon Sep 17 00:00:00 2001 From: Ivan Ryabukha Date: Tue, 23 Sep 2025 12:41:40 +0300 Subject: [PATCH 2/3] fix --- src/app.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/app.js b/src/app.js index 53fb903..f2343a1 100644 --- a/src/app.js +++ b/src/app.js @@ -62,7 +62,12 @@ function move() { return; } - fs.renameSync(sourceFile, normalizedDest); + const renameFile = path.join( + path.dirname(normalizedSource), + path.basename(destinationFile), + ); + + fs.renameSync(sourceFile, renameFile); } move(); From be050e2ecd12cd68f66540074ecf21974a1b2475 Mon Sep 17 00:00:00 2001 From: Ivan Ryabukha Date: Tue, 23 Sep 2025 13:02:30 +0300 Subject: [PATCH 3/3] hotfix --- src/app.js | 16 ++-------------- 1 file changed, 2 insertions(+), 14 deletions(-) diff --git a/src/app.js b/src/app.js index f2343a1..194bf8a 100644 --- a/src/app.js +++ b/src/app.js @@ -2,7 +2,7 @@ const fs = require('fs'); const path = require('path'); function move() { - if (process.argv.slice(2).length > 2) { + if (process.argv.slice(2).length !== 2) { // eslint-disable-next-line no-console console.error( 'Wrong number of arguments is supplied, should pass 2 arguments', @@ -13,13 +13,6 @@ function move() { const [sourceFile, destinationFile] = process.argv.slice(2); - if (!sourceFile || !destinationFile) { - // eslint-disable-next-line no-console - console.error('One or two params is undefined'); - - return; - } - if (!fs.existsSync(sourceFile)) { // eslint-disable-next-line no-console console.error('Non-existent source file'); @@ -62,12 +55,7 @@ function move() { return; } - const renameFile = path.join( - path.dirname(normalizedSource), - path.basename(destinationFile), - ); - - fs.renameSync(sourceFile, renameFile); + fs.renameSync(sourceFile, normalizedDest); } move();