Skip to content

Commit 1675c3e

Browse files
authored
Merge pull request #12071 from github/repo-sync
repo sync
2 parents 7347e05 + 9774388 commit 1675c3e

File tree

2 files changed

+66
-31
lines changed

2 files changed

+66
-31
lines changed

script/i18n/lint-translation-files.js

100755100644
Lines changed: 61 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,42 +2,73 @@
22

33
// [start-readme]
44
//
5-
// Use this script as part of the Crowdin merge process to output a list of parsing and rendering
6-
// errors in translated files and run script/i18n/reset-translated-file.js on them.
5+
// Use this script as part of the Crowdin merge process to output a list of either parsing
6+
// or rendering errors in translated files and run script/i18n/reset-translated-file.js on them.
77
//
88
// [end-readme]
99

1010
import { execSync } from 'child_process'
11+
import program from 'commander'
1112

12-
const parsingErrorsLog = '~/docs-translation-parsing-error.txt'
13-
const renderErrorsLog = '~/docs-translation-rendering-error.txt'
14-
15-
// 1. Check for parsing errors and output to file. Note this one must be run FIRST.
16-
console.log('Checking for parsing errors...')
17-
try {
18-
execSync(`TEST_TRANSLATION=true npx jest linting/lint-files > ${parsingErrorsLog}`)
19-
} catch (error) {
20-
console.log('There were new parsing errors!')
13+
// Set up supported linting check types and their corresponding commands.
14+
const CHECK_COMMANDS = {
15+
parsing: 'TEST_TRANSLATION=true npx jest linting/lint-files',
16+
rendering: 'script/i18n/test-render-translation.js',
2117
}
18+
const SUPPORTED_CHECK_TYPES = Object.keys(CHECK_COMMANDS)
19+
const CHECK_TYPE_DESCRIPTION = `Specify no more than one of the supported checks: ${SUPPORTED_CHECK_TYPES.join(
20+
', '
21+
)}`
22+
23+
// Initialize a new program for linting translation files, requiring a check type.
24+
program
25+
.description('lint translation files')
26+
.requiredOption('-c, --check <type>', CHECK_TYPE_DESCRIPTION)
27+
.parse(process.argv)
28+
29+
// Cache a reference to the client's specified check type.
30+
const specifiedCheckType = program.opts().check
2231

23-
// 2. Check for rendering errors and output to file. Note this one must be run SECOND.
24-
console.log('Checking for rendering errors...')
25-
try {
26-
execSync(`script/i18n/test-render-translation.js > ${renderErrorsLog}`)
27-
} catch (error) {
28-
console.log('There were new rendering errors!')
32+
if (SUPPORTED_CHECK_TYPES.includes(specifiedCheckType)) {
33+
// Lint and reset the files based on a supported check type.
34+
lintAndResetFiles(specifiedCheckType)
35+
} else {
36+
// Otherwise, print an error message.
37+
console.error(`
38+
${specifiedCheckType} is not a supported check type.
39+
${CHECK_TYPE_DESCRIPTION}
40+
`)
2941
}
3042

31-
// Reset the broken files.
32-
console.log('Resetting broken files...')
33-
execSync(
34-
`cat ${parsingErrorsLog} ${renderErrorsLog} | egrep "^translations/.*/(.+.md|.+.yml)$" | uniq | xargs -L1 script/i18n/reset-translated-file.js --prefer-main`
35-
)
36-
37-
// Print a message with next steps.
38-
console.log(`Success!
39-
40-
Verify changes with git status and then run:
41-
42-
git commit --no-verify -m "Reverted translated files with parsing and rendering errors"
43-
`)
43+
/**
44+
* Lint and reset the files based on the specified check type.
45+
* @param {string} checkType
46+
* @return {undefined}
47+
*/
48+
function lintAndResetFiles(checkType) {
49+
console.log(`Running ${checkType} check...`)
50+
51+
const log = `~/docs-translation-${checkType}-error.txt`
52+
const cmd = `${CHECK_COMMANDS[checkType]} > ${log}`
53+
54+
// Lint the files based on the check type and output the errors to a log file.
55+
try {
56+
execSync(cmd[checkType])
57+
} catch (error) {
58+
console.log(`There were new ${checkType} errors!`)
59+
return
60+
}
61+
62+
// Reset the files
63+
execSync(
64+
`cat ${log} | egrep "^translations/.*/(.+.md|.+.yml)$" | uniq | xargs -L1 script/i18n/reset-translated-file.js --prefer-main`
65+
)
66+
67+
// Print a message with next steps
68+
console.log(`Success!
69+
70+
Verify changes with git status and then run:
71+
72+
git commit --no-verify -m "Reverted translated files with ${checkType} errors"
73+
`)
74+
}

tests/unit/pages.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,11 @@ describe('pages module', () => {
165165
.value()
166166

167167
const diff = difference(nonEnglishPaths, englishPaths)
168-
const failureMessage = `Unmatched non-English pages ${diff.length}:\n - ${diff.join('\n - ')}`
168+
const failureMessage = `
169+
Found ${diff.length} non-English pages without a matching English page:\n - ${diff.join('\n - ')}
170+
171+
Remove them with script/i18n/prune-stale-files.js and commit your changes using "git commit --no-verify".
172+
`
169173
expect(diff.length, failureMessage).toBe(0)
170174
})
171175
})

0 commit comments

Comments
 (0)