-
Notifications
You must be signed in to change notification settings - Fork 438
Solution #359
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
base: master
Are you sure you want to change the base?
Solution #359
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,41 @@ | ||
| // write code here | ||
| const fs = require('fs'); | ||
| const path = require('path'); | ||
|
|
||
| function moveFiles() { | ||
| const [source, destination] = process.argv.slice(2); | ||
|
|
||
| try { | ||
| if (!source || !destination) { | ||
| throw new Error('Invzlid arguments'); | ||
| } | ||
|
|
||
| if (!fs.existsSync(source) || !fs.statSync(source).isFile()) { | ||
| throw new Error('Source is not a file'); | ||
| } | ||
|
|
||
| let finalDest = destination; | ||
|
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 requirements state that the app must support only moving files. The current implementation would also move a directory if it were provided as the source. It's a good practice to add a check here to verify that the |
||
|
|
||
| if (destination.endsWith('/')) { | ||
| if ( | ||
| !fs.existsSync(destination) || | ||
| !fs.statSync(destination).isDirectory() | ||
| ) { | ||
| throw new Error('Destination directory does not exist'); | ||
| } | ||
|
|
||
| finalDest = path.join(destination, path.basename(source)); | ||
| } else if ( | ||
| fs.existsSync(destination) && | ||
| fs.statSync(destination).isDirectory() | ||
| ) { | ||
| finalDest = path.join(destination, path.basename(source)); | ||
| } | ||
|
Comment on lines
+18
to
+32
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. Great job handling both cases for the destination directory (with and without a trailing slash). To make the code even cleaner, you could refactor this |
||
|
|
||
| fs.renameSync(source, finalDest); | ||
| } catch (error) { | ||
| // eslint-disable-next-line no-console | ||
| console.error(error.message); | ||
| } | ||
| } | ||
|
|
||
| moveFiles(); | ||
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.
There's a small typo here. It should be
Invalid arguments.