import { existsSync , readFileSync } from "fs" ; import { parse } from "comment-json" ; import path from "path" ; import { ConfigContainer , ConfigFileObject , FileExtension , LinterConfigs } from "./types" ; /**
 * Maps the FileObject name by joining its name with the dirname
 */ export function mapPath ( dirPath ) { return ( filePath ) => ( { ... filePath , name : path . join ( dirPath , filePath . name ) } ) ; } /**
 * Check if the filePath exists within the directory
 */ export function checkFilePath ( filePath ) { return existsSync ( filePath . name ) ; } /**
 * Check if the configjuration exists within file object
 */ export function checkIfConfigurationExists < T extends LinterConfigs > ( container ) { return Boolean ( container . config ) ; } function parseJSONFile < T extends LinterConfigs > ( configFile ) { const configObject = parse ( readFileSync ( configFile . name , "utf-8" ) , undefined , true ) ; return { config : ( configFile . attribute !== undefined ? configObject [ configFile . attribute ] : configObject ) as T , file : configFile , } ; } function parseJSFile < T extends LinterConfigs > ( configFile ) { const configObject = require ( configFile . name ) ; return { config : ( configFile . attribute !== undefined ? configObject [ configFile . attribute ] : configObject ) as T , file : configFile , } ; } /**
 * Parse files according to their extensions and return their configs
 */ export function parseLinterConfigFiles < T extends LinterConfigs > ( configFiles , dirPath = "" ) { return configFiles . map ( mapPath ( dirPath ) ) . filter ( checkFilePath ) . map < ConfigContainer < T > > ( ( configFile ) => { switch ( configFile . extension ) { case FileExtension . JSON : case FileExtension . NONE : return parseJSONFile ( configFile ) ; case FileExtension . YAML : throw "NO PARSER FOR YAML FILES DEFINED" ; case FileExtension . JS : return parseJSFile ( configFile ) ; } } ) . filter ( checkIfConfigurationExists ) ; }