From 2c8dad1e3620fa9d13ed12231e2535348abf7f7d Mon Sep 17 00:00:00 2001 From: Inna K Date: Mon, 29 Jun 2026 19:54:01 +0300 Subject: [PATCH] Solution --- src/app.js | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 57 insertions(+), 1 deletion(-) diff --git a/src/app.js b/src/app.js index 0d15e7b..ab28261 100644 --- a/src/app.js +++ b/src/app.js @@ -1 +1,57 @@ -// write code here +'use strict'; + +const fs = require('fs'); +const path = require('path'); + +function writeError(message) { + process.stderr.write(`${message}\n`); +} + +function main() { + const [source, destination] = process.argv.slice(2); + + if (!source || !destination) { + writeError('Source and destination are required'); + + return; + } + + if (!fs.existsSync(source)) { + writeError('Source file does not exist'); + + return; + } + + if (source === destination) { + return; + } + + let finalDestination = destination; + + if (destination.endsWith('/') || destination.endsWith('\\')) { + if (!fs.existsSync(destination)) { + writeError('Destination directory does not exist'); + + return; + } + + finalDestination = path.join(destination, path.basename(source)); + } else if ( + fs.existsSync(destination) && + fs.statSync(destination).isDirectory() + ) { + finalDestination = path.join(destination, path.basename(source)); + } else { + const destinationDir = path.dirname(destination); + + if (destinationDir !== '.' && !fs.existsSync(destinationDir)) { + writeError('Destination directory does not exist'); + + return; + } + } + + fs.renameSync(source, finalDestination); +} + +main();