Skip to content

Commit 3b102c3

Browse files
committed
lesson 4
1 parent 6cde29c commit 3b102c3

File tree

11 files changed

+167
-1
lines changed

11 files changed

+167
-1
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/node_modules/

Makefile

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
install:
2+
npm ci
3+
4+
publish:
5+
npm publish --dry-run
6+
7+
fix:
8+
npx eslint --fix .
9+
10+
link:
11+
sudo npm link
12+
13+
gendiff.js:
14+
node bin/gendiff.js -h

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
### Hexlet tests and linter status:
2-
[![Actions Status](https://github.com/WisdomQuest/frontend-project-46/actions/workflows/hexlet-check.yml/badge.svg)](https://github.com/WisdomQuest/frontend-project-46/actions)
2+
[![Actions Status](https://github.com/WisdomQuest/frontend-project-46/actions/workflows/hexlet-check.yml/badge.svg)](https://github.com/WisdomQuest/frontend-project-46/actions)
3+
4+
Ascinema(lesson4)
5+
[![asciicast](https://asciinema.org/a/wHepNLn0QNSfYXf9OyHoPT4HJ.svg)](https://asciinema.org/a/wHepNLn0QNSfYXf9OyHoPT4HJ)

bin/gendiff.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#!/usr/bin/env node
2+
3+
import { program } from 'commander';
4+
import parseFile from '../src/fileParser.js';
5+
import diffFiles from '../src/diffFiles.js';
6+
7+
program
8+
.name('gendiff')
9+
.description('Compares two configuration files and shows a difference.')
10+
.version('0.0.1')
11+
.usage('[options] <filepath1> <filepath2>')
12+
// .option('-V, --version', 'output the version number')
13+
.option('-f, --format [type]', 'output format')
14+
.option('-h, --help', 'output usage information')
15+
.argument('<filepath1>', 'path to file 1')
16+
.argument('<filepath2>', 'path to file 2')
17+
.action((filePath1, filePath2) => {
18+
const file1 = parseFile(filePath1);
19+
const file2 = parseFile(filePath2);
20+
21+
console.log(diffFiles(file1, file2));
22+
});
23+
24+
program.parse(process.argv);
25+
26+
if (program.opts().help) {
27+
program.outputHelp();
28+
}

demo.cast

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{"version": 2, "width": 225, "height": 17, "timestamp": 1738224597, "env": {"SHELL": "/bin/bash", "TERM": "xterm-256color"}}
2+
[0.144981, "o", "\u001b[?2004h💻 \u001b[1;34mfrontend-project-46\u001b[m\u001b[32m (main)\u001b[00m $ "]
3+
[6.645801, "o", "gendiff files/file1.json files/file2.json"]
4+
[6.910432, "o", "\r\u001b[C\u001b[C\u001b[C\u001b[C\u001b[C\u001b[C\u001b[C\u001b[C\u001b[C\u001b[C\u001b[C\u001b[C\u001b[C\u001b[C\u001b[C\u001b[C\u001b[C\u001b[C\u001b[C\u001b[C\u001b[C\u001b[C\u001b[C\u001b[C\u001b[C\u001b[C\u001b[C\u001b[C\u001b[C\u001b[C\u001b[C\u001b[Cclear\u001b[K"]
5+
[7.359291, "o", "\b\b\b\b\bgendiff files/file1.json files/file2.json"]
6+
[8.516322, "o", "\r\n\u001b[?2004l\r"]
7+
[9.129015, "o", "{\r\n - follow: false\r\n host: hexlet.io\r\n - proxy: 123.234.53.22\r\n - timeout: 50\r\n + timeout: 20\r\n + verbose: true\r\n}\r\n"]
8+
[9.209824, "o", "\u001b[?2004h💻 \u001b[1;34mfrontend-project-46\u001b[m\u001b[32m (main)\u001b[00m $ "]
9+
[10.880547, "o", "\u001b[?2004l\r\r\nexit\r\n"]

files/file1.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"host": "hexlet.io",
3+
"timeout": 50,
4+
"proxy": "123.234.53.22",
5+
"follow": false
6+
}

files/file2.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"timeout": 20,
3+
"verbose": true,
4+
"host": "hexlet.io"
5+
}

package-lock.json

Lines changed: 25 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"name": "@hexlet/code",
3+
"version": "1.0.0",
4+
"description": "[![Actions Status](https://github.com/WisdomQuest/frontend-project-46/actions/workflows/hexlet-check.yml/badge.svg)](https://github.com/WisdomQuest/frontend-project-46/actions)",
5+
"main": "bin/gendiff.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"author": "",
10+
"license": "ISC",
11+
"type": "module",
12+
"bin": {
13+
"gendiff": "bin/gendiff.js"
14+
},
15+
"dependencies": {
16+
"commander": "^13.1.0"
17+
}
18+
}

src/diffFiles.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import _ from 'lodash';
2+
3+
function compareFiles(file1, file2) {
4+
const key1 = Object.keys(file1);
5+
const key2 = Object.keys(file2);
6+
const unionKeys = _.union(key1, key2);
7+
const temp = _.sortBy(unionKeys).map((key) => {
8+
if (!file2[key]) {
9+
return ` - ${key}: ${file1[key]}`;
10+
}
11+
if (!file1[key]) {
12+
return ` + ${key}: ${file2[key]}`;
13+
}
14+
if (file2[key] === file1[key]) {
15+
return ` ${key}: ${file1[key]}`;
16+
}
17+
return ` - ${key}: ${file1[key]}\n + ${key}: ${file2[key]}`;
18+
});
19+
return `{\n${temp.join('\n')}\n}`;
20+
}
21+
22+
export default compareFiles;
23+
24+
// gendiff filepath1.json filepath2.json
25+
26+
// {
27+
// - follow: false
28+
// host: hexlet.io
29+
// - proxy: 123.234.53.22
30+
// - timeout: 50
31+
// + timeout: 20
32+
// + verbose: true
33+
// }
34+
35+
// {
36+
// "host": "hexlet.io",
37+
// "timeout": 50,
38+
// "proxy": "123.234.53.22",
39+
// "follow": false
40+
// }
41+
42+
// {
43+
// "timeout": 20,
44+
// "verbose": true,
45+
// "host": "hexlet.io"
46+
// }

0 commit comments

Comments
 (0)