From 19bab365cc924c03e2dc16aaddbdff224698eee4 Mon Sep 17 00:00:00 2001 From: Maksym Bobryk Date: Thu, 30 Oct 2025 00:18:04 +0200 Subject: [PATCH] Solution --- src/app.js | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 63 insertions(+), 1 deletion(-) diff --git a/src/app.js b/src/app.js index 0d15e7b..07822f2 100644 --- a/src/app.js +++ b/src/app.js @@ -1 +1,63 @@ -// write code here +/* eslint-disable no-console */ + +'use strict'; + +const fs = require('fs'); +const path = require('path'); + +function moveFile() { + const args = process.argv.slice(2); + const [source, destination] = args; + + if (!source || !destination) { + console.error('Both source and destination are required'); + + return; + } + + if (!fs.existsSync(source)) { + console.error('Source file does not exist'); + + return; + } + + const sourceStat = fs.statSync(source); + + if (!sourceStat.isFile()) { + console.error('Source is not a file'); + + return; + } + + let targetDestination = destination; + + if (fs.existsSync(destination)) { + const destinationStat = fs.statSync(destination); + + if (destinationStat.isDirectory()) { + targetDestination = path.join(destination, path.basename(source)); + } + } else { + if (destination.endsWith(path.sep)) { + console.error('Destination directory does not exist'); + + return; + } + + const parentDir = path.dirname(destination); + + if (!fs.existsSync(parentDir)) { + console.error('Parent directory does not exist'); + + return; + } + } + + try { + fs.renameSync(source, targetDestination); + } catch (e) { + console.error(e.message); + } +} + +moveFile();