Skip to content

Commit

Permalink
add sources config file to have a separate config file for sources co…
Browse files Browse the repository at this point in the history
…nfiguration
  • Loading branch information
Thierry DEGREMONT committed Aug 2, 2024
1 parent 906958f commit 6af9ad6
Showing 1 changed file with 32 additions and 11 deletions.
43 changes: 32 additions & 11 deletions packages/graphql-mesh/utils/parseYamlConfig.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,54 @@
import { YamlConfig } from '@graphql-mesh/types'
import { DefaultLogger } from '@graphql-mesh/utils'
//import { DefaultLogger } from '@graphql-mesh/utils'
import { load } from 'js-yaml'
import { readFileSync } from 'node:fs'
import { resolve } from 'node:path'
import { Logger } from '../utils/logger'

const logger = new DefaultLogger()
//const logger = new DefaultLogger()

// Load the config.yaml file
export const getConfig = (): YamlConfig.Config => {
Logger.debug('CONFIG', 'getConfig', 'Loading config file')
let config: YamlConfig.Config

let sourcesConfig: YamlConfig.Config
const configFile = process.env['ConfigFile'] || '../../config/config.yaml'
try {
const configPath = resolve('./config.yaml')
const configPath = resolve(configFile)
config = load(readFileSync(configPath, { encoding: 'utf-8' }))
} catch (e) {
Logger.error('CONFIG', 'getConfig', "Failed loading config ", e)
throw new Error('FAiles laoding config file ./config.yaml', e)
Logger.error('CONFIG', 'getConfig', "Failed loading config " + configFile, e)
throw new Error('FAiles laoding config file ' + configFile, e)
}

if (!config) {
Logger.error('CONFIG', 'getConfig', "No config loaded")
throw new Error('No configuration')
Logger.error('CONFIG', 'getConfig', "No config loaded from " + configFile)
throw new Error('No configuration loaded from ' + configFile)
}
if (config.sources) {
Logger.info('CONFIG', 'getConfig', 'Config file loaded successfully with ' + config.sources.length + " sources")

// if sources config is on a separate file define by SourcesConfigFile env var
const sourcesConfigFile = process.env['SourcesConfigFile']
if (sourcesConfigFile) {
const sourcesConfigFile = process.env['SourcesConfigFile'] || '../../config/sourcesConfig.yaml'
try {
const sourcesConfigPath = resolve(sourcesConfigFile)
sourcesConfig = load(readFileSync(sourcesConfigPath, { encoding: 'utf-8' }))
} catch (e) {
Logger.error('CONFIG', 'getConfig', "Failed loading sources Config " + sourcesConfigFile, e)
throw new Error('FAiles laoding sources config file' + sourcesConfigFile, e)
}
if (sourcesConfig.sources) {
Logger.info('CONFIG', 'getConfig', 'sources Config file loaded successfully with ' + sourcesConfig.sources.length + " sources")
} else {
Logger.error('CONFIG', 'getConfig', 'sources Config file loaded successfully but without sources')
throw new Error('FAiles laoding sources config file no sources found' + sourcesConfigFile)
}
config['sources'] = sourcesConfig.sources
} else {
Logger.warn('CONFIG', 'getConfig', 'Config file loaded successfully but without sources')
if (!config.sources) {
Logger.error('CONFIG', 'getConfig', "No source defined in configuration file" + configFile)
throw new Error('No source defioned in configuration file ' + configFile)
}
}
return config
}
Expand Down

0 comments on commit 6af9ad6

Please sign in to comment.