forked from github/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reset-translated-file.js
executable file
·75 lines (66 loc) · 2.84 KB
/
reset-translated-file.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
#!/usr/bin/env node
// [start-readme]
//
// This is a convenience script for replacing the contents of translated
// files with the English content from their corresponding source file.
//
// It's intended to be a workaround to temporarily bypass Crowdin parser bugs
// while we wait for translators to fix them.
//
// Usage:
// script/reset-translated-file.js <filename>
//
// Examples:
//
// reset a single translated file using a relative path:
// $ script/reset-translated-file.js translations/es-XL/content/actions/index.md
//
// reset a single translated file using a full path:
// $ script/reset-translated-file.js /Users/z/git/github/docs-internal/translations/es-XL/content/actions/index.md
//
// reset all language variants of a single English file (using a relative path):
// $ script/reset-translated-file.js content/actions/index.md
// $ script/reset-translated-file.js data/ui.yml
//
// reset all language variants of a single English file (using a full path):
// $ script/reset-translated-file.js /Users/z/git/github/docs-internal/content/desktop/index.md
// $ script/reset-translated-file.js /Users/z/git/github/docs-internal/data/ui.yml
//
// [end-readme]
const program = require('commander')
const { execSync } = require('child_process')
const assert = require('assert')
const fs = require('fs')
const path = require('path')
const languages = require('../lib/languages')
program
.description('reset translated files')
.option('-m, --use-main', 'Reset file to the translated file from `main` branch instead of from English source.')
.parse(process.argv)
const [pathArg] = program.args
assert(pathArg, 'first arg must be a target filename')
let languageCode
// Is the arg a fully-qualified path?
let relativePath = fs.existsSync(pathArg)
? path.relative(process.cwd(), pathArg)
: pathArg
if (program.useMain) {
execSync(`git checkout main -- ${relativePath}`)
console.log('reverted to file from main branch: %s', relativePath)
} else {
// extract relative path and language code if pathArg is in the format `translations/<lang>/path/to/file`
if (relativePath.startsWith('translations/')) {
languageCode = Object.values(languages).find(language => relativePath.startsWith(language.dir) && language.code !== 'en').code
relativePath = relativePath.split(path.sep).slice(2).join(path.sep)
}
const englishFile = path.join(process.cwd(), relativePath)
assert(fs.existsSync(englishFile), `file does not exist: ${englishFile}`)
const englishContent = fs.readFileSync(englishFile, 'utf8')
Object.values(languages).forEach(({ code }) => {
if (code === 'en') return
if (languageCode && languageCode !== code) return
const translatedFile = path.join(process.cwd(), languages[code].dir, relativePath)
fs.writeFileSync(translatedFile, englishContent)
console.log('reverted to English: %s', path.relative(process.cwd(), translatedFile))
})
}