diff --git a/script/i18n/lint-translation-files.js b/script/i18n/lint-translation-files.js old mode 100755 new mode 100644 index 651be7ce384c..efa300443ed3 --- a/script/i18n/lint-translation-files.js +++ b/script/i18n/lint-translation-files.js @@ -2,42 +2,73 @@ // [start-readme] // -// Use this script as part of the Crowdin merge process to output a list of parsing and rendering -// errors in translated files and run script/i18n/reset-translated-file.js on them. +// Use this script as part of the Crowdin merge process to output a list of either parsing +// or rendering errors in translated files and run script/i18n/reset-translated-file.js on them. // // [end-readme] import { execSync } from 'child_process' +import program from 'commander' -const parsingErrorsLog = '~/docs-translation-parsing-error.txt' -const renderErrorsLog = '~/docs-translation-rendering-error.txt' - -// 1. Check for parsing errors and output to file. Note this one must be run FIRST. -console.log('Checking for parsing errors...') -try { - execSync(`TEST_TRANSLATION=true npx jest linting/lint-files > ${parsingErrorsLog}`) -} catch (error) { - console.log('There were new parsing errors!') +// Set up supported linting check types and their corresponding commands. +const CHECK_COMMANDS = { + parsing: 'TEST_TRANSLATION=true npx jest linting/lint-files', + rendering: 'script/i18n/test-render-translation.js', } +const SUPPORTED_CHECK_TYPES = Object.keys(CHECK_COMMANDS) +const CHECK_TYPE_DESCRIPTION = `Specify no more than one of the supported checks: ${SUPPORTED_CHECK_TYPES.join( + ', ' +)}` + +// Initialize a new program for linting translation files, requiring a check type. +program + .description('lint translation files') + .requiredOption('-c, --check ', CHECK_TYPE_DESCRIPTION) + .parse(process.argv) + +// Cache a reference to the client's specified check type. +const specifiedCheckType = program.opts().check -// 2. Check for rendering errors and output to file. Note this one must be run SECOND. -console.log('Checking for rendering errors...') -try { - execSync(`script/i18n/test-render-translation.js > ${renderErrorsLog}`) -} catch (error) { - console.log('There were new rendering errors!') +if (SUPPORTED_CHECK_TYPES.includes(specifiedCheckType)) { + // Lint and reset the files based on a supported check type. + lintAndResetFiles(specifiedCheckType) +} else { + // Otherwise, print an error message. + console.error(` + ${specifiedCheckType} is not a supported check type. + ${CHECK_TYPE_DESCRIPTION} + `) } -// Reset the broken files. -console.log('Resetting broken files...') -execSync( - `cat ${parsingErrorsLog} ${renderErrorsLog} | egrep "^translations/.*/(.+.md|.+.yml)$" | uniq | xargs -L1 script/i18n/reset-translated-file.js --prefer-main` -) - -// Print a message with next steps. -console.log(`Success! - -Verify changes with git status and then run: - -git commit --no-verify -m "Reverted translated files with parsing and rendering errors" -`) +/** + * Lint and reset the files based on the specified check type. + * @param {string} checkType + * @return {undefined} + */ +function lintAndResetFiles(checkType) { + console.log(`Running ${checkType} check...`) + + const log = `~/docs-translation-${checkType}-error.txt` + const cmd = `${CHECK_COMMANDS[checkType]} > ${log}` + + // Lint the files based on the check type and output the errors to a log file. + try { + execSync(cmd[checkType]) + } catch (error) { + console.log(`There were new ${checkType} errors!`) + return + } + + // Reset the files + execSync( + `cat ${log} | egrep "^translations/.*/(.+.md|.+.yml)$" | uniq | xargs -L1 script/i18n/reset-translated-file.js --prefer-main` + ) + + // Print a message with next steps + console.log(`Success! + + Verify changes with git status and then run: + + git commit --no-verify -m "Reverted translated files with ${checkType} errors" + `) +} diff --git a/tests/unit/pages.js b/tests/unit/pages.js index b0a2c545cdad..4a8f0454173f 100644 --- a/tests/unit/pages.js +++ b/tests/unit/pages.js @@ -165,7 +165,11 @@ describe('pages module', () => { .value() const diff = difference(nonEnglishPaths, englishPaths) - const failureMessage = `Unmatched non-English pages ${diff.length}:\n - ${diff.join('\n - ')}` + const failureMessage = ` +Found ${diff.length} non-English pages without a matching English page:\n - ${diff.join('\n - ')} + +Remove them with script/i18n/prune-stale-files.js and commit your changes using "git commit --no-verify". +` expect(diff.length, failureMessage).toBe(0) }) })