UNPKG

cl-diff-tool

Version:
39 lines (32 loc) 1.08 kB
import fs from 'fs'; import path from 'path'; import yamlParser from 'js-yaml'; import iniParser from 'ini'; import { standart, plain, json } from './formats'; import buildAst from './buildAst'; const parsers = { json: JSON.parse, yaml: yamlParser.safeLoad, yml: yamlParser.safeLoad, ini: iniParser.parse, }; const formatters = { standart, plain, json, }; const isFilesExtEqual = (firstPath, secondPath) => path.extname(firstPath) === path.extname(secondPath); export default (firstPath, secondPath, format = 'standart') => { if (!isFilesExtEqual(firstPath, secondPath)) { throw new Error('File types should be equal'); } const configType = path.extname(secondPath).split('.').join(''); const beforeConfig = fs.readFileSync(firstPath, 'utf-8'); const afterConfig = fs.readFileSync(secondPath, 'utf-8'); const parseConfig = parsers[configType]; const beforeObject = parseConfig(beforeConfig); const afterObject = parseConfig(afterConfig); const ast = buildAst(beforeObject, afterObject, format); return formatters[format](ast); };