From af48487296805bdb8330602b2b844e68adeeaf12 Mon Sep 17 00:00:00 2001 From: Yan Kazymyr Date: Wed, 1 Apr 2026 19:22:35 +0300 Subject: [PATCH 1/2] Solution --- src/app.js | 37 ++++++++++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/src/app.js b/src/app.js index 0d15e7b..57db6da 100644 --- a/src/app.js +++ b/src/app.js @@ -1 +1,36 @@ -// write code here +const fs = require('fs'); +const path = require('path'); + +function moveFiles() { + const [source, destination] = process.argv.slice(2); + + try { + let finalDest = destination; + + if (destination.endsWith('/')) { + if ( + !fs.existsSync(destination) || + !fs.statSync(destination).isDirectory() + ) { + throw new Error('Destination directory does not exist'); + } + + finalDest = path.join(destination, path.basename(source)); + + fs.renameSync(source, finalDest); + + return; + } + + if (fs.existsSync(destination) && fs.statSync(destination).isDirectory()) { + finalDest = path.join(destination, path.basename(source)); + } + + fs.renameSync(source, finalDest); + } catch (error) { + // eslint-disable-next-line no-console + console.error(error.message); + } +} + +moveFiles(); From 1d68807222bff59dcf43e26f42ee7a0517f789a1 Mon Sep 17 00:00:00 2001 From: Yan Kazymyr Date: Wed, 1 Apr 2026 19:33:30 +0300 Subject: [PATCH 2/2] Fix --- src/app.js | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/src/app.js b/src/app.js index 57db6da..4e85ba8 100644 --- a/src/app.js +++ b/src/app.js @@ -5,6 +5,14 @@ function moveFiles() { const [source, destination] = process.argv.slice(2); try { + if (!source || !destination) { + throw new Error('Invzlid arguments'); + } + + if (!fs.existsSync(source) || !fs.statSync(source).isFile()) { + throw new Error('Source is not a file'); + } + let finalDest = destination; if (destination.endsWith('/')) { @@ -16,13 +24,10 @@ function moveFiles() { } finalDest = path.join(destination, path.basename(source)); - - fs.renameSync(source, finalDest); - - return; - } - - if (fs.existsSync(destination) && fs.statSync(destination).isDirectory()) { + } else if ( + fs.existsSync(destination) && + fs.statSync(destination).isDirectory() + ) { finalDest = path.join(destination, path.basename(source)); }