Skip to content
Open
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
86 changes: 86 additions & 0 deletions src/app.js
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');
}
Comment on lines +65 to +71

Choose a reason for hiding this comment

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

This if block is unreachable. The case where isDestinationDirectory is true is fully handled by the logic starting on line 33, which always exits the function with either a return or a throw.

} 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

Choose a reason for hiding this comment

The 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 console.error instead of throwing an error. Please change this to throw new Error(...) to conform to the requirements.

}
}

fs.renameSync(source, destination);
}

moveFile();

module.exports = { moveFile };