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
40 changes: 40 additions & 0 deletions src/app.js
Original file line number Diff line number Diff line change
@@ -1 +1,41 @@
/* eslint-disable no-console */
'use strict';

const [source, dest] = process.argv.slice(2);
const fs = require('fs');
const path = require('path');

if (!source || !dest) {
console.error('Missing source or destination file path.');
process.exit(0);

Choose a reason for hiding this comment

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

It's a standard convention for command-line applications to exit with a non-zero status code when an error occurs. Using process.exit(0) indicates success, which isn't the case when an error is encountered. Consider using process.exit(1) here and in all other error-handling blocks to properly signal that the script failed.

}

const sourcePath = path.resolve(source);
const destPath = path.resolve(dest);

if (sourcePath === destPath) {
process.exit(0);
}

if (!fs.existsSync(sourcePath)) {
console.error(`Source file does not exist: ${sourcePath}`);
process.exit(0);
}

if (fs.statSync(sourcePath).isDirectory()) {
console.error(`Source file is a directory: ${sourcePath}`);
process.exit(0);
}

if (fs.existsSync(destPath) && fs.statSync(destPath).isDirectory()) {
console.error(`Destination file is a directory: ${destPath}`);
process.exit(0);
}

fs.copyFile(sourcePath, destPath, (err) => {
if (err) {
console.error(`Error copying file from ${sourcePath} to ${destPath}:`, err);
process.exit(0);
}
console.log(`File copied from ${sourcePath} to ${destPath} successfully.`);
});