sideloader.js 2.44 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
const fs = require('fs-extra')
const path = require('path')
const _ = require('lodash')

/* global WIKI */

module.exports = {
  async init () {
    if (!WIKI.config.offline) {
      return
    }

    const sideloadExists = await fs.pathExists(path.join(WIKI.ROOTPATH, 'data/sideload'))

    if (!sideloadExists) {
      return
    }

    WIKI.logger.info('Sideload directory detected. Looking for packages...')

    try {
      await this.importLocales()
    } catch (err) {
      WIKI.logger.warn(err)
    }
  },
  async importLocales() {
    const localeExists = await fs.pathExists(path.join(WIKI.ROOTPATH, 'data/sideload/locales.json'))
    if (localeExists) {
      WIKI.logger.info('Found locales master file. Importing locale packages...')
      let importedLocales = 0

      const locales = await fs.readJson(path.join(WIKI.ROOTPATH, 'data/sideload/locales.json'))
      if (locales && _.has(locales, 'data.localization.locales')) {
        for (const locale of locales.data.localization.locales) {
          try {
            const localeData = await fs.readJson(path.join(WIKI.ROOTPATH, `data/sideload/${locale.code}.json`))
            if (localeData) {
              WIKI.logger.info(`Importing ${locale.name} locale package...`)

              let lcObj = {}
              _.forOwn(localeData, (value, key) => {
                if (_.includes(key, '::')) { return }
                if (_.isEmpty(value)) { value = key }
                _.set(lcObj, key.replace(':', '.'), value)
              })

Nick's avatar
Nick committed
48 49
              const localeDbExists = await WIKI.models.locales.query().select('code').where('code', locale.code).first()
              if (localeDbExists) {
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
                await WIKI.models.locales.query().update({
                  code: locale.code,
                  strings: lcObj,
                  isRTL: locale.isRTL,
                  name: locale.name,
                  nativeName: locale.nativeName
                }).where('code', locale.code)
              } else {
                await WIKI.models.locales.query().insert({
                  code: locale.code,
                  strings: lcObj,
                  isRTL: locale.isRTL,
                  name: locale.name,
                  nativeName: locale.nativeName
                })
              }
              importedLocales++
            }
          } catch (err) {
            // skip
          }
        }
        WIKI.logger.info(`Imported ${importedLocales} locale packages: [COMPLETED]`)
      }
    }
  }
}