1
0
mirror of synced 2025-12-21 10:57:10 -05:00
Files
docs/script/fix-translation-errors.js
Kevin Heis 42e785b0a8 Migrate CommonJS to ESM (#20301)
* First run of script

* Get the app running --- ish

* Get NextJS working

* Remove `node:`

* Get more tests passing in unit directory

* Update FailBot test to use nock

* Update test.yml

* Update Dockerfile

* tests/content fixes

* Update page.js

* Update build-changelog.js

* updating tests/routing

* Update orphan-tests.js

* updating tests/rendering

* Update .eslintrc.js

* Update .eslintrc.js

* Install jest/globals

* "linting" tests

* staging update to server.mjs

* Change '.github/allowed-actions.js' to a ESM export

* Lint

* Fixes for the main package.json

* Move Jest to be last in the npm test command so we can pass args

* Just use 'npm run lint' in the npm test command

* update algolia label script

* update openapi script

* update require on openapi

* Update enterprise-algolia-label.js

* forgot JSON.parse

* Update lunr-search-index.js

* Always explicitly include process.cwd() for JSON file reads pathed from project root

* update graphql/update-files.js script

* Update other npm scripts using jest to pass ESM NODE_OPTIONS

* Update check-for-enterprise-issues-by-label.js for ESM

* Update create-enterprise-issue.js for ESM

* Import jest global for browser tests

* Convert 'script/deploy' to ESM

Co-authored-by: Grace Park <gracepark@github.com>
Co-authored-by: James M. Greene <jamesmgreene@github.com>
2021-07-14 13:49:18 -07:00

105 lines
3.4 KiB
JavaScript
Executable File

#!/usr/bin/env node
import { fileURLToPath } from 'url'
import path from 'path'
import { execSync } from 'child_process'
import { get, set } from 'lodash-es'
import fs from 'fs'
import readFileAsync from '../lib/readfile-async.js'
import fm from '../lib/frontmatter.js'
import matter from 'gray-matter'
import chalk from 'chalk'
import yaml from 'js-yaml'
import ghesReleaseNotesSchema from '../tests/helpers/schemas/release-notes-schema.js'
import revalidator from 'revalidator'
const __dirname = path.dirname(fileURLToPath(import.meta.url))
// [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]
main()
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.load(fileContents)
} catch {}
if (data && schema) {
({ errors } = revalidator.validate(data, schema))
}
return { data, errors, content: null }
} else {
return fm(fileContents)
}
}
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.dump(newData, { lineWidth: 10000, forceQuotes: true })
}
fs.writeFileSync(localisedAbsPath, toWrite)
}
}