Skip to content

Commit a8d54c9

Browse files
authored
Improvements in tools to help merge crowdin PRs (github#18409)
- add `script/test-render-translation.js` to render all translated content to catch malformed liquid that would cause render errors - improve test output for `script/fix-translation-errors.js` and `tests/content/lint-files.js` - make it so `script/reset-translated-file.js` can handle files that have been renamed
1 parent 3899af0 commit a8d54c9

File tree

8 files changed

+348
-219
lines changed

8 files changed

+348
-219
lines changed

jest.config.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ const isBrowser = process.env.BROWSER
44
const isActions = Boolean(process.env.GITHUB_ACTIONS)
55
const testTranslation = Boolean(process.env.TEST_TRANSLATION)
66

7-
const reporters = ['default']
7+
let reporters = ['default']
88

99
if (testTranslation) {
1010
// only use custom reporter if we are linting translations
11-
reporters.push('<rootDir>/tests/helpers/lint-translation-reporter.js')
11+
reporters = ['<rootDir>/tests/helpers/lint-translation-reporter.js']
1212
} else if (isActions) {
1313
reporters.push('jest-github-actions-reporter')
1414
}

package-lock.json

Lines changed: 55 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@
158158
"replace": "^1.2.0",
159159
"robots-parser": "^2.1.1",
160160
"start-server-and-test": "^1.12.0",
161+
"strip-ansi": "^6.0.0",
161162
"supertest": "^4.0.2",
162163
"url-template": "^2.0.8",
163164
"webpack-dev-middleware": "^3.7.2",

script/fix-translation-errors.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,14 @@ changedFilesRelPaths.forEach(async (relPath) => {
7171
if (!engResult) return
7272
const { data: engData } = engResult
7373

74-
console.log(chalk.red('fixing errors in ') + chalk.bold(relPath))
74+
console.log(chalk.bold(relPath))
7575

7676
const newData = data
7777

78-
fixableErrors.forEach(({ property }) => {
78+
fixableErrors.forEach(({ property, message }) => {
7979
const correctValue = get(engData, property)
80-
console.log(` [${property}]: ${get(data, property)} -> ${correctValue}`)
80+
console.log(chalk.red(` error message: [${property}] ${message}`))
81+
console.log(` fix property [${property}]: ${get(data, property)} -> ${correctValue}`)
8182
set(newData, property, correctValue)
8283
})
8384

script/reset-translated-file.js

Lines changed: 30 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -13,63 +13,56 @@
1313
//
1414
// Examples:
1515
//
16-
// reset a single translated file using a relative path:
1716
// $ script/reset-translated-file.js translations/es-XL/content/actions/index.md
1817
//
19-
// reset a single translated file using a full path:
20-
// $ script/reset-translated-file.js /Users/z/git/github/docs-internal/translations/es-XL/content/actions/index.md
21-
//
22-
// reset all language variants of a single English file (using a relative path):
23-
// $ script/reset-translated-file.js content/actions/index.md
24-
// $ script/reset-translated-file.js data/ui.yml
25-
//
26-
// reset all language variants of a single English file (using a full path):
27-
// $ script/reset-translated-file.js /Users/z/git/github/docs-internal/content/desktop/index.md
28-
// $ script/reset-translated-file.js /Users/z/git/github/docs-internal/data/ui.yml
29-
//
3018
// [end-readme]
3119

3220
const program = require('commander')
3321
const { execSync } = require('child_process')
3422
const assert = require('assert')
3523
const fs = require('fs')
3624
const path = require('path')
37-
const languages = require('../lib/languages')
25+
const chalk = require('chalk')
3826

3927
program
4028
.description('reset translated files')
41-
.option('-m, --use-main', 'Reset file to the translated file from `main` branch instead of from English source.')
29+
.option('-m, --prefer-main', 'Reset file to the translated file, try using the file from `main` branch first, if not found (usually due to renaming), fall back to English source.')
4230
.parse(process.argv)
4331

32+
const resetToEnglishSource = (translationFilePath) => {
33+
assert(translationFilePath.startsWith('translations/'), 'path argument must be in the format `translations/<lang>/path/to/file`')
34+
assert(fs.existsSync(translationFilePath), `file does not exist: ${translationFilePath}`)
35+
36+
const relativePath = translationFilePath.split(path.sep).slice(2).join(path.sep)
37+
const englishFile = path.join(process.cwd(), relativePath)
38+
assert(fs.existsSync(englishFile), `file does not exist: ${englishFile}`)
39+
40+
// replace file with English source
41+
const englishContent = fs.readFileSync(englishFile, 'utf8')
42+
fs.writeFileSync(translationFilePath, englishContent)
43+
console.log('-> reverted to English: %s', path.relative(process.cwd(), translationFilePath))
44+
}
45+
4446
const [pathArg] = program.args
4547
assert(pathArg, 'first arg must be a target filename')
46-
let languageCode
4748

4849
// Is the arg a fully-qualified path?
49-
let relativePath = fs.existsSync(pathArg)
50+
const relativePath = fs.existsSync(pathArg)
5051
? path.relative(process.cwd(), pathArg)
5152
: pathArg
5253

53-
if (program.useMain) {
54-
execSync(`git checkout main -- ${relativePath}`)
55-
console.log('reverted to file from main branch: %s', relativePath)
56-
} else {
57-
// extract relative path and language code if pathArg is in the format `translations/<lang>/path/to/file`
58-
if (relativePath.startsWith('translations/')) {
59-
languageCode = Object.values(languages).find(language => relativePath.startsWith(language.dir) && language.code !== 'en').code
60-
relativePath = relativePath.split(path.sep).slice(2).join(path.sep)
54+
if (program.preferMain) {
55+
try {
56+
execSync(`git checkout main -- ${relativePath}`, { stdio: 'pipe' })
57+
console.log('-> reverted to file from main branch: %s', relativePath)
58+
} catch (e) {
59+
if (e.message.includes('pathspec')) {
60+
console.warn(chalk.red(`cannot find ${relativePath} in main branch (likely because it was renamed); falling back to English source file.`))
61+
resetToEnglishSource(relativePath)
62+
} else {
63+
console.warn(e.message)
64+
}
6165
}
62-
63-
const englishFile = path.join(process.cwd(), relativePath)
64-
assert(fs.existsSync(englishFile), `file does not exist: ${englishFile}`)
65-
const englishContent = fs.readFileSync(englishFile, 'utf8')
66-
67-
Object.values(languages).forEach(({ code }) => {
68-
if (code === 'en') return
69-
if (languageCode && languageCode !== code) return
70-
71-
const translatedFile = path.join(process.cwd(), languages[code].dir, relativePath)
72-
fs.writeFileSync(translatedFile, englishContent)
73-
console.log('reverted to English: %s', path.relative(process.cwd(), translatedFile))
74-
})
66+
} else {
67+
resetToEnglishSource(relativePath)
7568
}

script/test-render-translation.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
const renderContent = require('../lib/render-content')
2+
const loadSiteData = require('../lib/site-data')
3+
const { loadPages } = require('../lib/pages')
4+
const languages = require('../lib/languages')
5+
const path = require('path')
6+
const { execSync } = require('child_process')
7+
const fs = require('fs')
8+
const frontmatter = require('../lib/frontmatter')
9+
const chalk = require('chalk')
10+
11+
const main = async () => {
12+
const siteData = loadSiteData()
13+
const pages = await loadPages()
14+
const contextByLanguage = {}
15+
for (const lang in languages) {
16+
const langObj = languages[lang]
17+
const [crowdinLangCode] = langObj.dir === '' ? 'en' : langObj.dir.split('/').slice(1)
18+
if (!crowdinLangCode) continue
19+
contextByLanguage[crowdinLangCode] = {
20+
site: siteData[langObj.code].site,
21+
currentLanguage: langObj.code,
22+
currentVersion: 'free-pro-team@latest'
23+
}
24+
}
25+
26+
const rootDir = path.join(__dirname, '..')
27+
28+
const changedFilesRelPaths = execSync('git diff --name-only origin/main | egrep "^translations/.*/.+.md$"', { maxBuffer: 1024 * 1024 * 100 })
29+
.toString()
30+
.split('\n')
31+
.filter(path => path !== '' && !path.endsWith('README.md'))
32+
.sort()
33+
34+
console.log(`Found ${changedFilesRelPaths.length} translated files.`)
35+
36+
changedFilesRelPaths.forEach(async (relPath) => {
37+
const fullPath = path.join(rootDir, relPath)
38+
const lang = relPath.split('/')[1]
39+
const context = {
40+
...contextByLanguage[lang],
41+
pages,
42+
page: pages.find(page => page.fullPath === fullPath),
43+
redirects: {}
44+
}
45+
if (!context.page && !relPath.includes('data/reusables')) return
46+
const fileContents = await fs.promises.readFile(fullPath, 'utf8')
47+
const { content } = frontmatter(fileContents)
48+
try {
49+
await renderContent.liquid.parseAndRender(content, context)
50+
} catch (err) {
51+
console.log(chalk.bold(relPath))
52+
console.log(chalk.red(` error message: ${err.message}`))
53+
}
54+
})
55+
}
56+
57+
main()

0 commit comments

Comments
 (0)