From 054140fd663299949fe79e497c3eee49b823264f Mon Sep 17 00:00:00 2001 From: vilich Date: Sun, 28 Dec 2025 12:32:42 +0100 Subject: [PATCH 1/6] add --- src/app.js | 86 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) diff --git a/src/app.js b/src/app.js index 0d15e7b..b24fa2b 100644 --- a/src/app.js +++ b/src/app.js @@ -1 +1,87 @@ // write code here +/* eslint-disable no-console */ +const fs = require('fs'); +const path = require('path'); + +function moveFile() { + const [, , sourceArg, destinationArg] = process.argv; + + if (!sourceArg || !destinationArg) { + console.error('Not enough parameters'); + + return; + } + + const source = path.resolve(sourceArg); + const destination = path.resolve(destinationArg); + const isDestinationDirectory = destinationArg.endsWith(path.sep); + + if (!fs.existsSync(source)) { + console.error('Source does not exist'); + + return; + } + + const sourceStat = fs.statSync(source); + + if (!sourceStat.isFile()) { + console.error('Source is not a file'); + + return; + } + + if (isDestinationDirectory) { + if (!fs.existsSync(destination)) { + throw new Error('Destination directory does not exist'); + } + + const destStat = fs.statSync(destination); + + if (!destStat.isDirectory()) { + console.error('Destination is not a directory'); + + return; + } + + const finalPath = path.join(destination, path.basename(source)); + + fs.renameSync(source, finalPath); + + return; + } + + if (fs.existsSync(destination)) { + const destinationStat = fs.statSync(destination); + + if (destinationStat.isDirectory()) { + const finalPath = path.join(destination, path.basename(source)); + + fs.renameSync(source, finalPath); + + return; + } + } + + if (isDestinationDirectory) { + if ( + !fs.existsSync(destination) || + !fs.statSync(destination).isDirectory() + ) { + throw new Error('Destination directory does not exist'); + } + } else { + const parentDir = path.resolve(path.dirname(destination)); + + if (!fs.existsSync(parentDir) || !fs.statSync(parentDir).isDirectory()) { + console.error('Destination directory does not exist'); + + return; + } + } + + fs.renameSync(source, destination); +} + +moveFile(); + +module.exports = { moveFile }; From 24dca47cf9e6f5217b90b36a446c127ba76df962 Mon Sep 17 00:00:00 2001 From: vilich Date: Sun, 28 Dec 2025 12:38:24 +0100 Subject: [PATCH 2/6] add --- src/app.js | 93 +++++++++++++++--------------------------------------- 1 file changed, 25 insertions(+), 68 deletions(-) diff --git a/src/app.js b/src/app.js index b24fa2b..127e5f6 100644 --- a/src/app.js +++ b/src/app.js @@ -1,87 +1,44 @@ -// write code here /* eslint-disable no-console */ -const fs = require('fs'); + +const { rename } = require('fs/promises'); +const { statSync, existsSync } = require('fs'); const path = require('path'); -function moveFile() { - const [, , sourceArg, destinationArg] = process.argv; +async function app() { + const args = process.argv.slice(2); + const [source, destination] = args; - if (!sourceArg || !destinationArg) { - console.error('Not enough parameters'); + if (!source || !destination) { + console.error(`Two arguments was needed`); return; } - const source = path.resolve(sourceArg); - const destination = path.resolve(destinationArg); - const isDestinationDirectory = destinationArg.endsWith(path.sep); - - if (!fs.existsSync(source)) { - console.error('Source does not exist'); + if (existsSync(source) && !statSync(source).isFile()) { + console.error(`I can move just files! `); return; } - const sourceStat = fs.statSync(source); - - if (!sourceStat.isFile()) { - console.error('Source is not a file'); - - return; - } + const slicedDest = destination.endsWith('/') + ? destination.slice(0, -1) + : destination; - if (isDestinationDirectory) { - if (!fs.existsSync(destination)) { - throw new Error('Destination directory does not exist'); - } + try { + const isDestDir = + existsSync(slicedDest) && statSync(slicedDest).isDirectory(); - const destStat = fs.statSync(destination); + const finDest = isDestDir + ? path.join(slicedDest, path.basename(source)) + : slicedDest; - if (!destStat.isDirectory()) { - console.error('Destination is not a directory'); - - return; - } - - const finalPath = path.join(destination, path.basename(source)); - - fs.renameSync(source, finalPath); - - return; + await rename(source, finDest); + console.log(`${source} was moved to ${destination}`); + } catch (e) { + console.error(`The file could not be moved. Error: ${e}`); } - - if (fs.existsSync(destination)) { - const destinationStat = fs.statSync(destination); - - if (destinationStat.isDirectory()) { - const finalPath = path.join(destination, path.basename(source)); - - fs.renameSync(source, finalPath); - - return; - } - } - - if (isDestinationDirectory) { - if ( - !fs.existsSync(destination) || - !fs.statSync(destination).isDirectory() - ) { - throw new Error('Destination directory does not exist'); - } - } else { - const parentDir = path.resolve(path.dirname(destination)); - - if (!fs.existsSync(parentDir) || !fs.statSync(parentDir).isDirectory()) { - console.error('Destination directory does not exist'); - - return; - } - } - - fs.renameSync(source, destination); } -moveFile(); +app(); -module.exports = { moveFile }; +module.exports = { app }; From f09bbe8b50f7a8c11179bd1a7ce0d058dd20317a Mon Sep 17 00:00:00 2001 From: vilich Date: Sat, 3 Jan 2026 15:46:36 +0100 Subject: [PATCH 3/6] add1546 --- src/app.js | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/src/app.js b/src/app.js index 127e5f6..9d11d05 100644 --- a/src/app.js +++ b/src/app.js @@ -25,18 +25,28 @@ async function app() { : destination; try { - const isDestDir = - existsSync(slicedDest) && statSync(slicedDest).isDirectory(); + const endsWithSlash = destination.endsWith(path.sep); - const finDest = isDestDir - ? path.join(slicedDest, path.basename(source)) - : slicedDest; + if (endsWithSlash) { + if (!existsSync(slicedDest) || !statSync(slicedDest).isDirectory()) { + throw new Error('Destination directory does not exist'); + } + } + + const isDestDir = + existsSync(slicedDest) && statSync(slicedDest).isDirectory(); - await rename(source, finDest); - console.log(`${source} was moved to ${destination}`); + const finDest = isDestDir + ? path.join(slicedDest, path.basename(source)) + : slicedDest; + + await rename(source, finDest); + console.log(`${source} was moved to ${destination}`); } catch (e) { - console.error(`The file could not be moved. Error: ${e}`); + console.error(`The file could not be moved. Error: ${e.message}`); + throw e; // ⚠️ важливо для тестів } + } app(); From f44581da627bcdfb5a73451a1047125cd3e8986b Mon Sep 17 00:00:00 2001 From: vilich Date: Sat, 3 Jan 2026 15:48:50 +0100 Subject: [PATCH 4/6] add1548 --- src/app.js | 44 ++++++++++++++++++-------------------------- 1 file changed, 18 insertions(+), 26 deletions(-) diff --git a/src/app.js b/src/app.js index 9d11d05..8260db9 100644 --- a/src/app.js +++ b/src/app.js @@ -5,48 +5,40 @@ const { statSync, existsSync } = require('fs'); const path = require('path'); async function app() { - const args = process.argv.slice(2); - const [source, destination] = args; + const [source, destination] = process.argv.slice(2); if (!source || !destination) { - console.error(`Two arguments was needed`); - - return; + throw new Error('Two arguments was needed'); } - if (existsSync(source) && !statSync(source).isFile()) { - console.error(`I can move just files! `); + if (!existsSync(source)) { + throw new Error('Source file does not exist'); + } - return; + if (!statSync(source).isFile()) { + throw new Error('I can move just files!'); } - const slicedDest = destination.endsWith('/') + const endsWithSlash = destination.endsWith(path.sep); + const slicedDest = endsWithSlash ? destination.slice(0, -1) : destination; - try { - const endsWithSlash = destination.endsWith(path.sep); + const parentDir = endsWithSlash + ? slicedDest + : path.dirname(slicedDest); - if (endsWithSlash) { - if (!existsSync(slicedDest) || !statSync(slicedDest).isDirectory()) { - throw new Error('Destination directory does not exist'); - } + if (!existsSync(parentDir) || !statSync(parentDir).isDirectory()) { + throw new Error('Destination directory does not exist'); } - const isDestDir = - existsSync(slicedDest) && statSync(slicedDest).isDirectory(); - - const finDest = isDestDir - ? path.join(slicedDest, path.basename(source)) + const finalDest = statSync(parentDir).isDirectory() && endsWithSlash + ? path.join(parentDir, path.basename(source)) : slicedDest; - await rename(source, finDest); - console.log(`${source} was moved to ${destination}`); - } catch (e) { - console.error(`The file could not be moved. Error: ${e.message}`); - throw e; // ⚠️ важливо для тестів - } + await rename(source, finalDest); + console.log(`${source} was moved to ${destination}`); } app(); From 83c7a361eea8c89d187e5136c2bd74e33878fdef Mon Sep 17 00:00:00 2001 From: vilich Date: Sat, 3 Jan 2026 15:50:32 +0100 Subject: [PATCH 5/6] add1550 --- src/app.js | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/app.js b/src/app.js index 8260db9..d1d7a4c 100644 --- a/src/app.js +++ b/src/app.js @@ -36,11 +36,15 @@ async function app() { ? path.join(parentDir, path.basename(source)) : slicedDest; - await rename(source, finalDest); - - console.log(`${source} was moved to ${destination}`); +await rename(source, finalDest); +console.log(`${source} was moved to ${destination}`); } -app(); +try { + app(); +} catch (error) { + console.error(error.message); + process.exit(1); +} module.exports = { app }; From 4f450149f9676d13249574e8581d81b8ad05716b Mon Sep 17 00:00:00 2001 From: vilich Date: Sat, 3 Jan 2026 15:52:07 +0100 Subject: [PATCH 6/6] add1552 --- src/app.js | 101 ++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 69 insertions(+), 32 deletions(-) diff --git a/src/app.js b/src/app.js index d1d7a4c..b24fa2b 100644 --- a/src/app.js +++ b/src/app.js @@ -1,50 +1,87 @@ +// write code here /* eslint-disable no-console */ - -const { rename } = require('fs/promises'); -const { statSync, existsSync } = require('fs'); +const fs = require('fs'); const path = require('path'); -async function app() { - const [source, destination] = process.argv.slice(2); +function moveFile() { + const [, , sourceArg, destinationArg] = process.argv; + + if (!sourceArg || !destinationArg) { + console.error('Not enough parameters'); - if (!source || !destination) { - throw new Error('Two arguments was needed'); + return; } - if (!existsSync(source)) { - throw new Error('Source file does not exist'); + const source = path.resolve(sourceArg); + const destination = path.resolve(destinationArg); + const isDestinationDirectory = destinationArg.endsWith(path.sep); + + if (!fs.existsSync(source)) { + console.error('Source does not exist'); + + return; } - if (!statSync(source).isFile()) { - throw new Error('I can move just files!'); + const sourceStat = fs.statSync(source); + + if (!sourceStat.isFile()) { + console.error('Source is not a file'); + + return; } - const endsWithSlash = destination.endsWith(path.sep); - const slicedDest = endsWithSlash - ? destination.slice(0, -1) - : destination; + if (isDestinationDirectory) { + if (!fs.existsSync(destination)) { + throw new Error('Destination directory does not exist'); + } + + const destStat = fs.statSync(destination); + + if (!destStat.isDirectory()) { + console.error('Destination is not a directory'); + + return; + } + + const finalPath = path.join(destination, path.basename(source)); - const parentDir = endsWithSlash - ? slicedDest - : path.dirname(slicedDest); + fs.renameSync(source, finalPath); - if (!existsSync(parentDir) || !statSync(parentDir).isDirectory()) { - throw new Error('Destination directory does not exist'); + return; } - const finalDest = statSync(parentDir).isDirectory() && endsWithSlash - ? path.join(parentDir, path.basename(source)) - : slicedDest; + if (fs.existsSync(destination)) { + const destinationStat = fs.statSync(destination); -await rename(source, finalDest); -console.log(`${source} was moved to ${destination}`); -} + if (destinationStat.isDirectory()) { + const finalPath = path.join(destination, path.basename(source)); + + fs.renameSync(source, finalPath); + + return; + } + } + + if (isDestinationDirectory) { + if ( + !fs.existsSync(destination) || + !fs.statSync(destination).isDirectory() + ) { + throw new Error('Destination directory does not exist'); + } + } else { + const parentDir = path.resolve(path.dirname(destination)); -try { - app(); -} catch (error) { - console.error(error.message); - process.exit(1); + if (!fs.existsSync(parentDir) || !fs.statSync(parentDir).isDirectory()) { + console.error('Destination directory does not exist'); + + return; + } + } + + fs.renameSync(source, destination); } -module.exports = { app }; +moveFile(); + +module.exports = { moveFile };