forked from github/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lint-translation-files.js
executable file
·96 lines (79 loc) · 2.81 KB
/
lint-translation-files.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#!/usr/bin/env node
// [start-readme]
//
// Use this script as part of the translation 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]
// Remove this when removing translations directory B504EDD0
import { execSync } from 'child_process'
import { program } from 'commander'
import fs from 'fs'
// Set up supported linting check types and their corresponding commands.
const CHECK_COMMANDS = {
parsing: 'npm run lint-translation',
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
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}
`)
}
/**
* 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 = `${process.env.HOME}/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)
} catch (error) {
console.log(`There were new ${checkType} errors! Check ${log} for more details.`)
}
// return if file does not exist
if (!fs.existsSync(log)) {
console.log(`${log}: no such file`)
return
}
const filesToReset = fs
.readFileSync(log)
.toString()
.split('\n')
.filter((p) => p.startsWith('translations') && (p.endsWith('.md') || p.endsWith('.yml')))
if (filesToReset.length === 0) {
console.log('There are no violations')
return
}
// We are not passing --prefer-main because we want to remove the file so we
// reset it directly to the English source
filesToReset.forEach((file) => {
execSync(`script/i18n/reset-translated-file.js ${file} --reason="${checkType} error"`, {
stdio: 'inherit',
})
})
// 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"
`)
}