You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

125 lines
4.7 KiB

  1. /* global config */
  2. const Promise = require('bluebird')
  3. const colors = require('colors/safe')
  4. const fs = Promise.promisifyAll(require('fs-extra'))
  5. const path = require('path')
  6. const uglify = require('uglify-es')
  7. const request = require('request-promise')
  8. const yaml = require('js-yaml')
  9. const _ = require('lodash')
  10. module.exports = {
  11. /**
  12. * Fetch Localization Resources from Lokalise
  13. */
  14. async fetchLocalizationResources () {
  15. console.info(colors.white(' └── ') + colors.green('Fetching latest localization resources...'))
  16. let langs = await request({
  17. method: 'POST',
  18. uri: `${config.lokalise.api}/string/list`,
  19. form: {
  20. api_token: config.lokalise.key,
  21. id: config.lokalise.project
  22. },
  23. json: true
  24. })
  25. if (langs && langs.strings && _.isPlainObject(langs.strings)) {
  26. _.forIn(langs.strings, (langData, langKey) => {
  27. let lang = {}
  28. let langTotal = 0
  29. langData.forEach(item => {
  30. if (item.is_archived === '1' || _.includes(item.key, '::')) { return }
  31. let keyParts = item.key.split(':')
  32. let keyNamespace = (keyParts.length > 1) ? _.head(keyParts) : 'common'
  33. let keyString = _.last(keyParts)
  34. _.set(lang, `${keyNamespace}.${keyString}`, item.translation)
  35. langTotal++
  36. })
  37. _.forOwn(lang, (langObject, langNamespace) => {
  38. let langYaml = yaml.safeDump(langObject, {
  39. indent: 2,
  40. sortKeys: true,
  41. lineWidth: 2048
  42. })
  43. fs.outputFileSync(path.join(process.cwd(), `server/locales/${langKey}/${langNamespace}.yml`), langYaml, 'utf8')
  44. })
  45. console.info(colors.white(` ${langKey} - ${langTotal} keys written`))
  46. })
  47. } else {
  48. throw new Error('Failed to fetch language list from Lokalise API.')
  49. }
  50. return true
  51. },
  52. /**
  53. * SimpleMDE
  54. */
  55. copySimpleMdeAssets () {
  56. return fs.accessAsync('./assets/js/simplemde').then(() => {
  57. console.info(colors.white(' └── ') + colors.magenta('SimpleMDE directory already exists. Task skipped.'))
  58. return true
  59. }).catch(err => {
  60. if (err.code === 'ENOENT') {
  61. console.info(colors.white(' └── ') + colors.green('Copy + Minify SimpleMDE to assets...'))
  62. return fs.copy('./node_modules/simplemde/dist/simplemde.min.js', './assets/js/simplemde/simplemde.min.js')
  63. } else {
  64. throw err
  65. }
  66. })
  67. },
  68. /**
  69. * ACE Modes
  70. */
  71. copyAceModes () {
  72. return fs.accessAsync('./assets/js/ace').then(() => {
  73. console.info(colors.white(' └── ') + colors.magenta('ACE modes directory already exists. Task skipped.'))
  74. return true
  75. }).catch(err => {
  76. if (err.code === 'ENOENT') {
  77. console.info(colors.white(' └── ') + colors.green('Copy + Minify ACE modes to assets...'))
  78. return fs.ensureDirAsync('./assets/js/ace').then(() => {
  79. return Promise.join(
  80. // Core
  81. Promise.all([
  82. fs.readFileAsync('./node_modules/brace/index.js', 'utf8'),
  83. fs.readFileAsync('./node_modules/brace/ext/modelist.js', 'utf8'),
  84. fs.readFileAsync('./node_modules/brace/theme/dawn.js', 'utf8'),
  85. fs.readFileAsync('./node_modules/brace/theme/tomorrow_night.js', 'utf8'),
  86. fs.readFileAsync('./node_modules/brace/mode/markdown.js', 'utf8')
  87. ]).then(items => {
  88. console.info(colors.white(' ace.js'))
  89. let result = uglify.minify(items.join(';\n'), { output: { 'max_line_len': 1000000 } })
  90. return fs.writeFileAsync('./assets/js/ace/ace.js', result.code)
  91. }),
  92. // Modes
  93. fs.readdirAsync('./node_modules/brace/mode').then(modeList => {
  94. return Promise.map(modeList, mdFile => {
  95. return fs.readFileAsync(path.join('./node_modules/brace/mode', mdFile), 'utf8').then(modeCode => {
  96. console.info(colors.white(' mode-' + mdFile))
  97. let result = uglify.minify(modeCode, { output: { 'max_line_len': 1000000 } })
  98. return fs.writeFileAsync(path.join('./assets/js/ace', 'mode-' + mdFile), result.code)
  99. })
  100. }, { concurrency: 3 })
  101. })
  102. )
  103. })
  104. } else {
  105. throw err
  106. }
  107. })
  108. },
  109. /**
  110. * Delete Fusebox cache
  111. */
  112. cleanFuseboxCache () {
  113. console.info(colors.white(' └── ') + colors.green('Clearing fuse-box cache...'))
  114. return fs.emptyDirAsync('./.fusebox')
  115. },
  116. /**
  117. * Delete Test Results
  118. */
  119. cleanTestResults () {
  120. console.info(colors.white(' └── ') + colors.green('Clearing test results...'))
  121. return fs.remove('./test-results')
  122. }
  123. }