|
13 | 13 | //
|
14 | 14 | // Examples:
|
15 | 15 | //
|
16 |
| -// reset a single translated file using a relative path: |
17 | 16 | // $ script/reset-translated-file.js translations/es-XL/content/actions/index.md
|
18 | 17 | //
|
19 |
| -// reset a single translated file using a full path: |
20 |
| -// $ script/reset-translated-file.js /Users/z/git/github/docs-internal/translations/es-XL/content/actions/index.md |
21 |
| -// |
22 |
| -// reset all language variants of a single English file (using a relative path): |
23 |
| -// $ script/reset-translated-file.js content/actions/index.md |
24 |
| -// $ script/reset-translated-file.js data/ui.yml |
25 |
| -// |
26 |
| -// reset all language variants of a single English file (using a full path): |
27 |
| -// $ script/reset-translated-file.js /Users/z/git/github/docs-internal/content/desktop/index.md |
28 |
| -// $ script/reset-translated-file.js /Users/z/git/github/docs-internal/data/ui.yml |
29 |
| -// |
30 | 18 | // [end-readme]
|
31 | 19 |
|
32 | 20 | const program = require('commander')
|
33 | 21 | const { execSync } = require('child_process')
|
34 | 22 | const assert = require('assert')
|
35 | 23 | const fs = require('fs')
|
36 | 24 | const path = require('path')
|
37 |
| -const languages = require('../lib/languages') |
| 25 | +const chalk = require('chalk') |
38 | 26 |
|
39 | 27 | program
|
40 | 28 | .description('reset translated files')
|
41 |
| - .option('-m, --use-main', 'Reset file to the translated file from `main` branch instead of from English source.') |
| 29 | + .option('-m, --prefer-main', 'Reset file to the translated file, try using the file from `main` branch first, if not found (usually due to renaming), fall back to English source.') |
42 | 30 | .parse(process.argv)
|
43 | 31 |
|
| 32 | +const resetToEnglishSource = (translationFilePath) => { |
| 33 | + assert(translationFilePath.startsWith('translations/'), 'path argument must be in the format `translations/<lang>/path/to/file`') |
| 34 | + assert(fs.existsSync(translationFilePath), `file does not exist: ${translationFilePath}`) |
| 35 | + |
| 36 | + const relativePath = translationFilePath.split(path.sep).slice(2).join(path.sep) |
| 37 | + const englishFile = path.join(process.cwd(), relativePath) |
| 38 | + assert(fs.existsSync(englishFile), `file does not exist: ${englishFile}`) |
| 39 | + |
| 40 | + // replace file with English source |
| 41 | + const englishContent = fs.readFileSync(englishFile, 'utf8') |
| 42 | + fs.writeFileSync(translationFilePath, englishContent) |
| 43 | + console.log('-> reverted to English: %s', path.relative(process.cwd(), translationFilePath)) |
| 44 | +} |
| 45 | + |
44 | 46 | const [pathArg] = program.args
|
45 | 47 | assert(pathArg, 'first arg must be a target filename')
|
46 |
| -let languageCode |
47 | 48 |
|
48 | 49 | // Is the arg a fully-qualified path?
|
49 |
| -let relativePath = fs.existsSync(pathArg) |
| 50 | +const relativePath = fs.existsSync(pathArg) |
50 | 51 | ? path.relative(process.cwd(), pathArg)
|
51 | 52 | : pathArg
|
52 | 53 |
|
53 |
| -if (program.useMain) { |
54 |
| - execSync(`git checkout main -- ${relativePath}`) |
55 |
| - console.log('reverted to file from main branch: %s', relativePath) |
56 |
| -} else { |
57 |
| - // extract relative path and language code if pathArg is in the format `translations/<lang>/path/to/file` |
58 |
| - if (relativePath.startsWith('translations/')) { |
59 |
| - languageCode = Object.values(languages).find(language => relativePath.startsWith(language.dir) && language.code !== 'en').code |
60 |
| - relativePath = relativePath.split(path.sep).slice(2).join(path.sep) |
| 54 | +if (program.preferMain) { |
| 55 | + try { |
| 56 | + execSync(`git checkout main -- ${relativePath}`, { stdio: 'pipe' }) |
| 57 | + console.log('-> reverted to file from main branch: %s', relativePath) |
| 58 | + } catch (e) { |
| 59 | + if (e.message.includes('pathspec')) { |
| 60 | + console.warn(chalk.red(`cannot find ${relativePath} in main branch (likely because it was renamed); falling back to English source file.`)) |
| 61 | + resetToEnglishSource(relativePath) |
| 62 | + } else { |
| 63 | + console.warn(e.message) |
| 64 | + } |
61 | 65 | }
|
62 |
| - |
63 |
| - const englishFile = path.join(process.cwd(), relativePath) |
64 |
| - assert(fs.existsSync(englishFile), `file does not exist: ${englishFile}`) |
65 |
| - const englishContent = fs.readFileSync(englishFile, 'utf8') |
66 |
| - |
67 |
| - Object.values(languages).forEach(({ code }) => { |
68 |
| - if (code === 'en') return |
69 |
| - if (languageCode && languageCode !== code) return |
70 |
| - |
71 |
| - const translatedFile = path.join(process.cwd(), languages[code].dir, relativePath) |
72 |
| - fs.writeFileSync(translatedFile, englishContent) |
73 |
| - console.log('reverted to English: %s', path.relative(process.cwd(), translatedFile)) |
74 |
| - }) |
| 66 | +} else { |
| 67 | + resetToEnglishSource(relativePath) |
75 | 68 | }
|
0 commit comments