|
| 1 | +import fs from 'fs'; |
1 | 2 | import _ from 'lodash'; |
2 | | -import { logger } from './lib/logger'; |
3 | 3 |
|
4 | 4 | const Config = (): any => { |
5 | | - let defaultConfig = { |
| 5 | + const defaultConfig = { |
6 | 6 | basePath: '/bws/api', |
7 | 7 | disableLogs: false, |
8 | 8 | port: 3232, |
@@ -474,23 +474,41 @@ const Config = (): any => { |
474 | 474 | // whitelist: [] |
475 | 475 | // }, |
476 | 476 | }; |
| 477 | + |
| 478 | + const configPath = process.env.BWS_CONFIG || 'bws.config.js'; |
477 | 479 |
|
478 | | - // Override default values with bws.config.js' values, if present |
| 480 | + if (!fs.existsSync(configPath)) { |
| 481 | + throw new Error(`No bitcore config exists at ${configPath}`); |
| 482 | + } |
| 483 | + |
| 484 | + const bitcoreConfigStat = fs.statSync(configPath); |
| 485 | + if (bitcoreConfigStat.isDirectory()) { |
| 486 | + throw new Error(`Provided bitcore config path: ${configPath} is a directory`); |
| 487 | + } |
| 488 | + |
| 489 | + let rawConfig; |
479 | 490 | try { |
480 | | - // eslint-disable-next-line @typescript-eslint/no-require-imports |
481 | | - const bwsConfig = require('../../bws.config'); |
482 | | - defaultConfig = _.merge(defaultConfig, bwsConfig); |
483 | | - } catch { |
484 | | - logger.info('bws.config.js not found, using default configuration values'); |
| 491 | + rawConfig = fs.readFileSync(configPath).toString(); |
| 492 | + } catch (error) { |
| 493 | + throw new Error(`Error in loading bitcore config\nFound file at ${configPath}\n${error}`); |
485 | 494 | } |
486 | 495 |
|
| 496 | + let config; |
| 497 | + try { |
| 498 | + config = JSON.parse(rawConfig); |
| 499 | + } catch (error) { |
| 500 | + throw new Error(`Error in parsing bitcore config\nFound and loaded file at ${configPath}\n${error}`); |
| 501 | + } |
| 502 | + |
| 503 | + config = _.merge(defaultConfig, config); |
| 504 | + |
487 | 505 | if (process.env.SENDGRID_API_KEY) { |
488 | 506 | // override the config value in bws.config.js with env var |
489 | | - defaultConfig.emailOpts.sendGridApiKey = process.env.SENDGRID_API_KEY; |
| 507 | + config.emailOpts.sendGridApiKey = process.env.SENDGRID_API_KEY; |
490 | 508 | } |
491 | 509 | if (process.env.MAILERSEND_API_KEY) { |
492 | 510 | // override the config value in bws.config.js with env var |
493 | | - defaultConfig.emailOpts.mailerSendApiKey = process.env.MAILERSEND_API_KEY; |
| 511 | + config.emailOpts.mailerSendApiKey = process.env.MAILERSEND_API_KEY; |
494 | 512 | } |
495 | 513 |
|
496 | 514 | return defaultConfig; |
|
0 commit comments