From 09d7535d60c3d99cd997c5a97fbfbfae284f4b0f Mon Sep 17 00:00:00 2001 From: Sidach Ruslan Date: Mon, 22 Jun 2026 12:18:33 +0300 Subject: [PATCH 1/2] add solution --- src/app.js | 54 +++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 53 insertions(+), 1 deletion(-) diff --git a/src/app.js b/src/app.js index 0d15e7b..f0c9e46 100644 --- a/src/app.js +++ b/src/app.js @@ -1 +1,53 @@ -// write code here +const fsPromises = require('fs/promises'); +const fs = require('fs'); +const path = require('path'); + +async function main() { + const [source, destination] = process.argv.slice(2); + + try { + if (!source || !destination) { + throw new Error('Missing arguments'); + } + + if (!fs.existsSync(source)) { + throw new Error('Source file does not exist'); + } + + let correctPath; + + if (destination.endsWith('/')) { + if ( + !fs.existsSync(destination) || + !fs.statSync(destination).isDirectory() + ) { + throw new Error('Destination directory does not exist'); + } + + correctPath = path.join(destination, path.basename(source)); + } else if ( + fs.existsSync(destination) && + fs.statSync(destination).isDirectory() + ) { + correctPath = path.join(destination, path.basename(source)); + } else { + const parentDir = path.dirname(destination); + + if (parentDir !== '.' && !fs.existsSync(parentDir)) { + throw new Error('Parent directory does not exist'); + } + + correctPath = destination; + } + + const dataFile = await fsPromises.readFile(source, 'utf-8'); + + await fsPromises.unlink(source); + await fsPromises.writeFile(correctPath, dataFile, 'utf-8'); + } catch (e) { + throw e; + } +} + +// eslint-disable-next-line no-console +main().catch((e) => console.error(e.massage)); From 2c90d675dbbfc0a53eb8604fa566895c9554b4de Mon Sep 17 00:00:00 2001 From: Sidach Ruslan Date: Mon, 22 Jun 2026 12:34:22 +0300 Subject: [PATCH 2/2] add fix --- src/app.js | 66 +++++++++++++++++++++++++----------------------------- 1 file changed, 31 insertions(+), 35 deletions(-) diff --git a/src/app.js b/src/app.js index f0c9e46..2b46c4c 100644 --- a/src/app.js +++ b/src/app.js @@ -5,49 +5,45 @@ const path = require('path'); async function main() { const [source, destination] = process.argv.slice(2); - try { - if (!source || !destination) { - throw new Error('Missing arguments'); - } - - if (!fs.existsSync(source)) { - throw new Error('Source file does not exist'); - } + if (!source || !destination) { + throw new Error('Missing arguments'); + } - let correctPath; + if (!fs.existsSync(source)) { + throw new Error('Source file does not exist'); + } - if (destination.endsWith('/')) { - if ( - !fs.existsSync(destination) || - !fs.statSync(destination).isDirectory() - ) { - throw new Error('Destination directory does not exist'); - } + let correctPath; - correctPath = path.join(destination, path.basename(source)); - } else if ( - fs.existsSync(destination) && - fs.statSync(destination).isDirectory() + if (destination.endsWith('/')) { + if ( + !fs.existsSync(destination) || + !fs.statSync(destination).isDirectory() ) { - correctPath = path.join(destination, path.basename(source)); - } else { - const parentDir = path.dirname(destination); - - if (parentDir !== '.' && !fs.existsSync(parentDir)) { - throw new Error('Parent directory does not exist'); - } - - correctPath = destination; + throw new Error('Destination directory does not exist'); } - const dataFile = await fsPromises.readFile(source, 'utf-8'); + correctPath = path.join(destination, path.basename(source)); + } else if ( + fs.existsSync(destination) && + fs.statSync(destination).isDirectory() + ) { + correctPath = path.join(destination, path.basename(source)); + } else { + const parentDir = path.dirname(destination); + + if (parentDir !== '.' && !fs.existsSync(parentDir)) { + throw new Error('Parent directory does not exist'); + } - await fsPromises.unlink(source); - await fsPromises.writeFile(correctPath, dataFile, 'utf-8'); - } catch (e) { - throw e; + correctPath = destination; } + + const dataFile = await fsPromises.readFile(source, 'utf-8'); + + await fsPromises.unlink(source); + await fsPromises.writeFile(correctPath, dataFile, 'utf-8'); } // eslint-disable-next-line no-console -main().catch((e) => console.error(e.massage)); +main().catch((e) => console.error(e.message));