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
1 change: 1 addition & 0 deletions dir/text1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
erfer
8 changes: 4 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.2",
"eslint": "^8.57.0",
"eslint-plugin-jest": "^28.6.0",
"eslint-plugin-node": "^11.1.0",
Expand Down
45 changes: 44 additions & 1 deletion src/app.js
Original file line number Diff line number Diff line change
@@ -1 +1,44 @@
// write code here
/* eslint-disable no-console */

const { rename } = require('fs/promises');
const { statSync, existsSync } = require('fs');
const path = require('path');

async function app() {
const args = process.argv.slice(2);
const [source, destination] = args;

if (!source || !destination) {
console.error(`Two arguments was needed`);

return;
}

if (existsSync(source) && !statSync(source).isFile()) {
console.error(`I can move just files! `);

return;
}

const slicedDest = destination.endsWith('/')
? destination.slice(0, -1)
: destination;

try {

Choose a reason for hiding this comment

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

The requirements state that the app must only support moving files. You should add a check here to verify that the source path points to a file and not a directory. Consider using statSync(source).isFile() before attempting to move it.

const isDestDir =
existsSync(slicedDest) && statSync(slicedDest).isDirectory();

const finDest = isDestDir
? path.join(slicedDest, path.basename(source))
: slicedDest;
Comment on lines +23 to +33

Choose a reason for hiding this comment

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

This logic block doesn't correctly handle a key requirement: when the destination path ends with a / (like someDir/), it must be treated as a directory, and an error must be thrown if that directory does not exist.

Currently, if someDir/ is provided as a destination and the directory doesn't exist, your code proceeds to rename the source file to someDir instead of reporting an error.

You'll need to add a specific check before this try block. If destination.endsWith('/'), you should first verify that the directory path (without the trailing slash) exists. If it doesn't, you must log an error and exit, as per the task requirements.

Copy link
Author

Choose a reason for hiding this comment

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

try {
const isDestDir =
existsSync(slicedDest) && statSync(slicedDest).isDirectory();

const finDest = isDestDir
  ? path.join(slicedDest, path.basename(source))
  : slicedDest;

await rename(source, finDest);
console.log(`${source} was moved to ${destination}`);

} catch (e) {
console.error(The file could not be moved. Error: ${e});
} - у нас программа попадет в ветку catch в этом случае если finDest не существует, отдельно это обрабатывать не нужно


await rename(source, finDest);
console.log(`${source} was moved to ${destination}`);
} catch (e) {
console.error(`The file could not be moved. Error: ${e}`);
}
Comment on lines +23 to +39

Choose a reason for hiding this comment

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

This logic still doesn't meet the requirement for handling destination paths that end with a /. According to the task description: "In case destination directory does not exist an error must be thrown."

Currently, if the destination is nonExistentDir/, your code removes the trailing slash, sees that nonExistentDir doesn't exist, and proceeds to rename the source file to nonExistentDir. This is incorrect.

You need to add a specific check before this try block. If destination.endsWith('/') is true, you must verify that the path (without the /) points to an existing directory. If it does not, you should log an error and exit.

}

app();

module.exports = { app };
3 changes: 3 additions & 0 deletions tests/next.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Vel odio sequi caput cernuus temperantia. Vulnus tabella callide acceptus teres xiphias. Arma desolo quam tubineus bos cupiditas pecco decor confugo.
Vomer caute cado audentia perspiciatis conicio vulticulus abundans atavus civis. Patior volo comedo vigilo cohaero cubo dolorem. Ipsa esse adhuc.
Accusator vesper nihil turba subito tredecim quibusdam curiositas. Dapifer textor aufero aliquam adamo casus deserunt charisma depereo decens. Accommodo viscus vigor adduco.