Chilim/project-lvl2-s133

View on GitHub
src/index.js

Summary

Maintainability
A
0 mins
Test Coverage
import fs from 'fs';
import ini from 'ini';
import path from 'path';
import yaml from 'js-yaml';
import buildAst from './ast';
import getRenderFormat from './renderers';


const formatActions = {
  '.json': JSON.parse,
  '.yaml': yaml.safeLoad,
  '.ini': ini.parse,
};

const getExtenAction = exten => formatActions[exten];

const genDiff = (firstFilePath, secondFilePath, format = 'json') => {
  const firstObj = fs.readFileSync(firstFilePath, 'utf8');
  const secondObj = fs.readFileSync(secondFilePath, 'utf8');
  const exten = path.extname(firstFilePath);
  const extenAction = getExtenAction(exten);
  const firstFileContent = extenAction(firstObj);
  const secondFileContent = extenAction(secondObj);
  const ast = buildAst(firstFileContent, secondFileContent);
  const renderFormat = getRenderFormat(format);
  return renderFormat(ast);
};

export default genDiff;