From 28f160d350d6ae1d4f20ed57ec34867d9a3d42e5 Mon Sep 17 00:00:00 2001 From: TetiankaSh Date: Wed, 21 Jan 2026 10:49:51 +0200 Subject: [PATCH 1/3] initial commit --- .github/workflows/test.yml-template | 23 ++++++++ package-lock.json | 8 +-- package.json | 2 +- src/app.js | 54 ++++++++++++++++++- .../illustrious_dissuade_overconfidently.txt | 3 ++ 5 files changed, 84 insertions(+), 6 deletions(-) create mode 100644 .github/workflows/test.yml-template create mode 100644 tests/galley/illustrious_dissuade_overconfidently.txt diff --git a/.github/workflows/test.yml-template b/.github/workflows/test.yml-template new file mode 100644 index 0000000..bb13dfc --- /dev/null +++ b/.github/workflows/test.yml-template @@ -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 diff --git a/package-lock.json b/package-lock.json index 2a93237..b1eda88 100644 --- a/package-lock.json +++ b/package-lock.json @@ -12,7 +12,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", @@ -1484,9 +1484,9 @@ } }, "node_modules/@mate-academy/scripts": { - "version": "1.8.6", - "resolved": "https://registry.npmjs.org/@mate-academy/scripts/-/scripts-1.8.6.tgz", - "integrity": "sha512-b4om/whj4G9emyi84ORE3FRZzCRwRIesr8tJHXa8EvJdOaAPDpzcJ8A0sFfMsWH9NUOVmOwkBtOXDu5eZZ00Ig==", + "version": "2.1.3", + "resolved": "https://registry.npmjs.org/@mate-academy/scripts/-/scripts-2.1.3.tgz", + "integrity": "sha512-a07wHTj/1QUK2Aac5zHad+sGw4rIvcNl5lJmJpAD7OxeSbnCdyI6RXUHwXhjF5MaVo9YHrJ0xVahyERS2IIyBQ==", "dev": true, "dependencies": { "@octokit/rest": "^17.11.2", diff --git a/package.json b/package.json index f8c126f..c27ea01 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/app.js b/src/app.js index 0d15e7b..613b809 100644 --- a/src/app.js +++ b/src/app.js @@ -1 +1,53 @@ -// write code here +/* eslint-disable no-console */ +const fs = require('fs'); +const path = require('path'); + +function main() { + const params = process.argv.slice(2); + const oldFile = params[0]; + let newFile = params[1]; + + if (params.length < 2) { + console.error('2 parameters are required'); + + return; + } + + if (!fs.existsSync(oldFile)) { + console.error('Source file does not exist'); + + return; + } + + const fileName = path.basename(oldFile); + + const endsWithSlash = newFile.endsWith('/'); + const targetExists = fs.existsSync(newFile); + const isDirectory = targetExists && fs.lstatSync(newFile).isDirectory(); + + if (endsWithSlash || isDirectory) { + if (!targetExists && endsWithSlash) { + console.error('Destination directory does not exist'); + + return; + } + + newFile = path.join(newFile, fileName); + } + + const targetFolder = path.dirname(newFile); + + if (!fs.existsSync(targetFolder)) { + console.error('Directory does not exist'); + + return; + } + + fs.rename(oldFile, newFile, (err) => { + if (err) { + throw err; + } + }); +} + +main(); diff --git a/tests/galley/illustrious_dissuade_overconfidently.txt b/tests/galley/illustrious_dissuade_overconfidently.txt new file mode 100644 index 0000000..7756723 --- /dev/null +++ b/tests/galley/illustrious_dissuade_overconfidently.txt @@ -0,0 +1,3 @@ +Thesis apto admiratio textor. Bibo vigor blandior tum terebro tricesimus absorbeo aspernatur. Aiunt solio amo sono tyrannus. +Conitor dens tamdiu acceptus vos cinis. Turpis varietas defungo pauci perferendis casso distinctio pauper. Patrocinor talio ducimus blandior deleo degusto pel sunt aliquam caecus. +Subnecto chirographum denego defessus tepidus beatae defessus. Est explicabo titulus valde pel testimonium tollo capto demo. Conculco omnis coepi placeat. \ No newline at end of file From d21ccd4e2fc201129c81b5e8d50c49402299a6d3 Mon Sep 17 00:00:00 2001 From: TetiankaSh Date: Wed, 21 Jan 2026 11:03:54 +0200 Subject: [PATCH 2/3] changes were made --- src/app.js | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/app.js b/src/app.js index 613b809..601a2f4 100644 --- a/src/app.js +++ b/src/app.js @@ -27,9 +27,7 @@ function main() { if (endsWithSlash || isDirectory) { if (!targetExists && endsWithSlash) { - console.error('Destination directory does not exist'); - - return; + throw new Error('Destination directory does not exist'); } newFile = path.join(newFile, fileName); @@ -43,7 +41,7 @@ function main() { return; } - fs.rename(oldFile, newFile, (err) => { + fs.renameSync(oldFile, newFile, (err) => { if (err) { throw err; } From a41476ba910f499670e538f4c4deeeb9c6ebb308 Mon Sep 17 00:00:00 2001 From: TetiankaSh Date: Wed, 21 Jan 2026 11:10:48 +0200 Subject: [PATCH 3/3] fixed renameSync function --- src/app.js | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/app.js b/src/app.js index 601a2f4..29501c4 100644 --- a/src/app.js +++ b/src/app.js @@ -41,11 +41,7 @@ function main() { return; } - fs.renameSync(oldFile, newFile, (err) => { - if (err) { - throw err; - } - }); + fs.renameSync(oldFile, newFile); } main();