Skip to content
Open
Show file tree
Hide file tree
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
81 changes: 81 additions & 0 deletions src/app.js
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');
}
Comment on lines +18 to +28

Choose a reason for hiding this comment

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

This try...catch block has a logic issue. The throw on 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.

} 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);

Choose a reason for hiding this comment

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

The setDestinationPath function can throw an error (for example, when a destination directory with a trailing slash doesn't exist). This call is not wrapped in a try...catch block, which will lead to an unhandled promise rejection and cause the program to crash. You should handle potential errors from this function call, just like you do for validateSource and fs.rename.


try {
await fs.rename(absoluteSource, destinationPath);
} catch (error) {
// eslint-disable-next-line no-console
console.error('Error moving file: ' + error.message);
}
}

main();
7 changes: 7 additions & 0 deletions src/b/test.txt
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]