Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move files #71

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 60 additions & 17 deletions src/app.js
Original file line number Diff line number Diff line change
@@ -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);