From 96d8a23e93ae4b40e8dcea6c7ca3803963c16c93 Mon Sep 17 00:00:00 2001 From: Mykola Hadupiak Date: Tue, 25 Apr 2023 15:27:53 +0300 Subject: [PATCH 1/3] move files --- src/app.js | 77 ++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 60 insertions(+), 17 deletions(-) diff --git a/src/app.js b/src/app.js index a27d075..8825b57 100644 --- a/src/app.js +++ b/src/app.js @@ -1,21 +1,64 @@ +/* eslint-disable no-console */ 'use strict'; -/** - * Implement sum function: - * - * Function takes 2 numbers and returns their sum - * - * sum(1, 2) === 3 - * sum(1, 11) === 12 - * - * @param {number} a - * @param {number} b - * - * @return {number} - */ -function sum(a, b) { - // write code here - return a + b; +const fs = require('fs'); +const path = require('path'); + +const [paramFile, paramDestination] = process.argv.slice(2); + +const green = '\x1b[32m'; +const red = '\x1b[31m'; + +const logs = { + arguments: () => { + console.log( + `${red}You need to write the old file name and the new path. \x1b[0m` + + `\nLike ${green}node ./src/moveFile.js file.txt ./someDir/` + ); + }, + directory: () => { + console.log( + `${red}The directory does not exist` + ); + }, + error: () => { + console.log( + `${red}Something went wrong. Try again!` + ); + }, + success: (destinationPath) => { + console.log( + `${green}Success!` + + `\nMoved to: ${destinationPath}` + ); + }, +}; + +function moveFile(file, destination) { + if (!file || !destination) { + logs.arguments(); + + return; + } + + const filePath = path.join(__dirname, file); + const destinationPath = path.join(__dirname, destination); + const fileName = path.basename(filePath); + const isValid = destination.endsWith('/'); + + if (isValid && !fs.existsSync(destinationPath)) { + logs.directory(); + } + + try { + fs.renameSync( + filePath, + isValid ? path.join(destinationPath, fileName) : destinationPath, + ); + logs.success(destinationPath); + } catch (err) { + logs.error(); + } } -module.exports = sum; +moveFile(paramFile, paramDestination); From 50878127adf7008e11e3c9ef94cdcf5e5c91324d Mon Sep 17 00:00:00 2001 From: Mykola Hadupiak Date: Fri, 5 Jan 2024 18:34:28 +0200 Subject: [PATCH 2/3] Revert "move files" This reverts commit 96d8a23e93ae4b40e8dcea6c7ca3803963c16c93. --- src/app.js | 77 ++++++++++++------------------------------------------ 1 file changed, 17 insertions(+), 60 deletions(-) diff --git a/src/app.js b/src/app.js index 8825b57..a27d075 100644 --- a/src/app.js +++ b/src/app.js @@ -1,64 +1,21 @@ -/* eslint-disable no-console */ 'use strict'; -const fs = require('fs'); -const path = require('path'); - -const [paramFile, paramDestination] = process.argv.slice(2); - -const green = '\x1b[32m'; -const red = '\x1b[31m'; - -const logs = { - arguments: () => { - console.log( - `${red}You need to write the old file name and the new path. \x1b[0m` - + `\nLike ${green}node ./src/moveFile.js file.txt ./someDir/` - ); - }, - directory: () => { - console.log( - `${red}The directory does not exist` - ); - }, - error: () => { - console.log( - `${red}Something went wrong. Try again!` - ); - }, - success: (destinationPath) => { - console.log( - `${green}Success!` - + `\nMoved to: ${destinationPath}` - ); - }, -}; - -function moveFile(file, destination) { - if (!file || !destination) { - logs.arguments(); - - return; - } - - const filePath = path.join(__dirname, file); - const destinationPath = path.join(__dirname, destination); - const fileName = path.basename(filePath); - const isValid = destination.endsWith('/'); - - if (isValid && !fs.existsSync(destinationPath)) { - logs.directory(); - } - - try { - fs.renameSync( - filePath, - isValid ? path.join(destinationPath, fileName) : destinationPath, - ); - logs.success(destinationPath); - } catch (err) { - logs.error(); - } +/** + * Implement sum function: + * + * Function takes 2 numbers and returns their sum + * + * sum(1, 2) === 3 + * sum(1, 11) === 12 + * + * @param {number} a + * @param {number} b + * + * @return {number} + */ +function sum(a, b) { + // write code here + return a + b; } -moveFile(paramFile, paramDestination); +module.exports = sum; From c79070782e0ab46c76ab52480bd301e9f4821f5e Mon Sep 17 00:00:00 2001 From: Mykola Hadupiak Date: Fri, 5 Jan 2024 19:00:23 +0200 Subject: [PATCH 3/3] move files --- src/app.js | 41 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/src/app.js b/src/app.js index 0d15e7b..69aa210 100644 --- a/src/app.js +++ b/src/app.js @@ -1 +1,40 @@ -// write code here +/* eslint-disable no-console */ +'use strict'; + +const fs = require('fs'); +const path = require('path'); + +const [sourceFile, destination] = process.argv.slice(2); + +function moveFile(source, destinationName) { + const absoluteSourcePath = path.resolve(source); + let absoluteDestinationPath = path.resolve(destinationName); + + if (!fs.existsSync(absoluteSourcePath)) { + console.error('Source file does not exist.'); + + return; + } + + const isDirectory = fs.existsSync(absoluteDestinationPath) + && fs.statSync(absoluteDestinationPath).isDirectory(); + + if (isDirectory) { + const fileName = path.basename(absoluteSourcePath); + + absoluteDestinationPath = path.join(absoluteDestinationPath, fileName); + } else { + const destinationDir = path.dirname(absoluteDestinationPath); + + if (!fs.existsSync(destinationDir)) { + console.error('Destination directory does not exist.'); + + return; + } + } + + fs.renameSync(absoluteSourcePath, absoluteDestinationPath); + console.log(`File '${source}' moved to '${destinationName}'.`); +} + +moveFile(sourceFile, destination);