From 83cdd563e381356808b1c1097033b053a01abeb8 Mon Sep 17 00:00:00 2001 From: BodyaKutsyk Date: Sat, 21 Sep 2024 17:36:23 +0300 Subject: [PATCH] solution --- src/1.txt | 1 + src/app.js | 35 ++++++++++++++++++++++++++++++++++- 2 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 src/1.txt diff --git a/src/1.txt b/src/1.txt new file mode 100644 index 0000000..2d53e30 --- /dev/null +++ b/src/1.txt @@ -0,0 +1 @@ +vplmioevnjirjkiv kv kvn jvnire \ No newline at end of file diff --git a/src/app.js b/src/app.js index 0d15e7b..c792eda 100644 --- a/src/app.js +++ b/src/app.js @@ -1 +1,34 @@ -// write code here +/* eslint-disable no-console */ + +const fs = require('fs'); +const path = require('path'); +const [inputPath, outputPath] = process.argv.slice(2); + +async function moveFile(from, to) { + let fromData; + let isItFolder = false; + + try { + fromData = await fs.promises.readFile(from, 'utf-8'); + + if (fs.existsSync(to)) { + isItFolder = (await fs.promises.stat(to)).isDirectory(); + } + + if (isItFolder) { + const outputDirectory = path.join(to, path.basename(from)); + + await fs.promises.rm(from); + + await fs.promises.writeFile(outputDirectory, fromData); + + return; + } + + await fs.promises.rename(from, to); + } catch (err) { + console.error(err); + } +} + +moveFile(inputPath, outputPath);