From 41c9a6a995db41b01836abe4275bb04dfdf8700b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D0=B0=D1=88=D0=B0?= Date: Fri, 31 Oct 2025 18:08:10 +0200 Subject: [PATCH 1/3] add task solution --- src/app.js | 72 +++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 71 insertions(+), 1 deletion(-) diff --git a/src/app.js b/src/app.js index 0d15e7b..4801392 100644 --- a/src/app.js +++ b/src/app.js @@ -1 +1,71 @@ -// write code here +/* eslint-disable no-console */ + +const fs = require('fs'); +const path = require('path'); + +const args = process.argv.slice(2); + +function moveFile(src, dest) { + if (args.length !== 2 || !src || !dest) { + console.error(new Error('Має бути 2 аргумента')); + + return; + } + + if (args.some((a) => a.startsWith('-'))) { + console.error(new Error('Недійсні аргументи: прапорці заборонені')); + + return; + } + + const filePath = path.resolve(src); + + const fileStats = fs.statSync(filePath, { throwIfNoEntry: false }); + + if (!fileStats || !fileStats.isFile()) { + console.error( + new Error('Вихідний файл не існує або не є звичайним файлом'), + ); + + return; + } + + let finalDestination = dest; + + const destinationExists = fs.existsSync(dest); + + if (destinationExists) { + const destinationStats = fs.statSync(dest); + + if (destinationStats.isDirectory()) { + finalDestination = path.join(dest, path.basename(src)); + } else { + finalDestination = dest; + } + } else { + const endsWithSlash = dest.endsWith(path.sep); + + if (endsWithSlash) { + console.error('Каталог призначення не існує'); + + return; + } + + const parentDir = path.dirname(dest); + + if (!fs.existsSync(parentDir)) { + console.error('Батьківський каталог призначення не існує'); + + return; + } + } + + try { + fs.renameSync(filePath, finalDestination); + console.log(`File moved successfully to ${finalDestination}`); + } catch (err) { + console.error(err); + } +} + +moveFile(args[0], args[1]); From 593477d00ff0d80131731dc9f354a2e13dbafa2f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D0=B0=D1=88=D0=B0?= Date: Fri, 31 Oct 2025 18:16:44 +0200 Subject: [PATCH 2/3] add task --- src/app.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/app.js b/src/app.js index 4801392..7e73148 100644 --- a/src/app.js +++ b/src/app.js @@ -58,6 +58,14 @@ function moveFile(src, dest) { return; } + + const parentStats = fs.statSync(parentDir, { throwIfNoEntry: false }); + + if (!parentStats || !parentStats.isDirectory()) { + console.error(new Error('Призначення не є каталогом')); + + return; + } } try { From 2d3f9b17a3e79055dd4dd7e70f5f7adf77aa888f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D0=B0=D1=88=D0=B0?= Date: Fri, 31 Oct 2025 18:39:28 +0200 Subject: [PATCH 3/3] add --- src/app.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/app.js b/src/app.js index 7e73148..26daa34 100644 --- a/src/app.js +++ b/src/app.js @@ -40,6 +40,12 @@ function moveFile(src, dest) { if (destinationStats.isDirectory()) { finalDestination = path.join(dest, path.basename(src)); } else { + if (dest.endsWith(path.sep) || dest.endsWith('/')) { + console.error(new Error('Призначення не є каталогом')); + + return; + } + finalDestination = dest; } } else {