import * as fs from 'fs' ; import { posix as path } from 'path' ; import { getFileType } from './get-file-type' ; param filePath returns export function readFile ( filePath ) { return new Promise < string | any > ( ( resolve , reject ) => { fs . readFile ( filePath , 'utf-8' , ( readFileError , fileContent ) => { if ( readFileError ) { reject ( new Error ( `An error occured while reading the file "${ filePath }". [Code "${ readFileError . code }", Number "${ readFileError . errno }"]` ) ) ; return ; } let parsedFileContent = fileContent ; if ( getFileType ( filePath ) === 'json' ) { try { parsedFileContent = JSON . parse ( fileContent ) ; } catch ( jsonParseError ) { reject ( new Error ( `An error occured while parsing the file "${ filePath }" as JSON. [${ ( < Error > jsonParseError ) . message }]` ) ) ; return ; } } resolve ( parsedFileContent ) ; } ) ; } ) ; }