From b4a4fa2507b93cc8cb48336a0e9c8134f3f317e0 Mon Sep 17 00:00:00 2001 From: Iryna Knyzh Date: Tue, 10 Feb 2026 12:59:42 +0200 Subject: [PATCH 1/2] task --- src/app.js | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/src/app.js b/src/app.js index 0d15e7b..1683a01 100644 --- a/src/app.js +++ b/src/app.js @@ -1 +1,59 @@ // write code here +/* eslint-disable no-console */ +const fs = require('fs/promises'); +const path = require('path'); + +async function isDirectoryExists(directoryPath) { + try { + const stats = await fs.stat(directoryPath); + + return stats.isDirectory(); + } catch (error) { + if (error.code === 'ENOENT') { + return false; + } + throw error; + } +} + +async function moveFile() { + const [, , source, destination] = process.argv; + + if (!source || !destination) { + console.error('Please provide both source and destination paths.'); + + return; + } + + if (source === destination) { + return; + } + + const lastChar = destination[destination.length - 1]; + let newDestination = destination; + + if (lastChar === '/') { + const stat = await fs.stat(destination); + + if (!stat.isDirectory()) { + throw new Error('directory not exists'); + } + + newDestination = destination + path.basename(source); + } else { + const directoryExists = await isDirectoryExists(destination); + + if (directoryExists) { + newDestination = destination + '/' + path.basename(source); + } + } + + try { + await fs.rename(source, newDestination); + console.log(`Moved '${source}' to '${newDestination}'`); + } catch (error) { + console.error(`Error moving file: ${error.message}`); + } +} + +moveFile(); From f82e9db55412a0e5bff727c7d4bbf1e5f27b68bf Mon Sep 17 00:00:00 2001 From: Iryna Knyzh Date: Tue, 10 Feb 2026 13:04:12 +0200 Subject: [PATCH 2/2] fix --- src/app.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/app.js b/src/app.js index 1683a01..0670e50 100644 --- a/src/app.js +++ b/src/app.js @@ -33,10 +33,12 @@ async function moveFile() { let newDestination = destination; if (lastChar === '/') { - const stat = await fs.stat(destination); + const directoryExists = await isDirectoryExists(destination); + + if (!directoryExists) { + console.error('directory not exists'); - if (!stat.isDirectory()) { - throw new Error('directory not exists'); + return; } newDestination = destination + path.basename(source);