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
54 changes: 53 additions & 1 deletion src/app.js
Original file line number Diff line number Diff line change
@@ -1 +1,53 @@
// write code here
/* eslint-disable no-console */

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

const [, , srcArg, destArg] = process.argv;

if (!srcArg || !destArg) {
console.error('Two arguments is required');
process.exit(0);
}

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

if (!fs.existsSync(src)) {
console.error('Source does not exist');
process.exit(0);
}

if (src === dest) {
process.exit(0);
}

const srcStat = fs.statSync(src);

if (srcStat.isDirectory()) {
console.error('Source must be a file');
process.exit(0);
}

if (destArg.endsWith('/') || destArg.endsWith('\\')) {
if (fs.existsSync(dest) && fs.statSync(dest).isDirectory()) {
fs.renameSync(src, path.join(dest, path.basename(src)));

process.exit(0);
}

console.error('IT / is in the then it should be folder');

Choose a reason for hiding this comment

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

This error message is not very clear. According to the requirements, an error should be thrown if the destination directory does not exist. A more descriptive message, similar to the one on line 48, would be better. For example: Destination directory does not exist.


process.exit(0);
Comment on lines 33 to 41

Choose a reason for hiding this comment

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

It's great that you've added the check for destination paths ending with a slash, as requested in the previous review. However, this logic block doesn't quite work as intended.

Currently, after a successful file move on line 34, the code continues execution and incorrectly prints an error message on line 37. A successful operation should exit cleanly without any errors.

To fix this, you should restructure this if block. The check on line 33 correctly identifies the success case. You can add an else branch to handle the error case (when the directory does not exist). This will ensure that an error is shown only when it's supposed to, and it will also allow you to provide a more descriptive error message.

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

if (!fs.existsSync(destDir)) {
console.error('Destination directory does not exist');
process.exit(0);
}

fs.renameSync(src, dest);
}
1 change: 1 addition & 0 deletions src/test1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fasdf