config.js 2.81 KB
Newer Older
1
const _ = require('lodash')
2
const chalk = require('chalk')
NGPixel's avatar
NGPixel committed
3
const cfgHelper = require('../helpers/config')
4 5 6 7
const fs = require('fs')
const path = require('path')
const yaml = require('js-yaml')

8
/* global WIKI */
9

10 11 12 13 14 15
module.exports = {
  /**
   * Load root config from disk
   */
  init() {
    let confPaths = {
16 17 18
      config: path.join(WIKI.ROOTPATH, 'config.yml'),
      data: path.join(WIKI.SERVERPATH, 'app/data.yml'),
      dataRegex: path.join(WIKI.SERVERPATH, 'app/regex.js')
19 20
    }

21
    if (process.env.dockerdev) {
22
      confPaths.config = path.join(WIKI.ROOTPATH, `dev/docker-${process.env.DEVDB}/config.yml`)
23 24
    }

25 26
    process.stdout.write(chalk.blue(`Loading configuration from ${confPaths.config}... `))

27 28 29 30 31 32 33 34
    let appconfig = {}
    let appdata = {}

    try {
      appconfig = yaml.safeLoad(
        cfgHelper.parseConfigValue(
          fs.readFileSync(confPaths.config, 'utf8')
        )
35
      )
36 37
      appdata = yaml.safeLoad(fs.readFileSync(confPaths.data, 'utf8'))
      appdata.regex = require(confPaths.dataRegex)
38 39 40 41 42 43
      console.info(chalk.green.bold(`OK`))
    } catch (err) {
      console.error(chalk.red.bold(`FAILED`))
      console.error(err.message)

      console.error(chalk.red.bold(`>>> Unable to read configuration file! Did you create the config.yml file?`))
44 45
      process.exit(1)
    }
46

47
    // Merge with defaults
48

49
    appconfig = _.defaultsDeep(appconfig, appdata.defaults.config)
50

Nick's avatar
Nick committed
51
    if (appconfig.port < 1 || process.env.HEROKU) {
52 53
      appconfig.port = process.env.PORT || 80
    }
NGPixel's avatar
NGPixel committed
54

55 56
    const packageInfo = require(path.join(WIKI.ROOTPATH, 'package.json'))

57 58
    WIKI.config = appconfig
    WIKI.data = appdata
59 60
    WIKI.version = packageInfo.version
    WIKI.releaseDate = packageInfo.releaseDate
61 62 63 64 65
  },

  /**
   * Load config from DB
   */
66
  async loadFromDb() {
67
    let conf = await WIKI.models.settings.getConfig()
68 69
    if (conf) {
      WIKI.config = _.defaultsDeep(conf, WIKI.config)
NGPixel's avatar
NGPixel committed
70
    } else {
71 72
      WIKI.logger.warn('DB Configuration is empty or incomplete. Switching to Setup mode...')
      WIKI.config.setup = true
NGPixel's avatar
NGPixel committed
73 74 75 76 77
    }
  },
  /**
   * Save config to DB
   *
78
   * @param {Array} keys Array of keys to save
NGPixel's avatar
NGPixel committed
79 80
   * @returns Promise
   */
81
  async saveToDb(keys) {
NGPixel's avatar
NGPixel committed
82
    try {
83
      for (let key of keys) {
84 85 86 87
        let value = _.get(WIKI.config, key, null)
        if (!_.isPlainObject(value)) {
          value = { v: value }
        }
88
        let affectedRows = await WIKI.models.settings.query().patch({ value }).where('key', key)
89
        if (affectedRows === 0 && value) {
90
          await WIKI.models.settings.query().insert({ key, value })
91
        }
92
      }
NGPixel's avatar
NGPixel committed
93
    } catch (err) {
94
      WIKI.logger.error(`Failed to save configuration to DB: ${err.message}`)
NGPixel's avatar
NGPixel committed
95 96 97 98
      return false
    }

    return true
Nick's avatar
Nick committed
99 100 101 102 103 104
  },
  /**
   * Apply Dev Flags
   */
  async applyFlags() {
    WIKI.models.knex.client.config.debug = WIKI.config.flags.sqllog
105 106
  }
}