Crowdin remediation process tweaks (#19252)
This commit is contained in:
@@ -20,79 +20,83 @@ const yaml = require('js-yaml')
|
||||
const ghesReleaseNotesSchema = require('../tests/helpers/schemas/release-notes-schema')
|
||||
const revalidator = require('revalidator')
|
||||
|
||||
const fixableFmProps = Object.keys(fm.schema.properties)
|
||||
.filter(property => !fm.schema.properties[property].translatable)
|
||||
.sort()
|
||||
const fixableYmlProps = ['date']
|
||||
main()
|
||||
|
||||
const loadAndValidateContent = async (path, schema) => {
|
||||
let fileContents
|
||||
try {
|
||||
fileContents = await readFileAsync(path, 'utf8')
|
||||
} catch (e) {
|
||||
console.error(e.message)
|
||||
return null
|
||||
async function main () {
|
||||
const fixableFmProps = Object.keys(fm.schema.properties)
|
||||
.filter(property => !fm.schema.properties[property].translatable)
|
||||
.sort()
|
||||
const fixableYmlProps = ['date']
|
||||
|
||||
const loadAndValidateContent = async (path, schema) => {
|
||||
let fileContents
|
||||
try {
|
||||
fileContents = await readFileAsync(path, 'utf8')
|
||||
} catch (e) {
|
||||
console.error(e.message)
|
||||
return null
|
||||
}
|
||||
|
||||
if (path.endsWith('yml')) {
|
||||
let data; let errors = []
|
||||
try {
|
||||
data = yaml.safeLoad(fileContents)
|
||||
} catch {}
|
||||
if (data && schema) {
|
||||
({ errors } = revalidator.validate(data, schema))
|
||||
}
|
||||
return { data, errors, content: null }
|
||||
} else {
|
||||
return fm(fileContents)
|
||||
}
|
||||
}
|
||||
|
||||
if (path.endsWith('yml')) {
|
||||
let data; let errors = []
|
||||
try {
|
||||
data = yaml.safeLoad(fileContents)
|
||||
} catch {}
|
||||
if (data && schema) {
|
||||
({ errors } = revalidator.validate(data, schema))
|
||||
const cmd = 'git -c diff.renameLimit=10000 diff --name-only origin/main | egrep "^translations/.*/(content/.+.md|data/release-notes/.*.yml)$"'
|
||||
const changedFilesRelPaths = execSync(cmd).toString().split('\n')
|
||||
|
||||
for (const relPath of changedFilesRelPaths) {
|
||||
// Skip READMEs
|
||||
if (!relPath || relPath.endsWith('README.md')) continue
|
||||
|
||||
const localisedAbsPath = path.join(__dirname, '..', relPath)
|
||||
// find the corresponding english file by removing the first 2 path segments: /translation/<language code>
|
||||
const engAbsPath = path.join(__dirname, '..', relPath.split(path.sep).slice(2).join(path.sep))
|
||||
|
||||
const localisedResult = await loadAndValidateContent(localisedAbsPath, ghesReleaseNotesSchema)
|
||||
if (!localisedResult) continue
|
||||
const { data, errors, content } = localisedResult
|
||||
|
||||
const fixableProps = relPath.endsWith('yml') ? fixableYmlProps : fixableFmProps
|
||||
|
||||
const fixableErrors = errors.filter(({ property }) => {
|
||||
const prop = property.split('.')
|
||||
return fixableProps.includes(prop[0])
|
||||
})
|
||||
|
||||
if (!data || fixableErrors.length === 0) continue
|
||||
|
||||
const engResult = await loadAndValidateContent(engAbsPath)
|
||||
if (!engResult) continue
|
||||
const { data: engData } = engResult
|
||||
|
||||
console.log(chalk.bold(relPath))
|
||||
|
||||
const newData = data
|
||||
|
||||
fixableErrors.forEach(({ property, message }) => {
|
||||
const correctValue = get(engData, property)
|
||||
console.log(chalk.red(` error message: [${property}] ${message}`))
|
||||
console.log(` fix property [${property}]: ${get(data, property)} -> ${correctValue}`)
|
||||
set(newData, property, correctValue)
|
||||
})
|
||||
|
||||
let toWrite
|
||||
if (content) {
|
||||
toWrite = matter.stringify(content, newData, { lineWidth: 10000, forceQuotes: true })
|
||||
} else {
|
||||
toWrite = yaml.safeDump(newData, { lineWidth: 10000, forceQuotes: true })
|
||||
}
|
||||
return { data, errors, content: null }
|
||||
} else {
|
||||
return fm(fileContents)
|
||||
|
||||
fs.writeFileSync(localisedAbsPath, toWrite)
|
||||
}
|
||||
}
|
||||
|
||||
const cmd = 'git diff --name-only origin/main | egrep "^translations/.*/(content/.+.md|data/release-notes/.*.yml)$"'
|
||||
const changedFilesRelPaths = execSync(cmd).toString().split('\n')
|
||||
|
||||
changedFilesRelPaths.forEach(async (relPath) => {
|
||||
// Skip READMEs
|
||||
if (!relPath || relPath.endsWith('README.md')) return
|
||||
|
||||
const localisedAbsPath = path.join(__dirname, '..', relPath)
|
||||
// find the corresponding english file by removing the first 2 path segments: /translation/<language code>
|
||||
const engAbsPath = path.join(__dirname, '..', relPath.split(path.sep).slice(2).join(path.sep))
|
||||
|
||||
const localisedResult = await loadAndValidateContent(localisedAbsPath, ghesReleaseNotesSchema)
|
||||
if (!localisedResult) return
|
||||
const { data, errors, content } = localisedResult
|
||||
|
||||
const fixableProps = relPath.endsWith('yml') ? fixableYmlProps : fixableFmProps
|
||||
|
||||
const fixableErrors = errors.filter(({ property }) => {
|
||||
const prop = property.split('.')
|
||||
return fixableProps.includes(prop[0])
|
||||
})
|
||||
|
||||
if (!data || fixableErrors.length === 0) return
|
||||
|
||||
const engResult = await loadAndValidateContent(engAbsPath)
|
||||
if (!engResult) return
|
||||
const { data: engData } = engResult
|
||||
|
||||
console.log(chalk.bold(relPath))
|
||||
|
||||
const newData = data
|
||||
|
||||
fixableErrors.forEach(({ property, message }) => {
|
||||
const correctValue = get(engData, property)
|
||||
console.log(chalk.red(` error message: [${property}] ${message}`))
|
||||
console.log(` fix property [${property}]: ${get(data, property)} -> ${correctValue}`)
|
||||
set(newData, property, correctValue)
|
||||
})
|
||||
|
||||
let toWrite
|
||||
if (content) {
|
||||
toWrite = matter.stringify(content, newData, { lineWidth: 10000, forceQuotes: true })
|
||||
} else {
|
||||
toWrite = yaml.safeDump(newData, { lineWidth: 10000, forceQuotes: true })
|
||||
}
|
||||
|
||||
fs.writeFileSync(localisedAbsPath, toWrite)
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user