sideloader.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. const fs = require('fs-extra')
  2. const path = require('path')
  3. const _ = require('lodash')
  4. module.exports = {
  5. async init () {
  6. if (!WIKI.config.offline) {
  7. return
  8. }
  9. const sideloadExists = await fs.pathExists(path.resolve(WIKI.ROOTPATH, WIKI.config.dataPath, 'sideload'))
  10. if (!sideloadExists) {
  11. return
  12. }
  13. WIKI.logger.info('Sideload directory detected. Looking for packages...')
  14. try {
  15. await this.importLocales()
  16. } catch (err) {
  17. WIKI.logger.warn(err)
  18. }
  19. },
  20. async importLocales() {
  21. const localeExists = await fs.pathExists(path.resolve(WIKI.ROOTPATH, WIKI.config.dataPath, 'sideload/locales.json'))
  22. if (localeExists) {
  23. WIKI.logger.info('Found locales master file. Importing locale packages...')
  24. let importedLocales = 0
  25. const locales = await fs.readJson(path.resolve(WIKI.ROOTPATH, WIKI.config.dataPath, 'sideload/locales.json'))
  26. if (locales && _.has(locales, 'data.localization.locales')) {
  27. for (const locale of locales.data.localization.locales) {
  28. try {
  29. const localeData = await fs.readJson(path.resolve(WIKI.ROOTPATH, WIKI.config.dataPath, `sideload/${locale.code}.json`))
  30. if (localeData) {
  31. WIKI.logger.info(`Importing ${locale.name} locale package...`)
  32. let lcObj = {}
  33. _.forOwn(localeData, (value, key) => {
  34. if (_.includes(key, '::')) { return }
  35. if (_.isEmpty(value)) { value = key }
  36. _.set(lcObj, key.replace(':', '.'), value)
  37. })
  38. const localeDbExists = await WIKI.db.locales.query().select('code').where('code', locale.code).first()
  39. if (localeDbExists) {
  40. await WIKI.db.locales.query().update({
  41. code: locale.code,
  42. strings: lcObj,
  43. isRTL: locale.isRTL,
  44. name: locale.name,
  45. nativeName: locale.nativeName
  46. }).where('code', locale.code)
  47. } else {
  48. await WIKI.db.locales.query().insert({
  49. code: locale.code,
  50. strings: lcObj,
  51. isRTL: locale.isRTL,
  52. name: locale.name,
  53. nativeName: locale.nativeName
  54. })
  55. }
  56. importedLocales++
  57. }
  58. } catch (err) {
  59. // skip
  60. }
  61. }
  62. WIKI.logger.info(`Imported ${importedLocales} locale packages: [COMPLETED]`)
  63. }
  64. }
  65. }
  66. }