-
Notifications
You must be signed in to change notification settings - Fork 66.8k
Expand file tree
/
Copy pathcount-translation-corruptions.ts
More file actions
237 lines (203 loc) · 6.79 KB
/
count-translation-corruptions.ts
File metadata and controls
237 lines (203 loc) · 6.79 KB
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
import path from 'path'
import fs from 'fs'
import { program } from 'commander'
import walk from 'walk-sync'
import { engine } from '@/content-render/liquid/engine'
import { allVersions } from '@/versions/lib/all-versions'
import languages from '@/languages/lib/languages-server'
import warmServer from '@/frame/lib/warm-server'
import type { Site } from '@/types'
import { correctTranslatedContentStrings } from '@/languages/lib/correct-translation-content'
program
.description('Tally the number of liquid corruptions in a translation. Outputs JSON to stdout.')
.argument('[language...]', 'language(s) to compare against')
.action(main)
program.parse(process.argv)
type Reusables = Map<string, string>
interface CorruptionEntry {
file: string
location: string
error: string
illegalTag?: string
}
interface LanguageResult {
language: string
languageName: string
total: number
corruptions: CorruptionEntry[]
byLocation: Record<string, number>
topIllegalTags: Array<{ tag: string; count: number }>
}
interface FrontmatterError {
file: string
message: string
}
interface CorruptionReport {
hasFailures: boolean
totalCount: number
frontmatterErrors: FrontmatterError[]
languages: LanguageResult[]
}
async function main(languageCodes: string[]) {
// Suppress warmServer noise (frontmatter errors from translations)
// and capture them as structured data instead
const originalError = console.error
const originalWarn = console.warn
const originalLog = console.log
const suppressedErrors: string[] = []
console.error = (...args: unknown[]) => {
suppressedErrors.push(args.map(String).join(' '))
}
console.warn = (...args: unknown[]) => {
suppressedErrors.push(args.map(String).join(' '))
}
console.log = () => {}
let langCodes: string[]
let site: Site
let reusables: Reusables
try {
langCodes = languageCodes.length
? languageCodes
: Object.keys(languages).filter((x) => x !== 'en')
for (const code of langCodes) {
if (!(code in languages)) {
throw new Error(`Language ${code} not found`)
}
if (code === 'en') {
throw new Error("Can't test in English ('en')")
}
}
site = await warmServer(languageCodes.length ? ['en', ...langCodes] : [])
reusables = getReusables()
} finally {
console.error = originalError
console.warn = originalWarn
console.log = originalLog
}
const frontmatterErrors = parseFrontmatterErrors(suppressedErrors)
const languageResults: LanguageResult[] = []
for (const languageCode of langCodes) {
languageResults.push(await run(languageCode, site, reusables))
}
const totalCount = languageResults.reduce((sum, r) => sum + r.total, 0)
const report: CorruptionReport = {
hasFailures: totalCount > 0 || frontmatterErrors.length > 0,
totalCount,
frontmatterErrors,
languages: languageResults,
}
console.log(JSON.stringify(report, null, 2))
}
function parseFrontmatterErrors(suppressedErrors: string[]): FrontmatterError[] {
const errors: FrontmatterError[] = []
const seen = new Set<string>()
for (const err of suppressedErrors) {
const match = err.match(/translations\/[^\s']+/)
if (match && !seen.has(match[0])) {
seen.add(match[0])
errors.push({ file: match[0], message: 'YML parsing error' })
}
}
return errors
}
function getReusables(): Reusables {
const reusables = new Map()
const files = walk('data/reusables', {
includeBasePath: true,
globs: ['**/*.md'],
ignore: ['**/README.md'],
})
for (const file of files) {
const content = fs.readFileSync(file, 'utf8')
reusables.set(file, content)
}
return reusables
}
// Build a minimal Liquid render context for validating translated content.
// ifversion needs currentVersionObj; data tags call getDataByLanguage() internally.
const defaultVersion = 'free-pro-team@latest'
function buildRenderContext(languageCode: string) {
return {
currentLanguage: languageCode,
currentVersion: defaultVersion,
currentVersionObj: allVersions[defaultVersion],
}
}
async function run(
languageCode: string,
site: Site,
englishReusables: Reusables,
): Promise<LanguageResult> {
const language = languages[languageCode as keyof typeof languages]
const corruptions: CorruptionEntry[] = []
const wheres = new Map<string, number>()
const illegalTags = new Map<string, number>()
const context = buildRenderContext(languageCode)
// Suppress console.warn during rendering — the {% data %} tag warns
// when it can't find translated data, which is expected noise.
const originalWarn = console.warn
console.warn = () => {}
function countError(error: Error, where: string, file: string) {
const errorString = error.message
let illegalTag: string | undefined
const errorWithExtras = error as Error & { token?: { content?: string } }
if (errorString.includes('illegal tag syntax') && errorWithExtras.token?.content) {
illegalTag = errorWithExtras.token.content
illegalTags.set(illegalTag, (illegalTags.get(illegalTag) || 0) + 1)
}
corruptions.push({ file, location: where, error: errorString, illegalTag })
wheres.set(where, (wheres.get(where) || 0) + 1)
}
for (const page of site.pageList) {
if (page.languageCode !== languageCode) continue
const strings: string[][] = [
['title', page.title],
['shortTitle', page.shortTitle || ''],
['intro', page.intro || ''],
['markdown', page.markdown],
].filter(([, string]) => Boolean(string))
for (const [where, string] of strings) {
try {
await engine.parseAndRender(string, context)
} catch (error) {
if (error instanceof Error) {
countError(error, where, page.relativePath)
} else {
throw error
}
}
}
}
for (const [relativePath, englishContent] of Array.from(englishReusables.entries())) {
try {
const filePath = path.join(language.dir, relativePath)
const rawContent = fs.readFileSync(filePath, 'utf8')
const correctedContent = correctTranslatedContentStrings(rawContent, englishContent, {
code: languageCode,
relativePath,
})
await engine.parseAndRender(correctedContent, context)
} catch (error) {
if (error instanceof Error && error.message.startsWith('ENOENT')) {
continue
} else if (error instanceof Error) {
countError(error, 'reusable', relativePath)
} else {
throw error
}
}
}
const topIllegalTags = Array.from(illegalTags.entries())
.sort((a, b) => b[1] - a[1])
.slice(0, 10)
.map(([tag, count]) => ({ tag, count }))
console.warn = originalWarn
return {
language: languageCode,
languageName: language.name,
total: corruptions.length,
corruptions,
byLocation: Object.fromEntries(wheres),
topIllegalTags,
}
}