locale-packs.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. const glob = require('glob')
  2. const Uppy = require('../packages/@uppy/core')
  3. const chalk = require('chalk')
  4. const path = require('path')
  5. const stringifyObject = require('stringify-object')
  6. const fs = require('fs')
  7. const uppy = new Uppy()
  8. let localePack = {}
  9. const plugins = {}
  10. const sources = {}
  11. console.warn('\n--> Make sure to run `npm run build:lib` for this locale script to work properly. ')
  12. const mode = process.argv[2]
  13. if (mode === 'build') {
  14. build()
  15. } else if (mode === 'test') {
  16. test()
  17. } else {
  18. throw new Error("First argument must be either 'build' or 'test'")
  19. }
  20. function getSources (pluginName) {
  21. const dependencies = {
  22. // because 'provider-views' doesn't have its own locale, it uses Core's defaultLocale
  23. core: ['provider-views']
  24. }
  25. const globPath = path.join(__dirname, '..', 'packages', '@uppy', pluginName, 'lib', '**', '*.js')
  26. let contents = glob.sync(globPath).map((file) => {
  27. return fs.readFileSync(file, 'utf-8')
  28. })
  29. if (dependencies[pluginName]) {
  30. dependencies[pluginName].forEach((addPlugin) => {
  31. contents = contents.concat(getSources(addPlugin))
  32. })
  33. }
  34. return contents
  35. }
  36. function buildPluginsList () {
  37. // Go over all uppy plugins, check if they are constructors
  38. // and instanciate them, check for defaultLocale property,
  39. // then add to plugins object
  40. const packagesGlobPath = path.join(__dirname, '..', 'packages', '@uppy', '*', 'package.json')
  41. const files = glob.sync(packagesGlobPath)
  42. console.log('--> Checked plugins could be instantiated and have defaultLocale in them:\n')
  43. for (const file of files) {
  44. const dirName = path.dirname(file)
  45. const pluginName = path.basename(dirName)
  46. if (pluginName === 'locales' ||
  47. pluginName === 'react-native' ||
  48. pluginName === 'vue' ||
  49. pluginName === 'svelte') {
  50. continue
  51. }
  52. const Plugin = require(dirName)
  53. let plugin
  54. // A few hacks to emulate browser environment because e.g.:
  55. // GoldenRetrieves calls upon MetaDataStore in the constructor, which uses localStorage
  56. // @TODO Consider rewriting constructors so they don't make imperative calls that rely on
  57. // browser environment (OR: just keep this browser mocking, if it's only causing issues for this script, it doesn't matter)
  58. global.location = { protocol: 'https' }
  59. global.navigator = { userAgent: '' }
  60. global.localStorage = {
  61. key: () => { },
  62. getItem: () => { }
  63. }
  64. global.window = {
  65. indexedDB: {
  66. open: () => { return {} }
  67. }
  68. }
  69. global.document = {
  70. createElement: () => {
  71. return { style: {} }
  72. }
  73. }
  74. try {
  75. if (pluginName === 'provider-views') {
  76. plugin = new Plugin(plugins['drag-drop'], {
  77. companionPattern: '',
  78. companionUrl: 'https://companion.uppy.io'
  79. })
  80. } else if (pluginName === 'store-redux') {
  81. plugin = new Plugin({ store: { dispatch: () => { } } })
  82. } else {
  83. plugin = new Plugin(uppy, {
  84. companionPattern: '',
  85. companionUrl: 'https://companion.uppy.io',
  86. params: {
  87. auth: {
  88. key: 'x'
  89. }
  90. }
  91. })
  92. }
  93. } catch (err) {
  94. if (err.message !== 'Plugin is not a constructor') {
  95. console.error(`--> While trying to instantiate plugin: ${pluginName}, this error was thrown: `)
  96. throw err
  97. }
  98. }
  99. if (plugin && plugin.defaultLocale) {
  100. console.log(`[x] Check plugin: ${pluginName}`)
  101. plugins[pluginName] = plugin
  102. sources[pluginName] = getSources(pluginName)
  103. } else {
  104. console.log(`[ ] Check plugin: ${pluginName}`)
  105. }
  106. }
  107. console.log('')
  108. return { plugins, sources }
  109. }
  110. function addLocaleToPack (plugin, pluginName) {
  111. const localeStrings = plugin.defaultLocale.strings
  112. for (const key in localeStrings) {
  113. const valueInPlugin = JSON.stringify(localeStrings[key])
  114. const valueInPack = JSON.stringify(localePack[key])
  115. if (key in localePack && valueInPlugin !== valueInPack) {
  116. console.error(`⚠ Plugin ${chalk.magenta(pluginName)} has a duplicate key: ${chalk.magenta(key)}`)
  117. console.error(` Value in plugin: ${chalk.cyan(valueInPlugin)}`)
  118. console.error(` Value in pack : ${chalk.yellow(valueInPack)}`)
  119. console.error()
  120. }
  121. localePack[key] = localeStrings[key]
  122. }
  123. }
  124. function checkForUnused (fileContents, pluginName, localePack) {
  125. const buff = fileContents.join('\n')
  126. for (const key in localePack) {
  127. const regPat = new RegExp(`(i18n|i18nArray)\\([^\\)]*['\`"]${key}['\`"]`, 'g')
  128. if (!buff.match(regPat)) {
  129. console.error(`⚠ defaultLocale key: ${chalk.magenta(key)} not used in plugin: ${chalk.cyan(pluginName)}`)
  130. }
  131. }
  132. }
  133. function sortObjectAlphabetically (obj, sortFunc) {
  134. return Object.keys(obj).sort(sortFunc).reduce(function (result, key) {
  135. result[key] = obj[key]
  136. return result
  137. }, {})
  138. }
  139. function createTypeScriptLocale (plugin, pluginName) {
  140. const allowedStringTypes = Object.keys(plugin.defaultLocale.strings)
  141. .map(key => ` | '${key}'`)
  142. .join('\n')
  143. const pluginClassName = pluginName === 'core' ? 'Core' : plugin.id
  144. const localePath = path.join(__dirname, '..', 'packages', '@uppy', pluginName, 'types', 'generatedLocale.d.ts')
  145. const localeTypes =
  146. 'import Uppy = require(\'@uppy/core\')\n' +
  147. '\n' +
  148. `type ${pluginClassName}Locale = Uppy.Locale` + '<\n' +
  149. allowedStringTypes + '\n' +
  150. '>\n' +
  151. '\n' +
  152. `export = ${pluginClassName}Locale\n`
  153. fs.writeFileSync(localePath, localeTypes)
  154. }
  155. function build () {
  156. const { plugins, sources } = buildPluginsList()
  157. for (const pluginName in plugins) {
  158. addLocaleToPack(plugins[pluginName], pluginName)
  159. }
  160. for (const pluginName in plugins) {
  161. createTypeScriptLocale(plugins[pluginName], pluginName)
  162. }
  163. localePack = sortObjectAlphabetically(localePack)
  164. for (const pluginName in sources) {
  165. checkForUnused(sources[pluginName], pluginName, sortObjectAlphabetically(plugins[pluginName].defaultLocale.strings))
  166. }
  167. const prettyLocale = stringifyObject(localePack, {
  168. indent: ' ',
  169. singleQuotes: true,
  170. inlineCharacterLimit: 12
  171. })
  172. const localeTemplatePath = path.join(__dirname, '..', 'packages', '@uppy', 'locales', 'template.js')
  173. const template = fs.readFileSync(localeTemplatePath, 'utf-8')
  174. const finalLocale = template.replace('en_US.strings = {}', 'en_US.strings = ' + prettyLocale)
  175. const localePackagePath = path.join(__dirname, '..', 'packages', '@uppy', 'locales', 'src', 'en_US.js')
  176. fs.writeFileSync(localePackagePath, finalLocale, 'utf-8')
  177. console.log(`✅ Written '${localePackagePath}'`)
  178. }
  179. function test () {
  180. const leadingLocaleName = 'en_US'
  181. const followerLocales = {}
  182. const followerValues = {}
  183. const localePackagePath = path.join(__dirname, '..', 'packages', '@uppy', 'locales', 'src', '*.js')
  184. glob.sync(localePackagePath).forEach((localePath) => {
  185. const localeName = path.basename(localePath, '.js')
  186. // we renamed the es_GL → gl_ES locale, and kept the old name
  187. // for backwards-compat, see https://github.com/transloadit/uppy/pull/1929
  188. if (localeName === 'es_GL') return
  189. // Builds array with items like: 'uploadingXFiles'
  190. // We do not check nested items because different languages may have different amounts of plural forms.
  191. followerValues[localeName] = require(localePath).strings
  192. followerLocales[localeName] = Object.keys(followerValues[localeName])
  193. })
  194. // Take aside our leading locale: en_US
  195. const leadingLocale = followerLocales[leadingLocaleName]
  196. const leadingValues = followerValues[leadingLocaleName]
  197. delete followerLocales[leadingLocaleName]
  198. // Compare all follower Locales (RU, DE, etc) with our leader en_US
  199. const warnings = []
  200. const fatals = []
  201. for (const followerName in followerLocales) {
  202. const followerLocale = followerLocales[followerName]
  203. const missing = leadingLocale.filter((key) => !followerLocale.includes(key))
  204. const excess = followerLocale.filter((key) => !leadingLocale.includes(key))
  205. missing.forEach((key) => {
  206. // Items missing are a non-fatal warning because we don't want CI to bum out over all languages
  207. // as soon as we add some English
  208. let value = leadingValues[key]
  209. if (typeof value === 'object') {
  210. // For values with plural forms, just take the first one right now
  211. value = value[Object.keys(value)[0]]
  212. }
  213. warnings.push(`${chalk.cyan(followerName)} locale has missing string: '${chalk.red(key)}' that is present in ${chalk.cyan(leadingLocaleName)} with value: ${chalk.yellow(leadingValues[key])}`)
  214. })
  215. excess.forEach((key) => {
  216. // Items in excess are a fatal because we should clean up follower languages once we remove English strings
  217. fatals.push(`${chalk.cyan(followerName)} locale has excess string: '${chalk.yellow(key)}' that is not present in ${chalk.cyan(leadingLocaleName)}. `)
  218. })
  219. }
  220. if (warnings.length) {
  221. console.error('--> Locale warnings: ')
  222. console.error(warnings.join('\n'))
  223. console.error('')
  224. }
  225. if (fatals.length) {
  226. console.error('--> Locale fatal warnings: ')
  227. console.error(fatals.join('\n'))
  228. console.error('')
  229. process.exit(1)
  230. }
  231. if (!warnings.length && !fatals.length) {
  232. console.log(`--> All locale strings have matching keys ${chalk.green(': )')}`)
  233. console.log('')
  234. }
  235. }