From e64fd2c84b5b2d7044dfa49d63262ab7b8b6d274 Mon Sep 17 00:00:00 2001 From: Mykhailo Forkosh Date: Sat, 6 Sep 2025 20:27:46 +0300 Subject: [PATCH 1/4] solution --- src/app.js | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/src/app.js b/src/app.js index 0d15e7b..827444e 100644 --- a/src/app.js +++ b/src/app.js @@ -1 +1,25 @@ -// write code here +/* eslint-disable no-console */ +'use strict'; + +const fsCb = require('fs'); +const path = require('path'); + +const [subject, destination] = process.argv.slice(2); + +let fileName; +let destinationIsDirectory = false; + +try { + fileName = path.basename(subject); + destinationIsDirectory = fsCb.statSync(destination).isDirectory(); +} catch (error) {} + +const correctDestination = destinationIsDirectory + ? path.join(destination, fileName) + : destination; + +try { + fsCb.renameSync(subject, correctDestination); +} catch (err) { + console.error(err); +} From ef65a7b635487f672da01b831ca5ddc6fe0cf1ec Mon Sep 17 00:00:00 2001 From: Mykhailo Forkosh Date: Sat, 6 Sep 2025 20:28:17 +0300 Subject: [PATCH 2/4] fix code --- src/app.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/app.js b/src/app.js index 827444e..d9b1881 100644 --- a/src/app.js +++ b/src/app.js @@ -6,11 +6,10 @@ const path = require('path'); const [subject, destination] = process.argv.slice(2); -let fileName; +const fileName = path.basename(subject); let destinationIsDirectory = false; try { - fileName = path.basename(subject); destinationIsDirectory = fsCb.statSync(destination).isDirectory(); } catch (error) {} From 6f0cf4ace1a31db3248a5c021a39ab5cf08f1fc2 Mon Sep 17 00:00:00 2001 From: Mykhailo Forkosh Date: Sat, 6 Sep 2025 20:31:11 +0300 Subject: [PATCH 3/4] ... --- src/app.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/app.js b/src/app.js index d9b1881..827444e 100644 --- a/src/app.js +++ b/src/app.js @@ -6,10 +6,11 @@ const path = require('path'); const [subject, destination] = process.argv.slice(2); -const fileName = path.basename(subject); +let fileName; let destinationIsDirectory = false; try { + fileName = path.basename(subject); destinationIsDirectory = fsCb.statSync(destination).isDirectory(); } catch (error) {} From 6ac6038a1b21ac50dfd6421cbffe5c7d92d9a9e5 Mon Sep 17 00:00:00 2001 From: Mykhailo Forkosh Date: Sun, 7 Sep 2025 10:34:01 +0300 Subject: [PATCH 4/4] add fix --- src/app.js | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/src/app.js b/src/app.js index 827444e..d032fd6 100644 --- a/src/app.js +++ b/src/app.js @@ -6,11 +6,16 @@ const path = require('path'); const [subject, destination] = process.argv.slice(2); -let fileName; +if (process.argv.slice(2).length !== 2) { + console.error('Usage: node index '); + + process.exit(0); +} + +const fileName = path.basename(subject); let destinationIsDirectory = false; try { - fileName = path.basename(subject); destinationIsDirectory = fsCb.statSync(destination).isDirectory(); } catch (error) {} @@ -18,6 +23,24 @@ const correctDestination = destinationIsDirectory ? path.join(destination, fileName) : destination; +if (!destinationIsDirectory && destination.endsWith(path.sep)) { + console.error('Destination must be correct path'); + + process.exit(0); +} + +try { + fsCb.accessSync(subject, fsCb.constants.F_OK); + + if (fsCb.statSync(subject).isDirectory()) { + console.error('Subject must be a file'); + process.exit(0); + } +} catch (subError) { + console.error(`subject doesn't exist`); + process.exit(0); +} + try { fsCb.renameSync(subject, correctDestination); } catch (err) {