From c76e2ab632d38e63e421a1472c14b86ab0bf1c38 Mon Sep 17 00:00:00 2001 From: Tetiana Sobolieva Date: Mon, 11 May 2026 17:47:47 +0300 Subject: [PATCH 1/4] Solution --- .github/workflows/test.yml-template | 23 +++++++++++ package-lock.json | 9 +++-- package.json | 2 +- src/app.js | 60 ++++++++++++++++++++++++++++- 4 files changed, 88 insertions(+), 6 deletions(-) create mode 100644 .github/workflows/test.yml-template 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..60ddc72 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,10 +1484,11 @@ } }, "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, + "license": "MIT", "dependencies": { "@octokit/rest": "^17.11.2", "@types/get-port": "^4.2.0", 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..4d66aa0 100644 --- a/src/app.js +++ b/src/app.js @@ -1 +1,59 @@ -// 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 */ + console.error('You need to pass two arguments!'); + + return; + } + + const [srcArg, destArgv] = argv; + + const src = path.resolve(srcArg); + const dest = path.resolve(destArgv); + + if (!fs.existsSync(src) || !fs.statSync(src).isFile()) { + console.error('Please check if the file you want to transfer exists!'); + + return; + } + + const destIsDir = destArgv.endsWith(path.sep); + let finalPath = ''; + + if (destIsDir) { + if (!fs.existsSync(dest) || !fs.statSync(dest).isDirectory()) { + console.error('Destination must be an existing directory!'); + + 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)) { + console.error('Destination directory does not exist!'); + + return; + } + + try { + fs.renameSync(src, finalPath); + } catch (err) { + console.error('error'); + } +} + +moveFiles(); From 4f0dcfa90d936a73cc8c841c8537db7455380613 Mon Sep 17 00:00:00 2001 From: Tetiana Sobolieva Date: Tue, 12 May 2026 08:08:50 +0300 Subject: [PATCH 2/4] Added Throw Error instead of console.error --- src/app.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/app.js b/src/app.js index 4d66aa0..649278f 100644 --- a/src/app.js +++ b/src/app.js @@ -17,6 +17,8 @@ function moveFiles() { 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; @@ -27,9 +29,10 @@ function moveFiles() { if (destIsDir) { if (!fs.existsSync(dest) || !fs.statSync(dest).isDirectory()) { - console.error('Destination must be an existing directory!'); + throw new Error('Destination must be an existing directory!'); + /* console.error('Destination must be an existing directory!'); - return; + return; */ } finalPath = path.join(dest, path.basename(src)); @@ -44,6 +47,7 @@ function moveFiles() { 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; From b3e21694d59c6f052c466a26a6aef1ec528e21d8 Mon Sep 17 00:00:00 2001 From: Tetiana Sobolieva Date: Tue, 12 May 2026 08:16:37 +0300 Subject: [PATCH 3/4] Changed every console.error on throw Error --- src/app.js | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/src/app.js b/src/app.js index 649278f..c534e1a 100644 --- a/src/app.js +++ b/src/app.js @@ -6,9 +6,10 @@ function moveFiles() { if (argv.length !== 2) { /* eslint-disable no-console */ - console.error('You need to pass two arguments!'); + throw new Error('You need to pass two arguments!'); + /* console.error('You need to pass two arguments!'); - return; + return; */ } const [srcArg, destArgv] = argv; @@ -17,11 +18,11 @@ function moveFiles() { 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!'); + 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!'); + /* console.error('Please check if the file you want to transfer exists!'); - return; + return; */ } const destIsDir = destArgv.endsWith(path.sep); @@ -47,16 +48,16 @@ function moveFiles() { const parentDir = path.dirname(finalPath); if (!fs.existsSync(parentDir)) { - // throw new Error('Destination directory does not exist!'); - console.error('Destination directory does not exist!'); + throw new Error('Destination directory does not exist!'); + /* console.error('Destination directory does not exist!'); - return; + return; */ } try { fs.renameSync(src, finalPath); } catch (err) { - console.error('error'); + console.error(`Failed to move file: ${err.message}`); } } From 03a7562d27fb9b6cd569f809a9ede326794f5716 Mon Sep 17 00:00:00 2001 From: Tetiana Sobolieva Date: Tue, 12 May 2026 08:20:43 +0300 Subject: [PATCH 4/4] Changed every console.error on throw Error --- src/app.js | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/app.js b/src/app.js index c534e1a..70db54e 100644 --- a/src/app.js +++ b/src/app.js @@ -6,10 +6,10 @@ function moveFiles() { 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!'); + // throw new Error('You need to pass two arguments!'); + console.error('You need to pass two arguments!'); - return; */ + return; } const [srcArg, destArgv] = argv; @@ -18,11 +18,11 @@ function moveFiles() { 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!'); + // 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!'); + console.error('Please check if the file you want to transfer exists!'); - return; */ + return; } const destIsDir = destArgv.endsWith(path.sep); @@ -30,10 +30,10 @@ function moveFiles() { 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!'); + // throw new Error('Destination must be an existing directory!'); + console.error('Destination must be an existing directory!'); - return; */ + return; } finalPath = path.join(dest, path.basename(src)); @@ -48,10 +48,10 @@ function moveFiles() { const parentDir = path.dirname(finalPath); if (!fs.existsSync(parentDir)) { - throw new Error('Destination directory does not exist!'); - /* console.error('Destination directory does not exist!'); + // throw new Error('Destination directory does not exist!'); + console.error('Destination directory does not exist!'); - return; */ + return; } try {