From c614498b323c6a86032437a49015f9f900e7fe7d Mon Sep 17 00:00:00 2001 From: dminikulin Date: Mon, 29 Sep 2025 13:47:24 +0300 Subject: [PATCH 1/3] Add solution --- src/app.js | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) diff --git a/src/app.js b/src/app.js index 0d15e7b..3f39407 100644 --- a/src/app.js +++ b/src/app.js @@ -1 +1,76 @@ +/* eslint-disable no-console */ // write code here +'use strict'; + +const fs = require('fs/promises'); +const path = require('path'); + +async function move(oldPath, newPath) { + if (!oldPath || !newPath) { + console.error('Source and destination are required.'); + + return; + } + + if (oldPath === newPath) { + return; + } + + try { + const src = oldPath; + let dest = newPath; + const srcStats = await fs.stat(src); + + if (!srcStats.isFile()) { + console.error('Source is not a file'); + + return; + } + + try { + const destStats = await fs.stat(newPath); + + if (destStats.isDirectory()) { + const baseName = path.basename(src); + + dest = path.join(newPath, baseName); + } + } catch (err) { + const destDir = path.dirname(newPath); + + try { + const dirStats = await fs.stat(destDir); + + if (!dirStats.isDirectory()) { + console.error('Destination directory does not exist'); + + return; + } + } catch { + console.error('Destination directory does not exist'); + + return; + } + + dest = newPath; + } + await fs.rename(src, dest); + console.log(`Moved ${src} to ${dest}`); + } catch (err) { + console.error('Move error:', err.message); + } +} + +if (require.main === module) { + const args = process.argv.slice(2); + + if (args.length !== 2) { + console.error('Exactly two arguments are required'); + } + + const [oldPath, newPath] = args; + + move(oldPath, newPath); +} + +module.exports = { move }; From c52612845f0ce769a1c7bd982435e8a9387193a1 Mon Sep 17 00:00:00 2001 From: dminikulin Date: Mon, 29 Sep 2025 14:23:58 +0300 Subject: [PATCH 2/3] Fixed what AI Buddy asked to --- src/app.js | 78 +++++++++++++++++++++++++++++------------------------- 1 file changed, 42 insertions(+), 36 deletions(-) diff --git a/src/app.js b/src/app.js index 3f39407..f9a8bb9 100644 --- a/src/app.js +++ b/src/app.js @@ -6,56 +6,59 @@ const fs = require('fs/promises'); const path = require('path'); async function move(oldPath, newPath) { - if (!oldPath || !newPath) { - console.error('Source and destination are required.'); - - return; - } + try { + if (!oldPath || !newPath) { + throw new Error('Exactly two arguments are required'); + } - if (oldPath === newPath) { - return; - } + if (oldPath === newPath) { + return; + } - try { - const src = oldPath; - let dest = newPath; - const srcStats = await fs.stat(src); + const srcStats = await fs.stat(oldPath); if (!srcStats.isFile()) { - console.error('Source is not a file'); - - return; + throw new Error('Source is not a file'); } - try { - const destStats = await fs.stat(newPath); + const trailing = newPath.endsWith(path.sep) || newPath.endsWith('/'); + let dest; - if (destStats.isDirectory()) { - const baseName = path.basename(src); + if (trailing) { + const destStats = await fs.stat(newPath); - dest = path.join(newPath, baseName); + if (!destStats.isDirectory()) { + throw new Error('Destination is not a directory'); } - } catch (err) { - const destDir = path.dirname(newPath); + dest = path.join(newPath, path.basename(oldPath)); + } else { try { - const dirStats = await fs.stat(destDir); - - if (!dirStats.isDirectory()) { - console.error('Destination directory does not exist'); + const destStats = await fs.stat(newPath); - return; + if (destStats.isDirectory()) { + dest = path.join(newPath, path.basename(oldPath)); + } else { + dest = newPath; + } + } catch (err) { + if (err.code === 'ENOENT') { + const destDir = path.dirname(newPath); + const dirStats = await fs.stat(destDir); + + if (!dirStats.isDirectory()) { + throw new Error('Destination directory does not exist'); + } + + dest = newPath; + } else { + throw err; } - } catch { - console.error('Destination directory does not exist'); - - return; } - - dest = newPath; } - await fs.rename(src, dest); - console.log(`Moved ${src} to ${dest}`); + + await fs.rename(oldPath, dest); + console.log(`Moved ${oldPath} to ${dest}`); } catch (err) { console.error('Move error:', err.message); } @@ -70,7 +73,10 @@ if (require.main === module) { const [oldPath, newPath] = args; - move(oldPath, newPath); + move(oldPath, newPath).catch((err) => { + console.error(err); + process.exit(1); + }); } module.exports = { move }; From 9138a4b1ece26c4ae67a793e8e843abd73ad79e8 Mon Sep 17 00:00:00 2001 From: dminikulin Date: Mon, 29 Sep 2025 16:15:08 +0300 Subject: [PATCH 3/3] Change README file --- readme.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/readme.md b/readme.md index 119d00e..a65c4d1 100644 --- a/readme.md +++ b/readme.md @@ -2,7 +2,7 @@ **Read [the guideline](https://github.com/mate-academy/js_task-guideline/blob/master/README.md) before start** -Write an app that will move a file from one location to another like Linux mv +Write an app that will move a file from one location to another like Linux (or Mac) mv command: `mv file.txt ./someDir/` (this will create `file.txt` in `./someDir` and remove the source file). - If a destination contains `/` in the end it must be considered as a directory.