-
Notifications
You must be signed in to change notification settings - Fork 407
Solution #332
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 #332
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,87 @@ | ||
| // write code here | ||
| /* eslint-disable no-console */ | ||
| const fs = require('fs'); | ||
| const path = require('path'); | ||
|
|
||
| function moveFile() { | ||
| const [, , sourceArg, destinationArg] = process.argv; | ||
|
|
||
| if (!sourceArg || !destinationArg) { | ||
| console.error('Not enough parameters'); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| const source = path.resolve(sourceArg); | ||
| const destination = path.resolve(destinationArg); | ||
| const isDestinationDirectory = destinationArg.endsWith(path.sep); | ||
|
|
||
| if (!fs.existsSync(source)) { | ||
| console.error('Source does not exist'); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| const sourceStat = fs.statSync(source); | ||
|
|
||
| if (!sourceStat.isFile()) { | ||
| console.error('Source is not a file'); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| if (isDestinationDirectory) { | ||
| if (!fs.existsSync(destination)) { | ||
| throw new Error('Destination directory does not exist'); | ||
| } | ||
|
|
||
| const destStat = fs.statSync(destination); | ||
|
|
||
| if (!destStat.isDirectory()) { | ||
| console.error('Destination is not a directory'); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| const finalPath = path.join(destination, path.basename(source)); | ||
|
|
||
| fs.renameSync(source, finalPath); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| if (fs.existsSync(destination)) { | ||
| const destinationStat = fs.statSync(destination); | ||
|
|
||
| if (destinationStat.isDirectory()) { | ||
| const finalPath = path.join(destination, path.basename(source)); | ||
|
|
||
| fs.renameSync(source, finalPath); | ||
|
|
||
| return; | ||
| } | ||
| } | ||
|
|
||
| if (isDestinationDirectory) { | ||
| if ( | ||
| !fs.existsSync(destination) || | ||
| !fs.statSync(destination).isDirectory() | ||
| ) { | ||
| throw new Error('Destination directory does not exist'); | ||
| } | ||
| } else { | ||
| const parentDir = path.resolve(path.dirname(destination)); | ||
|
|
||
| if (!fs.existsSync(parentDir) || !fs.statSync(parentDir).isDirectory()) { | ||
| console.error('Destination directory does not exist'); | ||
|
|
||
| return; | ||
|
Comment on lines
+75
to
+78
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. According to the requirements, an error must be thrown if the destination directory does not exist. This check correctly identifies a non-existent parent directory but uses |
||
| } | ||
| } | ||
|
|
||
| fs.renameSync(source, destination); | ||
| } | ||
|
|
||
| moveFile(); | ||
|
|
||
| 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.
This
ifblock is unreachable. The case whereisDestinationDirectoryistrueis fully handled by the logic starting on line 33, which always exits the function with either areturnor athrow.