import yaml from 'js-yaml'; import { CompassYaml } from '../../../types'; import { INVALID_YAML_ERROR } from './validate-config-file/models/error-messages'; import { InvalidConfigFileError } from './validate-config-file/models/errors'; /** * Transforms string configFile into Compass Yaml data structure. * @throws InvalidConfigFileError If unable to parse the structure * @param configFile * @returns CompassYaml, If successfully loaded the contents else null */ export const loadConfigFile = (configFile: string): CompassYaml | null => { let componentYaml: CompassYaml | null = null; try { componentYaml = yaml.load(configFile) as CompassYaml; } catch (e) { console.warn({ message: 'Error parsing yaml file', error: e }); throw new InvalidConfigFileError([INVALID_YAML_ERROR]); } if (!componentYaml) { console.warn({ message: 'Unable to load config file into componentYaml.' }); throw new InvalidConfigFileError([INVALID_YAML_ERROR]); } return componentYaml; };