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

solution #238

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
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
52 changes: 51 additions & 1 deletion src/app.js
Original file line number Diff line number Diff line change
@@ -1 +1,51 @@
// write code here
'use strict';

/* eslint-disable no-console */

const fs = require('node:fs');
const path = require('node:path');

const moveFile = (source, destination) => {
if (!fs.existsSync(source)) {
console.error(`Error: Source file does not exist: ${source}`);
return;
}

if (source === destination) {
return;
}
Comment on lines +14 to +15

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's good that you check if the source and destination are the same to avoid unnecessary operations. However, it might be helpful to log a message indicating that the operation was skipped because the source and destination are identical.


let finalDestination = destination;

if (
(fs.existsSync(destination) && fs.lstatSync(destination).isDirectory()) ||
destination.endsWith('/')
) {
finalDestination = path.join(destination, path.basename(source));
}

try {
const fileData = fs.readFileSync(source, 'utf8');

try {
fs.writeFileSync(finalDestination, fileData, 'utf8');
fs.rmSync(source, { force: true });
} catch (e) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using fs.rmSync with { force: true } is potentially dangerous as it will remove the file without any checks. Consider adding a confirmation step or ensuring that the file is indeed the one intended for deletion.

console.error(`Error writing file: ${finalDestination}\n${e.message}`);
}
} catch (e) {
console.error(`Error reading file: ${source}\n${e.message}`);
}
};

const params = process.argv.slice(2);

if (params.length !== 2) {
console.error(
'Error: Exactly 2 parameters are required (source and destination paths).',
);
} else {
Comment on lines +43 to +46

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The error message for incorrect parameter count is clear, but it might be beneficial to also provide an example of the correct usage to guide the user.

moveFile(...params);
}

module.exports = { moveFile };
Loading