From c78f639b36c977204bb2e9319682c3fc2ba35726 Mon Sep 17 00:00:00 2001 From: Tetiana Date: Sat, 31 Aug 2024 00:50:56 +0300 Subject: [PATCH 1/2] add task solution --- src/1-copy.txt | 1 + src/1.txt | 1 + src/32.txt | 1 + src/app.js | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 49 insertions(+) create mode 100644 src/1-copy.txt create mode 100644 src/1.txt create mode 100644 src/32.txt diff --git a/src/1-copy.txt b/src/1-copy.txt new file mode 100644 index 0000000..a32a434 --- /dev/null +++ b/src/1-copy.txt @@ -0,0 +1 @@ +1234567890 diff --git a/src/1.txt b/src/1.txt new file mode 100644 index 0000000..a32a434 --- /dev/null +++ b/src/1.txt @@ -0,0 +1 @@ +1234567890 diff --git a/src/32.txt b/src/32.txt new file mode 100644 index 0000000..a32a434 --- /dev/null +++ b/src/32.txt @@ -0,0 +1 @@ +1234567890 diff --git a/src/app.js b/src/app.js index ad9a93a..5954e07 100644 --- a/src/app.js +++ b/src/app.js @@ -1 +1,47 @@ +/* eslint-disable no-console */ 'use strict'; + +const fs = require('fs'); + +async function readArgs() { + return new Promise((resolve, reject) => { + const args = process.argv; + + if (args.length < 4) { + reject(new Error('Please provide both current path and target path')); + } + + const [currentPath, targetPath] = args.slice(2); + + if (currentPath === targetPath) { + reject(new Error('Please provide unique name of a target path')); + } + + resolve({ currentPath, targetPath }); + }); +} + +function copyFile(targetPath, data) { + try { + fs.writeFileSync(targetPath, data); + } catch (error) { + console.error(error); + } +} + +async function makeACopy() { + readArgs() + .then(({ currentPath, targetPath }) => { + fs.readFile(currentPath, 'utf8', (err, data) => { + if (err) { + console.error(err); + } + copyFile(targetPath, data); + }); + }) + .catch((error1) => { + console.error(error1); + }); +} + +makeACopy(); From 17ed8811e37fa6496238acf732ec5b66f26bd506 Mon Sep 17 00:00:00 2001 From: Tetiana Date: Sat, 31 Aug 2024 00:51:41 +0300 Subject: [PATCH 2/2] add task solution --- src/1-copy.txt | 1 - src/1.txt | 1 - src/32.txt | 1 - 3 files changed, 3 deletions(-) delete mode 100644 src/1-copy.txt delete mode 100644 src/1.txt delete mode 100644 src/32.txt diff --git a/src/1-copy.txt b/src/1-copy.txt deleted file mode 100644 index a32a434..0000000 --- a/src/1-copy.txt +++ /dev/null @@ -1 +0,0 @@ -1234567890 diff --git a/src/1.txt b/src/1.txt deleted file mode 100644 index a32a434..0000000 --- a/src/1.txt +++ /dev/null @@ -1 +0,0 @@ -1234567890 diff --git a/src/32.txt b/src/32.txt deleted file mode 100644 index a32a434..0000000 --- a/src/32.txt +++ /dev/null @@ -1 +0,0 @@ -1234567890