Skip to content

Commit 2422a22

Browse files
Reset known and unique broken translation files sequentially (#22709)
* fix: de-duplicate and filter non-existent broken files * fix: sequentially reset known broken translation files When a known broken translation is reset, a Git operation performs the underlying reset. If more than one Git operation is running at a time, Git will throw an error: > Another git process seems to be running in this repository [...] This change ensures that we reset each broken translation file sequentially to not run into this error. * Skip reset of a file that does not exist Co-authored-by: Marcelo Jacobus <marcelo.jacobus@gmail.com>
1 parent 22a8cb3 commit 2422a22

2 files changed

Lines changed: 29 additions & 20 deletions

File tree

script/i18n/reset-known-broken-translation-files.js

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@
99

1010
import dotenv from 'dotenv'
1111
import Github from '../helpers/github.js'
12-
import { promisify } from 'util'
13-
import ChildProcess from 'child_process'
12+
import { execSync } from 'child_process'
13+
import uniq from 'lodash/uniq.js'
14+
import { existsSync } from 'fs'
1415

1516
dotenv.config()
1617
const github = Github()
17-
const exec = promisify(ChildProcess.exec)
1818

1919
// Check for required PAT
2020
if (!process.env.GITHUB_TOKEN) {
@@ -37,26 +37,28 @@ async function main() {
3737
// Get the list of broken files from the body text.
3838
const brokenFiles = body.replace(/^[\s\S]*?## List of Broken Translations/m, '').trim()
3939

40-
// Turn it into a simple array of files.
41-
const brokenFilesArray = brokenFiles
42-
.split('\n')
43-
.filter((line) => !line.toLowerCase().startsWith('- [x]'))
44-
.map((line) => line.replace('- [ ] ', '').trim())
45-
46-
// Run the script to revert them.
47-
await Promise.all(
48-
brokenFilesArray.map(async (file) => {
49-
console.log(`resetting ${file}`)
50-
await exec(`script/i18n/reset-translated-file.js --prefer-main ${file}`)
51-
})
40+
// De-duplicate the list of broken files and filter out any that don't exist in the repo.
41+
const brokenFilesArray = uniq(
42+
brokenFiles
43+
.split('\n')
44+
.filter((line) => !line.toLowerCase().startsWith('- [x]'))
45+
.map((line) => line.replace('- [ ] ', '').trim())
46+
.filter((line) => existsSync(line))
5247
)
5348

49+
// Revert each of the broken files.
50+
// This is done sequentially to ensure only one Git operation is running at any given time.
51+
brokenFilesArray.forEach((file) => {
52+
console.log(`Resetting ${file}`)
53+
execSync(`node script/i18n/reset-translated-file.js ${file}`)
54+
})
55+
5456
// Print a message with next steps.
5557
console.log(`
5658
Success!
57-
58-
Verify changes with git status and then run:
59-
59+
60+
Verify changes with git status and then run:
61+
6062
git commit --no-verify -m "Reset broken translated files to English"
6163
`)
6264
}

script/i18n/reset-translated-file.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,18 @@ const resetToEnglishSource = (translationFilePath) => {
3737
translationFilePath.startsWith('translations/'),
3838
'path argument must be in the format `translations/<lang>/path/to/file`'
3939
)
40-
assert(fs.existsSync(translationFilePath), `file does not exist: ${translationFilePath}`)
40+
41+
if (!fs.existsSync(translationFilePath)) {
42+
return
43+
}
4144

4245
const relativePath = translationFilePath.split(path.sep).slice(2).join(path.sep)
4346
const englishFile = path.join(process.cwd(), relativePath)
44-
assert(fs.existsSync(englishFile), `file does not exist: ${englishFile}`)
47+
48+
if (!fs.existsSync(englishFile)) {
49+
fs.unlinkSync(translationFilePath)
50+
return
51+
}
4552

4653
// replace file with English source
4754
const englishContent = fs.readFileSync(englishFile, 'utf8')

0 commit comments

Comments
 (0)