forked from github/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Translation tools to make our lives easier (github#17712)
* create script to fix easy-to-fix frontmatter errors in translation * improve reset-translated-file.js: allow reverting to the same file from `main` branch * fix release-notes as well * also lints liquid in frontmatter
- Loading branch information
1 parent
744e535
commit 25ff6b9
Showing
4 changed files
with
135 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
const { execSync } = require('child_process') | ||
const { get, set } = require('lodash') | ||
const fs = require('fs') | ||
const path = require('path') | ||
const fm = require('../lib/frontmatter') | ||
const matter = require('gray-matter') | ||
const chalk = require('chalk') | ||
const yaml = require('js-yaml') | ||
const ghesReleaseNotesSchema = require('../lib/release-notes-schema') | ||
const revalidator = require('revalidator') | ||
|
||
const fixableFmProps = ['type', 'changelog', 'mapTopic', 'hidden', 'layout', 'defaultPlatform', 'showMiniToc', 'allowTitleToDifferFromFilename', 'interactive', 'beta_product'] | ||
const fixableYmlProps = ['date'] | ||
|
||
// [start-readme] | ||
// | ||
// Run this script to fix known frontmatter errors by copying values from english file | ||
// Currently only fixing errors in: 'type', 'changelog' | ||
// Please double check the changes created by this script before committing. | ||
// | ||
// [end-readme] | ||
|
||
const loadAndValidateContent = async (path, schema) => { | ||
let fileContents | ||
try { | ||
fileContents = await fs.promises.readFile(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) | ||
} | ||
} | ||
|
||
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) => { | ||
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.red('fixing errors in ') + chalk.bold(relPath)) | ||
|
||
const newData = data | ||
|
||
fixableErrors.forEach(({ property }) => { | ||
const correctValue = get(engData, property) | ||
console.log(` [${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) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters