-
Notifications
You must be signed in to change notification settings - Fork 407
add task solution #340
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?
add task solution #340
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,82 @@ | ||
| // write code here | ||
|
|
||
| const fs = require('fs/promises'); | ||
| const path = require('path'); | ||
|
|
||
| async function validateSource(source) { | ||
| const sourceStat = await fs.stat(source); | ||
|
|
||
| if (!sourceStat.isFile()) { | ||
| throw new Error('Source is not a file'); | ||
| } | ||
| } | ||
|
|
||
| async function setDestinationPath(destination, source) { | ||
| let destinationPath; | ||
|
|
||
| if (destination.endsWith('/')) { | ||
| try { | ||
| const destinationStat = await fs.stat(destination); | ||
|
|
||
| if (!destinationStat.isDirectory()) { | ||
| throw new Error('Destination is not a directory'); | ||
| } | ||
|
|
||
| destinationPath = path.join(destination, path.basename(source)); | ||
| } catch (error) { | ||
| throw new Error('Destination path is invalid'); | ||
| } | ||
| } else { | ||
| try { | ||
| const destinationStat = await fs.stat(destination); | ||
|
|
||
| if (destinationStat.isDirectory()) { | ||
| destinationPath = path.join(destination, path.basename(source)); | ||
| } else { | ||
| destinationPath = destination; | ||
| } | ||
| } catch (error) { | ||
| destinationPath = destination; | ||
| } | ||
| } | ||
|
|
||
| return destinationPath; | ||
| } | ||
|
|
||
| async function main() { | ||
| const [source, destination] = process.argv.slice(2); | ||
|
|
||
| if (!source || !destination) { | ||
| // eslint-disable-next-line no-console | ||
| console.error('Source and destination paths are required'); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| const absoluteSource = path.resolve(source); | ||
| const absoluteDestination = path.resolve(destination); | ||
|
|
||
| if (absoluteSource === absoluteDestination) { | ||
| return; | ||
| } | ||
|
|
||
| try { | ||
| await validateSource(absoluteSource); | ||
| } catch (error) { | ||
| // eslint-disable-next-line no-console | ||
| console.error('Error validating source file: ' + error.message); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| const destinationPath = await setDestinationPath(destination, absoluteSource); | ||
|
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 |
||
|
|
||
| try { | ||
| await fs.rename(absoluteSource, destinationPath); | ||
| } catch (error) { | ||
| // eslint-disable-next-line no-console | ||
| console.error('Error moving file: ' + error.message); | ||
| } | ||
| } | ||
|
|
||
| main(); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| acdscs | ||
| sdcsdv | ||
| svsfd | ||
| dvs | ||
| ewvsdcdvsd dskvds dskvncdsjcn jsdnc | ||
| sdkncds djcnjdsc sdjcsd c ibbzl`cnoabvhb`hcc idc | ||
| `vsbv s[dvs] |
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
try...catchblock has a logic issue. Thethrowon line 22 is immediately caught on line 26. This causes a specific error ('Destination is not a directory') to be replaced by a more generic one ('Destination path is invalid'). Consider restructuring this to handle path existence and type checks separately to provide more descriptive error messages.