-
Notifications
You must be signed in to change notification settings - Fork 311
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
base: master
Are you sure you want to change the base?
solution #238
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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; | ||
} | ||
|
||
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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using |
||
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 }; |
There was a problem hiding this comment.
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.