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
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.3",
"eslint": "^8.57.0",
"eslint-plugin-jest": "^28.6.0",
"eslint-plugin-node": "^11.1.0",
Expand Down
48 changes: 47 additions & 1 deletion src/app.js
Original file line number Diff line number Diff line change
@@ -1 +1,47 @@
// 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) {
throw new Error('Destination directory does not exist');
}

newFile = path.join(newFile, fileName);
}

const targetFolder = path.dirname(newFile);

if (!fs.existsSync(targetFolder)) {
console.error('Directory does not exist');

return;
Comment on lines +38 to +41

Choose a reason for hiding this comment

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

This check correctly identifies that the destination directory doesn't exist. However, the requirement is to throw an error in this case, not just log a message to the console. Please change this to throw new Error(...) to align with the requirements and the error handling on line 30.

Copy link
Author

Choose a reason for hiding this comment

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

If I change this to :
if (!fs.existsSync(targetFolder)) {
throw new Error('Directory does not exist');
}

the tests are failing and I can't commit it
so I kept it as it is, because the tests are passing only with this console.error option

Comment on lines +38 to +41

Choose a reason for hiding this comment

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

The requirements state that an error must be thrown if the destination directory doesn't exist. This implementation logs an error to the console instead. Please change this to throw new Error('Directory does not exist'); to align with the requirements.

}

fs.renameSync(oldFile, newFile);
}

main();
3 changes: 3 additions & 0 deletions tests/galley/illustrious_dissuade_overconfidently.txt
Original file line number Diff line number Diff line change
@@ -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.