Skip to content

Commit

Permalink
feat: basic cli gen-diff util
Browse files Browse the repository at this point in the history
  • Loading branch information
sazanik committed Nov 15, 2024
1 parent db7f2a8 commit 4340f85
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 21 deletions.
11 changes: 11 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
install:
npm ci

gen-diff:
node bin/brain-games.js

publish:
npm publish --dry-run

link:
npm link
6 changes: 6 additions & 0 deletions __fixtures__/file1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"host": "hexlet.io",
"timeout": 50,
"proxy": "123.234.53.22",
"follow": false
}
5 changes: 5 additions & 0 deletions __fixtures__/file2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"timeout": 20,
"verbose": true,
"host": "hexlet.io"
}
2 changes: 1 addition & 1 deletion bin/gen-diff.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env node
import app from '../genDiff.js';
import app from '../src/index.js';

app();
19 changes: 0 additions & 19 deletions genDiff.js

This file was deleted.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "@hexlet/code",
"version": "1.0.0",
"description": "[![Actions Status](https://github.com/sazanik/frontend-project-46/actions/workflows/hexlet-check.yml/badge.svg)](https://github.com/sazanik/frontend-project-46/actions)",
"main": "index.js",
"main": "src/index.js",
"type": "module",
"bin": {
"gen-diff": "bin/gen-diff.js"
Expand Down
35 changes: 35 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import path from 'path';
import { program } from 'commander';

import { parseFile } from './utils/index.js';

const app = () => {
program
.name('genDiff')
.description('Compares two configuration files and shows a difference.')
.version('1.0.0')
.helpOption('-h, --help', 'output usage information')
.option('-f, --format [type]', 'output format')
.arguments('<filepath1> <filepath2>')
.action((filepath1, filepath2) => {
const absolutePath1 = path.resolve(process.cwd(), filepath1);
const absolutePath2 = path.resolve(process.cwd(), filepath2);

console.log(222, absolutePath1);
console.log(333, absolutePath2);

const data1 = parseFile(absolutePath1);
const data2 = parseFile(absolutePath2);

console.log('data1: ', data1);
console.log('data2: ', data2);
});

program.parse(process.argv);

// const options = program.opts();
// console.log(options);
// console.log(process.argv);
};

export default app;
1 change: 1 addition & 0 deletions src/utils/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default as parseFile } from './parseFile.js';
14 changes: 14 additions & 0 deletions src/utils/parseFile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import fs from 'fs';
import path from 'path';

export default (filePath) => {
const fileContent = fs.readFileSync(filePath, 'utf-8');
const extension = path.extname(filePath);

switch (extension) {
case '.json':
return JSON.parse(fileContent);
default:
throw new Error(`Unsupported file format: ${extension}`);
}
};

0 comments on commit 4340f85

Please sign in to comment.