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
62 changes: 61 additions & 1 deletion src/app.js
Original file line number Diff line number Diff line change
@@ -1 +1,61 @@
// write code here
/* eslint no-console: ["error", { allow: ["warn", "error"] }] */

const fs = require('fs/promises');
const path = require('node:path');

const sourceFile = process.argv[2];
const destination = process.argv[3];

if (process.argv.length <= 2) {
console.error('Zero argument is provided');
process.exit(0);
Comment on lines +9 to +11

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Using process.exit(0) for an error case signals success to the shell. For invalid argument counts you should use a non-zero exit code (e.g. process.exit(1)) so scripts can detect the failure.

}

if (process.argv.length === 3) {
console.error('Only one argument is provided');
process.exit(0);
Comment on lines +14 to +16

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Same as above: this is an error path but exits with code 0. Update to a non-zero code to align with correct CLI behavior.

}
Comment on lines +9 to +17

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The current argument validation only checks the number of arguments, but the assignment also requires specific behavior based on the form of the destination (e.g. ending with /). You’ll need logic here (or shortly after) to interpret the destination string (trailing slash or not) and enforce directory existence rules accordingly.


const filePath = path.join(destination, path.basename(sourceFile));

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

filePath is computed as if destination is always a directory, but the requirements say that sometimes destination is a file name (rename case). You should compute the final target path conditionally based on whether destination is a directory (existing or indicated by trailing /) or a new file name.


async function move(file, to, fullPath) {
let isDirectory = false;

try {
const stats = await fs.stat(to);

isDirectory = stats.isDirectory();
} catch {
isDirectory = false;
}

if (to.endsWith('/') || to.endsWith('\\')) {
try {
if (isDirectory) {
await fs.rename(file, fullPath);
} else {
console.error('directory is not exist');
process.exit(0);
Comment on lines +34 to +38

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The message directory is not exist is grammatically incorrect and unclear; per the requirements, prefer something like Destination directory does not exist, and use a non-zero exit code instead of process.exit(0) here.

}
} catch (err) {
console.error('Something went wrong');
process.exit(0);
Comment on lines +40 to +42

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

For unexpected errors you should not exit with code 0; use a non-zero exit code or let the error propagate so the process fails properly.

}
} else if (isDirectory) {
try {
await fs.rename(file, fullPath);
} catch {
console.error('directory does not exist');
process.exit(0);
Comment on lines +45 to +49

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 branch reports directory does not exist and exits with code 0. To meet the requirements, keep the message clear (e.g. Destination directory does not exist) and use a failing exit code (non-zero).

}
} else {
try {
await fs.rename(file, to);
} catch {
console.error('Source file is not exist');
Comment on lines +37 to +55

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Several error messages here (directory is not exist, Parent directory is no exist, Source file is not exist) are grammatically incorrect and not very descriptive. Consider clearer messages like Destination directory does not exist or Source file does not exist to make failures easier to understand.

process.exit(0);
Comment on lines +10 to +56

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

All error cases currently process.exit(0), which signals success. For a CLI-like utility, it’s better to exit with a non-zero code (e.g. 1) on argument or filesystem errors so that scripts can detect failures.

Comment on lines +52 to +56

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Source file is not exist is both grammatically incorrect and exits with code 0. Update the message to Source file does not exist (as suggested) and use a non-zero exit code for this error.

}
}
}

move(sourceFile, destination, filePath);
Loading