From f8808af51b719390c04a0fc42cebef8acab46c8e Mon Sep 17 00:00:00 2001 From: Nikita Doroshenko Date: Tue, 18 Nov 2025 19:18:31 +0200 Subject: [PATCH 1/3] task solution --- .github/workflows/test.yml-template | 23 ++++++++++++ package-lock.json | 9 +++-- package.json | 2 +- src/app.js | 57 +++++++++++++++++++++++++++++ 4 files changed, 86 insertions(+), 5 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..fdc58cd 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.2", "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.2", + "resolved": "https://registry.npmjs.org/@mate-academy/scripts/-/scripts-2.1.2.tgz", + "integrity": "sha512-gUXFdqqOfYzF9R3RSx2pCa5GLdOkxB9bFbF+dpUpzucdgGAANqOGdqpmNnMj+e3xA9YHraUWq3xo9cwe5vD9pQ==", "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..f3cbb8b 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.2", "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..bf146e9 100644 --- a/src/app.js +++ b/src/app.js @@ -1 +1,58 @@ // write code here +const fs = require('fs'); +const path = require('path'); +const argv = process.argv.slice(2); +const [file, directory] = argv; + +if (argv.length !== 2) { + /* eslint-disable-next-line no-console */ + console.error('Provide source and destination'); + process.exit(0); +} + +const rawDest = directory; + +const looksLikeDir = + rawDest.endsWith(path.sep) || + (fs.existsSync(rawDest) && fs.statSync(rawDest).isDirectory()); + +try { + const stats = fs.statSync(file); + + if (!stats.isFile()) { + /* eslint-disable-next-line no-console */ + console.error('Source is not a file'); + + process.exit(0); + } +} catch (error) { + /* eslint-disable-next-line no-console */ + console.error(error); + + process.exit(0); +} + +let targetPath; + +if (rawDest.endsWith(path.sep) && !fs.existsSync(rawDest)) { + /* eslint-disable-next-line no-console */ + console.error('Directory does not exist'); + process.exit(0); +} + +if (looksLikeDir) { + targetPath = path.join(rawDest, path.basename(file)); +} else { + targetPath = rawDest; +} + +if (path.resolve(file) !== path.resolve(targetPath)) { + try { + fs.renameSync(file, targetPath); + } catch (error) { + /* eslint-disable-next-line no-console */ + console.error(error); + + process.exit(0); + } +} From 1bb4794dcaf6b48de7a74b95067132f0b7adc113 Mon Sep 17 00:00:00 2001 From: Nikita Doroshenko Date: Tue, 18 Nov 2025 19:33:02 +0200 Subject: [PATCH 2/3] solution --- src/app.js | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/src/app.js b/src/app.js index bf146e9..d3d2226 100644 --- a/src/app.js +++ b/src/app.js @@ -12,9 +12,24 @@ if (argv.length !== 2) { const rawDest = directory; -const looksLikeDir = - rawDest.endsWith(path.sep) || - (fs.existsSync(rawDest) && fs.statSync(rawDest).isDirectory()); +const endsWithSep = rawDest.endsWith(path.sep) || rawDest.endsWith('/'); +let isDir; + +try { + const destExists = fs.existsSync(rawDest); + + if (destExists) { + const destStat = fs.statSync(rawDest); + isDir = destStat.isDirectory(); + } else { + isDir = false; + } +} catch (error) { + console.error(error); + process.exit(1) +} + +const looksLikeDir = endsWithSep || isDir; try { const stats = fs.statSync(file); From 0ef68f85efdc932654b51fac295a26cd9459dd43 Mon Sep 17 00:00:00 2001 From: Nikita Doroshenko Date: Tue, 18 Nov 2025 19:34:31 +0200 Subject: [PATCH 3/3] new solution --- src/app.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/app.js b/src/app.js index d3d2226..e3ba1e8 100644 --- a/src/app.js +++ b/src/app.js @@ -20,13 +20,15 @@ try { if (destExists) { const destStat = fs.statSync(rawDest); + isDir = destStat.isDirectory(); } else { isDir = false; } } catch (error) { + /* eslint-disable-next-line no-console */ console.error(error); - process.exit(1) + process.exit(1); } const looksLikeDir = endsWithSep || isDir;