|
2 | 2 |
|
3 | 3 | // [start-readme]
|
4 | 4 | //
|
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. |
7 | 7 | //
|
8 | 8 | // [end-readme]
|
9 | 9 |
|
10 | 10 | import { execSync } from 'child_process'
|
| 11 | +import program from 'commander' |
11 | 12 |
|
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', |
21 | 17 | }
|
| 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 |
22 | 31 |
|
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 | + `) |
29 | 41 | }
|
30 | 42 |
|
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