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

add task solution #239

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
37 changes: 36 additions & 1 deletion src/app.js
Original file line number Diff line number Diff line change
@@ -1 +1,36 @@
// write code here
const fs = require('fs');
const path = require('path');

function moveFiles(source, destination) {
if (!source) {
// eslint-disable-next-line no-console
console.error('No source provided');

return;
}

if (!destination) {
// eslint-disable-next-line no-console
console.error('No destination provided');

return;
}

const isDirectory =
destination.endsWith('/') ||
(fs.existsSync(destination) && fs.lstatSync(destination).isDirectory());
let destinationPath = destination;

if (isDirectory) {
destinationPath = path.join(destination, path.basename(source));
}

fs.rename(source, destinationPath, (error) => {
if (error) {
// eslint-disable-next-line no-console
console.error(error);
}
});
}
Comment on lines +28 to +33

Choose a reason for hiding this comment

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

The fs.rename function is used here to move files. Be aware that this function may not work across different file systems. If you need to ensure compatibility across different file systems, consider using fs.copyFile followed by fs.unlink to manually move the file.


Comment on lines +4 to +34

Choose a reason for hiding this comment

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

Consider adding a check to verify if the source file exists before attempting to move it. This can prevent errors when the source file does not exist.

moveFiles(...process.argv.slice(2, 4));
Loading