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
23 changes: 23 additions & 0 deletions .github/workflows/test.yml-template
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: Test

on:
pull_request:
branches: [ master ]

jobs:
build:

runs-on: ubuntu-latest

strategy:
matrix:
node-version: [20.x]

steps:
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- run: npm install
- run: npm test
9 changes: 5 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"devDependencies": {
"@faker-js/faker": "^8.4.1",
"@mate-academy/eslint-config": "latest",
"@mate-academy/scripts": "^1.8.6",
"@mate-academy/scripts": "^2.1.3",
"eslint": "^8.57.0",
"eslint-plugin-jest": "^28.6.0",
"eslint-plugin-node": "^11.1.0",
Expand Down
65 changes: 64 additions & 1 deletion src/app.js
Original file line number Diff line number Diff line change
@@ -1 +1,64 @@
// write code here
const fs = require('node:fs');
const path = require('node:path');

function moveFiles() {
const argv = process.argv.slice(2);

if (argv.length !== 2) {
/* eslint-disable no-console */
// throw new Error('You need to pass two arguments!');
console.error('You need to pass two arguments!');

return;
Comment on lines +8 to +12

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Replace console.error() + return with throw new Error() - the task specification requires 'an error must be thrown' for invalid arguments.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

the test will crash, I cant do this

}

const [srcArg, destArgv] = argv;

const src = path.resolve(srcArg);
const dest = path.resolve(destArgv);

if (!fs.existsSync(src) || !fs.statSync(src).isFile()) {
// throw new Error('Please check if the file you want to transfer exists!');

console.error('Please check if the file you want to transfer exists!');

return;
Comment on lines +20 to +25

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Replace console.error() + return with throw new Error() - the task requires 'an error must be thrown' when source file doesn't exist. The correct throw statement is commented out on line 20.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

the test will crash, I cant do this

}

const destIsDir = destArgv.endsWith(path.sep);
let finalPath = '';

if (destIsDir) {
if (!fs.existsSync(dest) || !fs.statSync(dest).isDirectory()) {
// throw new Error('Destination must be an existing directory!');
console.error('Destination must be an existing directory!');
Comment on lines +31 to +34

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This should throw an Error instead of using console.error() + return. The task explicitly requires 'an error is thrown' when a destination directory (specified with trailing /) does not exist.


return;
}

finalPath = path.join(dest, path.basename(src));
} else {
if (fs.existsSync(dest) && fs.statSync(dest).isDirectory()) {
finalPath = path.join(dest, path.basename(src));
} else {
finalPath = dest;
}
}

const parentDir = path.dirname(finalPath);

if (!fs.existsSync(parentDir)) {
// throw new Error('Destination directory does not exist!');
console.error('Destination directory does not exist!');

return;
Comment on lines +49 to +54

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Replace console.error() + return with throw new Error() - the task explicitly requires throwing an error when destination directory doesn't exist. The correct throw statement is commented out on line 50.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

the test will crash, I cant do this

}

try {
fs.renameSync(src, finalPath);
} catch (err) {
console.error(`Failed to move file: ${err.message}`);
}
Comment on lines +57 to +61

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

After throwing errors (or catching them), consider adding process.exit(1) to signal failure to parent processes, as the app currently exits with success code 0 even on errors.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

ok

}

moveFiles();
Loading