Skip to content

repo sync #12071

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Nov 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 61 additions & 30 deletions script/i18n/lint-translation-files.js
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -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 <type>', 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"
`)
}
6 changes: 5 additions & 1 deletion tests/unit/pages.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
})
Expand Down