Skip to content

Commit

Permalink
fix solution
Browse files Browse the repository at this point in the history
  • Loading branch information
denys-kononenko committed Feb 23, 2024
1 parent 1d34822 commit 0eb8010
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 33 deletions.
36 changes: 3 additions & 33 deletions src/app.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,7 @@
'use strict';

const fs = require('fs');
const path = require('path');
const { customCopyFile } = require('./modules/customCopyFile');

const customCopyFile = () => {
const inputArgs = process.argv.slice(2);
const [sourceFile, destinationFile] = process.argv.slice(2);

if (inputArgs.length !== 2) {
process.stderr.write('should be 2 params');

return;
}

const [sourceFile, destinationFile] = inputArgs;

if (sourceFile === destinationFile) {
return;
}

const sourcePath = path.join(__dirname, '..', sourceFile);
const destinationPath = path.join(__dirname, '..', destinationFile);

try {
fs.copyFileSync(sourcePath, destinationPath);
} catch (err) {
if (err.code === 'EISDIR' || err.code === 'EPERM') {
process.stderr.write('source or destination is a directory');
}

if (err.code === 'ENOENT') {
process.stderr.write('non-existent source file');
}
}
};

customCopyFile();
customCopyFile(sourceFile, destinationFile);
35 changes: 35 additions & 0 deletions src/modules/customCopyFile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
'use strict';

const fs = require('fs');
const path = require('path');

const customCopyFile = (sourceFile, destinationFile) => {
if (sourceFile === undefined || destinationFile === undefined) {
process.stderr.write('should be 2 params');

return;
}

if (sourceFile === destinationFile) {
return;
}

const sourcePath = path.join(__dirname, '..', '..', sourceFile);
const destinationPath = path.join(__dirname, '..', '..', destinationFile);

try {
fs.copyFileSync(sourcePath, destinationPath);
} catch (err) {
if (err.code === 'EISDIR' || err.code === 'EPERM') {
process.stderr.write('source or destination is a directory');
}

if (err.code === 'ENOENT') {
process.stderr.write('non-existent source file');
}
}
};

module.exports = {
customCopyFile,
};

0 comments on commit 0eb8010

Please sign in to comment.