Skip to content

Commit 5bb2c7a

Browse files
[Localization] Refactor script for linting translation files to require check type (#22653)
* feat: stub two reset scripts * refactor: split lint script into two separate files * fix: revert back to original lint script * refactor: use commander interface for linting translation files * Skip reset attempt when violation list is empty So that "execSync(`cat ${log}...`)" in line 64 is never executed. When there are no errors, the log file is not created, and unless it had been previously created by another process, the script will error out. Co-authored-by: Marcelo Jacobus <marcelo.jacobus@gmail.com>
1 parent f66b87c commit 5bb2c7a

1 file changed

Lines changed: 61 additions & 30 deletions

File tree

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+
}

0 commit comments

Comments
 (0)