forked from transloadit/uppy
/
test.mjs
159 lines (131 loc) · 4.09 KB
/
test.mjs
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
48
49
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/* eslint-disable no-console, prefer-arrow-callback */
import path from 'node:path'
import fs from 'node:fs'
import { fileURLToPath } from 'node:url'
import glob from 'glob'
import chalk from 'chalk'
import { getPaths, omit } from './helpers.mjs'
const root = fileURLToPath(new URL('../../', import.meta.url))
const leadingLocaleName = 'en_US'
const mode = process.argv[2]
const pluginLocaleDependencies = {
core: 'provider-views',
}
test()
.then(() => {
console.log('\n')
console.log('No blocking issues found')
})
.catch((error) => {
console.error(error)
process.exit(1)
})
function test () {
switch (mode) {
case 'unused':
return getPaths(`${root}/packages/@uppy/**/src/locale.js`)
.then((paths) => paths.map((filePath) => path.basename(path.join(filePath, '..', '..'))))
.then(getAllFilesPerPlugin)
.then(unused)
case 'warnings':
return getPaths(`${root}/packages/@uppy/locales/src/*.js`)
.then(importFiles)
.then((locales) => ({
leadingLocale: locales[leadingLocaleName],
followerLocales: omit(locales, leadingLocaleName),
}))
.then(warnings)
default:
return Promise.reject(new Error(`Invalid mode "${mode}"`))
}
}
async function importFiles (paths) {
const locales = {}
for (const filePath of paths) {
const localeName = path.basename(filePath, '.js')
// Note: `.default` should be removed when we move to ESM
const locale = (await import(filePath)).default
locales[localeName] = locale.strings
}
return locales
}
function getAllFilesPerPlugin (pluginNames) {
const filesPerPlugin = {}
function getFiles (name) {
return glob
.sync(`${root}/packages/@uppy/${name}/lib/**/*.js`)
.filter((filePath) => !filePath.includes('locale.js'))
.map((filePath) => fs.readFileSync(filePath, 'utf-8'))
}
for (const name of pluginNames) {
filesPerPlugin[name] = getFiles(name)
if (name in pluginLocaleDependencies) {
filesPerPlugin[name] = filesPerPlugin[name].concat(
getFiles(pluginLocaleDependencies[name]),
)
}
}
return filesPerPlugin
}
async function unused (filesPerPlugin, data) {
for (const [name, fileStrings] of Object.entries(filesPerPlugin)) {
const fileString = fileStrings.join('\n')
const localePath = path.join(
root,
'packages',
'@uppy',
name,
'src',
'locale.js',
)
const locale = (await import(localePath)).default
for (const key of Object.keys(locale.strings)) {
const regPat = new RegExp(
`(i18n|i18nArray)\\([^\\)]*['\`"]${key}['\`"]`,
'g',
)
if (!fileString.match(regPat)) {
return Promise.reject(new Error(`Unused locale key "${key}" in @uppy/${name}`))
}
}
}
return data
}
function warnings ({ leadingLocale, followerLocales }) {
const entries = Object.entries(followerLocales)
const logs = []
for (const [name, locale] of entries) {
const missing = Object.keys(leadingLocale).filter((key) => !(key in locale))
const excess = Object.keys(locale).filter((key) => !(key in leadingLocale))
logs.push('\n')
logs.push(`--> Keys from ${leadingLocaleName} missing in ${name}`)
logs.push('\n')
for (const key of missing) {
let value = leadingLocale[key]
if (typeof value === 'object') {
// For values with plural forms, just take the first one right now
value = value[Object.keys(value)[0]]
}
logs.push(
[
`${chalk.cyan(name)} locale has missing string: '${chalk.red(key)}'`,
`that is present in ${chalk.cyan(leadingLocaleName)}`,
`with value: ${chalk.yellow(value)}`,
].join(' '),
)
}
logs.push('\n')
logs.push(`--> Keys from ${name} missing in ${leadingLocaleName}`)
logs.push('\n')
for (const key of excess) {
logs.push(
[
`${chalk.cyan(name)} locale has excess string:`,
`'${chalk.yellow(key)}' that is not present`,
`in ${chalk.cyan(leadingLocaleName)}.`,
].join(' '),
)
}
}
console.log(logs.join('\n'))
}