From 8ca224ea2e1fe5aa66fb82f12d1f1215aa2e268e Mon Sep 17 00:00:00 2001 From: Alina Luchytska Date: Tue, 17 Feb 2026 19:31:01 +0200 Subject: [PATCH 1/4] Solution --- src/app.js | 78 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) diff --git a/src/app.js b/src/app.js index 0d15e7b..50d3392 100644 --- a/src/app.js +++ b/src/app.js @@ -1 +1,79 @@ +/* eslint-disable no-console */ // write code here +const fs = require('fs/promises'); +const path = require('path'); + +async function validateSource(source) { + const sourceStat = await fs.stat(source); + + if (!sourceStat.isFile()) { + throw new Error('Source is not a file'); + } +} + +async function setDestPath(destination, source) { + let destPath; + + if (destination.endsWith('/')) { + try { + const destStat = await fs.stat(destination); + + if (!destStat.isDirectory()) { + throw new Error('Destination is not a directory'); + } + + destPath = path.join(destination, path.basename(source)); + } catch (error) { + throw new Error('Destination path is invalid'); + } + } else { + try { + const destStat = await fs.stat(destination); + + if (destStat.isDirectory()) { + destPath = path.join(destination, path.basename(source)); + } else { + destPath = destination; + } + } catch (error) { + destPath = destination; + } + } + + return destPath; +} + +async function main() { + const [source, destination] = process.argv.slice(2); + + if (!source || !destination) { + console.error('Source and destination paths are required'); + + return; + } + + const absoluteSource = path.resolve(source); + const absoluteDestination = path.resolve(destination); + + if (absoluteSource === absoluteDestination) { + return; + } + + try { + await validateSource(absoluteSource); + } catch (error) { + console.error('Error validating source file: ' + error.message); + + return; + } + + const destPath = await setDestPath(destination, absoluteSource); + + try { + await fs.rename(absoluteSource, destPath); + } catch (error) { + console.error('Error moving file: ' + error.message); + } +} + +main(); From fb0cbcc77dccd398085e09d98da7dec58493cee6 Mon Sep 17 00:00:00 2001 From: Alina Luchytska Date: Wed, 18 Feb 2026 21:34:11 +0200 Subject: [PATCH 2/4] fix solution --- src/app.js | 32 ++++++++++++++++++++++++-------- 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/src/app.js b/src/app.js index 50d3392..1d08e56 100644 --- a/src/app.js +++ b/src/app.js @@ -14,14 +14,13 @@ async function validateSource(source) { async function setDestPath(destination, source) { let destPath; - if (destination.endsWith('/')) { + if (destination.endsWith(path.sep)) { try { const destStat = await fs.stat(destination); if (!destStat.isDirectory()) { throw new Error('Destination is not a directory'); } - destPath = path.join(destination, path.basename(source)); } catch (error) { throw new Error('Destination path is invalid'); @@ -36,11 +35,22 @@ async function setDestPath(destination, source) { destPath = destination; } } catch (error) { - destPath = destination; + const parentDir = path.dirname(destination); + + try { + const parentStat = await fs.stat(parentDir); + + if (!parentStat.isDirectory()) { + throw new Error('Destination parent is not a directory'); + } + destPath = destination; + } catch (parentError) { + throw new Error('Destination directory does not exist'); + } } } - return destPath; + return path.resolve(destPath); } async function main() { @@ -53,9 +63,17 @@ async function main() { } const absoluteSource = path.resolve(source); - const absoluteDestination = path.resolve(destination); + let destPath; - if (absoluteSource === absoluteDestination) { + try { + destPath = await setDestPath(destination, absoluteSource); + } catch (error) { + console.error('Error with destination: ' + error.message); + + return; + } + + if (absoluteSource === destPath) { return; } @@ -67,8 +85,6 @@ async function main() { return; } - const destPath = await setDestPath(destination, absoluteSource); - try { await fs.rename(absoluteSource, destPath); } catch (error) { From bcbdca0dd037904573fe3e680b7535628e2e60c9 Mon Sep 17 00:00:00 2001 From: Alina Luchytska Date: Thu, 19 Feb 2026 00:03:07 +0200 Subject: [PATCH 3/4] fixed solution --- src/app.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/app.js b/src/app.js index 1d08e56..865c93f 100644 --- a/src/app.js +++ b/src/app.js @@ -19,11 +19,11 @@ async function setDestPath(destination, source) { const destStat = await fs.stat(destination); if (!destStat.isDirectory()) { - throw new Error('Destination is not a directory'); + throw new Error('Destination is not a directory!'); } destPath = path.join(destination, path.basename(source)); } catch (error) { - throw new Error('Destination path is invalid'); + throw new Error('Destination path is invalid!'); } } else { try { From 53e9225d3097827ad77a8a6e5d431af95b42b648 Mon Sep 17 00:00:00 2001 From: Alina Luchytska Date: Thu, 19 Feb 2026 15:07:50 +0200 Subject: [PATCH 4/4] fix --- src/app.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/app.js b/src/app.js index 865c93f..59d53fd 100644 --- a/src/app.js +++ b/src/app.js @@ -14,7 +14,7 @@ async function validateSource(source) { async function setDestPath(destination, source) { let destPath; - if (destination.endsWith(path.sep)) { + if (destination.endsWith('/') || destination.endsWith(path.sep)) { try { const destStat = await fs.stat(destination); @@ -23,7 +23,7 @@ async function setDestPath(destination, source) { } destPath = path.join(destination, path.basename(source)); } catch (error) { - throw new Error('Destination path is invalid!'); + throw new Error('Destination directory does not exist'); } } else { try { @@ -73,10 +73,6 @@ async function main() { return; } - if (absoluteSource === destPath) { - return; - } - try { await validateSource(absoluteSource); } catch (error) { @@ -85,6 +81,10 @@ async function main() { return; } + if (absoluteSource === destPath) { + return; + } + try { await fs.rename(absoluteSource, destPath); } catch (error) {