|
9 | 9 | /* Nota bene: |
10 | 10 | If you are getting more errors all the sudden, try running this: |
11 | 11 | $ script/i18n/create-translation-health-report.js -l en -r 000 |
12 | | - If there's any errors, const context = { ... } probably needs more data. |
| 12 | + If there's any errors before getting the JSON output, |
| 13 | + const context = { ... } probably needs more data. |
13 | 14 | */ |
14 | 15 |
|
15 | 16 | import { program } from 'commander' |
16 | 17 | import fs from 'fs/promises' |
17 | | -import { pick } from 'lodash-es' |
18 | | - |
19 | | -import { loadPages, loadPageMap } from '../../lib/page-data.js' |
20 | | -import loadSiteData from '../../lib/site-data.js' |
21 | | -import loadRedirects from '../../lib/redirects/precompile.js' |
22 | | -import { allVersions, allVersionKeys } from '../../lib/all-versions.js' |
23 | | -import { languageKeys } from '../../lib/languages.js' |
24 | | -import { getProductStringFromPath } from '../../lib/path-utils.js' |
25 | 18 |
|
26 | 19 | program |
27 | 20 | .description('Create a translation health report for one language.') |
28 | 21 | .requiredOption('-l, --language <language>', 'The language to health check') |
29 | | - .requiredOption('-r, --gitref <sha>', 'Language repo latest git commit short SHA') |
| 22 | + .option('-r, --gitref <sha>', 'Language repo latest git commit short SHA') |
30 | 23 | .parse(process.argv) |
31 | 24 |
|
32 | | -// Gather popularity data the search uses to prioritize errors |
33 | | -async function fetchPopularityData() { |
34 | | - const output = {} |
35 | | - const popularPagesRaw = await fs.readFile('lib/search/popular-pages.json', 'utf8') |
36 | | - for (const line of popularPagesRaw.split('\n')) { |
37 | | - try { |
38 | | - const row = JSON.parse(line) |
39 | | - output[row.path_article] = row.path_count |
40 | | - } catch {} |
41 | | - } |
42 | | - return output |
43 | | -} |
| 25 | +// Throw errors instead of falling back to English |
| 26 | +process.env.DEBUG_TRANSLATION_FALLBACKS = true |
| 27 | +// The error option stops everything, but we want it to continue to generate the full report |
| 28 | +process.env.ENABLED_LANGUAGES = `en,${program.opts().language}` |
44 | 29 |
|
45 | | -async function collectPageErrors(page, { language, data, redirects, plainPath, pageMap }) { |
46 | | - // Go through each version... |
47 | | - const promises = allVersionKeys |
48 | | - .filter((version) => page.applicableVersions.includes(version)) |
49 | | - .map(async (version) => { |
50 | | - // Collect if errors |
51 | | - const pageVersionErrors = [] |
52 | | - try { |
53 | | - const path = `/${language}/${version}/${plainPath}` |
54 | | - // Reference middleware/context.js for data shape |
55 | | - const context = { |
56 | | - ...data, // needed for all pages |
57 | | - currentVersion: version, // needed for all pages |
58 | | - currentLanguage: language, // needed for all pages |
59 | | - currentPath: path, // needed for all pages |
60 | | - currentVersionObj: allVersions[version], // needed for ifversion tag |
61 | | - currentProduct: getProductStringFromPath(path), // needed for learning-track on guides pages |
62 | | - pages: pageMap, // needed for learning-track on guides pages |
63 | | - redirects, // needed for learning-track on guides pages |
64 | | - } |
65 | | - await page.render(context, pageVersionErrors) |
66 | | - } catch (err) { |
67 | | - pageVersionErrors.push(err) |
68 | | - } |
69 | | - if (pageVersionErrors.length) { |
70 | | - return [ |
71 | | - version, |
72 | | - // Filter down properties to make it easier for |
73 | | - // translators to get the clearest information on the error |
74 | | - pageVersionErrors.map((err) => pick(err, ['name', 'message', 'token.content'])), |
75 | | - ] |
76 | | - // Other fields: Object.getOwnPropertyNames(err) |
77 | | - } |
78 | | - }) |
79 | | - const arr = (await Promise.all(promises)).filter(Boolean) |
80 | | - if (arr.length) { |
81 | | - return Object.fromEntries(arr) |
82 | | - } |
83 | | -} |
| 30 | +// In debug mode, it will call console.warn ... so overriding :) |
| 31 | +// Want to make sure the result is valid JSON |
| 32 | +const prevConsoleWarn = console.warn |
| 33 | +const prevConsoleError = console.error |
84 | 34 |
|
85 | | -function groupErrors(errors) { |
86 | | - return errors |
87 | | - .map((page) => Object.values(page.versions).flat()) |
88 | | - .flat() |
89 | | - .map((version) => version.message) |
90 | | - .reduce((sum, val) => { |
91 | | - sum[val] = sum[val] || 0 |
92 | | - sum[val]++ |
93 | | - return sum |
94 | | - }, {}) |
| 35 | +let issues = [] |
| 36 | +console.warn = console.error = (...args) => { |
| 37 | + if (args.length > 1) { |
| 38 | + issues.push({ message: args.map(String).join(' '), score: 0 }) |
| 39 | + } else if (typeof args[0] === 'string') { |
| 40 | + issues.push({ message: args[0], score: 0 }) |
| 41 | + } else if (args[0]?.constructor === Object) { |
| 42 | + const path = args[0].path?.replace('/index.md', '').replace('.md', '') |
| 43 | + issues.push({ path, message: args[0].message, score: scores[path] || 0 }) |
| 44 | + } |
95 | 45 | } |
96 | 46 |
|
97 | | -async function createReport() { |
98 | | - // Check that the language is valid |
99 | | - const { language, gitref } = program.opts() |
100 | | - if (!languageKeys.includes(language)) { |
101 | | - throw new Error(`Language ${language} is not in ${languageKeys.join()}.`) |
102 | | - } |
| 47 | +// Weird import syntax, but forces it to load after process.env... changes |
| 48 | +const { languageKeys } = await import('../../lib/languages.js') |
| 49 | +const { loadPages, loadPageMap } = await import('../../lib/page-data.js') |
| 50 | +const { precompileRedirects } = await import('../../lib/redirects/precompile.js') |
| 51 | +const { allVersions, allVersionKeys } = await import('../../lib/all-versions.js') |
| 52 | +const { getProductStringFromPath } = await import('../../lib/path-utils.js') |
103 | 53 |
|
104 | | - // Load popularity data to sort errors |
105 | | - const popularity = await fetchPopularityData() |
| 54 | +// Check that the language is valid |
| 55 | +const { language, gitref } = program.opts() |
| 56 | +if (!languageKeys.includes(language)) { |
| 57 | + throw new Error(`Language ${language} is not in ${languageKeys.join()}.`) |
| 58 | +} |
106 | 59 |
|
107 | | - // Load all pages |
108 | | - const allPages = await loadPages() |
109 | | - const dataErrors = [] |
110 | | - const data = loadSiteData(dataErrors)[language] |
111 | | - const pages = allPages |
112 | | - .filter((page) => page.languageCode === language) |
113 | | - // Early access pages log to the console, which would show in the report |
114 | | - .filter((page) => !page.relativePath.includes('early-access')) |
115 | | - const pageMap = await loadPageMap(pages) |
116 | | - const redirects = await loadRedirects(pages) |
| 60 | +// Gather popularity data the search uses to prioritize errors |
| 61 | +const scores = {} |
| 62 | +const popularPagesRaw = await fs.readFile('lib/search/popular-pages.json', 'utf8') |
| 63 | +for (const line of popularPagesRaw.split('\n')) { |
| 64 | + try { |
| 65 | + const row = JSON.parse(line) |
| 66 | + scores[row.path_article] = row.path_count |
| 67 | + } catch {} |
| 68 | +} |
117 | 69 |
|
118 | | - // Try to render each page |
119 | | - const pageErrors = ( |
120 | | - await Promise.all( |
121 | | - pages.map(async (page) => { |
122 | | - const plainPath = page.relativePath.replace('/index.md', '').replace('.md', '') |
123 | | - const errorsByVersion = await collectPageErrors(page, { |
124 | | - language, |
125 | | - data, |
126 | | - redirects, |
127 | | - plainPath, |
128 | | - pageMap, |
129 | | - }) |
130 | | - if (errorsByVersion) { |
131 | | - return { |
132 | | - path: plainPath, |
133 | | - popularity: popularity[plainPath] || 0, |
134 | | - versions: errorsByVersion, |
135 | | - } |
136 | | - } |
137 | | - }) |
138 | | - ) |
139 | | - ) |
140 | | - .filter(Boolean) |
141 | | - // Sort by popularity desc so the translators know what to focus on first |
142 | | - .sort((a, b) => b.popularity - a.popularity) |
| 70 | +// Load all pages in language |
| 71 | +const allPages = await loadPages() |
| 72 | +const pages = allPages.filter((page) => page.languageCode === language) |
| 73 | +const pageMap = await loadPageMap(pages) |
| 74 | +const redirects = await precompileRedirects(pages) |
143 | 75 |
|
144 | | - // Begin an output report |
145 | | - const report = { |
146 | | - language, |
147 | | - gitref, |
148 | | - datetime: new Date().toJSON(), |
149 | | - totalPages: pages.length, |
150 | | - totalErrorPages: pageErrors.length, |
151 | | - pageErrors, |
152 | | - // To group errors by message instead |
153 | | - groupedPageErrors: groupErrors(pageErrors), |
154 | | - // Filter down properties to make it easier for |
155 | | - // translators to get the clearest information on the error |
156 | | - dataErrors: dataErrors.map((err) => pick(err, ['name', 'message', 'token.content'])), |
| 76 | +// Try to render each page |
| 77 | +for (const page of pages) { |
| 78 | + const plainPath = page.relativePath.replace('/index.md', '').replace('.md', '') |
| 79 | + // Go through each version... |
| 80 | + const versions = allVersionKeys.filter((version) => page.applicableVersions.includes(version)) |
| 81 | + const pageIssues = {} |
| 82 | + for (const version of versions) { |
| 83 | + const path = `/${language}/${version}/${plainPath}` |
| 84 | + // Reference middleware/context.js for shape |
| 85 | + const context = { |
| 86 | + currentVersion: version, // needed for all pages |
| 87 | + currentLanguage: language, // needed for all pages |
| 88 | + currentPath: path, // needed for all pages |
| 89 | + currentVersionObj: allVersions[version], // needed for ifversion tag |
| 90 | + currentProduct: getProductStringFromPath(path), // needed for learning-track on guides pages |
| 91 | + pages: pageMap, // needed for learning-track on guides pages |
| 92 | + redirects, // needed for learning-track on guides pages |
| 93 | + } |
| 94 | + try { |
| 95 | + await page.render(context) |
| 96 | + } catch (err) { |
| 97 | + // Which messages apply to which versions |
| 98 | + pageIssues[err.message] = pageIssues[err.message] || [] |
| 99 | + pageIssues[err.message].push(version) |
| 100 | + } |
| 101 | + } |
| 102 | + if (Object.keys(pageIssues).length) { |
| 103 | + issues.push({ |
| 104 | + path: plainPath, |
| 105 | + messages: pageIssues, |
| 106 | + score: scores[plainPath] || 0, |
| 107 | + }) |
157 | 108 | } |
| 109 | +} |
| 110 | + |
| 111 | +// Sort by score desc so the translators know what to focus on first |
| 112 | +// Issues with more information should be higher |
| 113 | +issues = issues |
| 114 | + .filter((issue) => !issue.message?.includes('early-access')) |
| 115 | + .sort((a, b) => b.score - a.score || JSON.stringify(b).length - JSON.stringify(a).length) |
158 | 116 |
|
159 | | - return report |
| 117 | +// Begin an output report |
| 118 | +const report = { |
| 119 | + language, |
| 120 | + gitref, |
| 121 | + datetime: new Date().toJSON(), |
| 122 | + issuesCount: issues.length, |
| 123 | + issues, |
160 | 124 | } |
161 | 125 |
|
162 | | -console.warn = () => {} // shhh |
163 | | -console.log(JSON.stringify(await createReport(), null, 2)) |
| 126 | +console.warn = prevConsoleWarn |
| 127 | +console.error = prevConsoleError |
| 128 | +console.log(JSON.stringify(report, null, 2)) |
0 commit comments