-
-
Notifications
You must be signed in to change notification settings - Fork 424
Expand file tree
/
Copy pathupdate-en-meta-json.ts
More file actions
62 lines (52 loc) · 1.94 KB
/
update-en-meta-json.ts
File metadata and controls
62 lines (52 loc) · 1.94 KB
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
import { writeFileSync } from 'node:fs'
import { resolve } from 'node:path'
import dot from 'dot-object'
import {
checkTranslationChanges,
createUpdatedEnMetaJson,
getCurrentCommitHash,
getNewEnJson,
getOldEnMetaJson,
} from './git-utils.ts'
import type { EnJson, EnMetaJson, MetaEntry } from './types.d.ts'
const enJsonPath = resolve('i18n/locales/en.json')
const enMetaJsonPath = resolve('i18n/locales/en.meta.json')
/**
* Update a metadata JSON file for English translations.
*/
export function updateEnMetaJson() {
const newEnJson = getNewEnJson(enJsonPath)
const oldEnMetaJson = getOldEnMetaJson(enMetaJsonPath)
const currentCommitHash = getCurrentCommitHash()
const enMetaJson = currentCommitHash
? makeEnMetaJson(oldEnMetaJson, newEnJson, currentCommitHash)
: ({} as EnMetaJson)
const hasChanges = checkTranslationChanges(oldEnMetaJson, enMetaJson)
if (!hasChanges) {
console.info('ℹ️ No translation changes – en.meta.json left untouched')
return
}
const finalMeta = createUpdatedEnMetaJson(currentCommitHash, enMetaJson)
writeFileSync(enMetaJsonPath, JSON.stringify(finalMeta, null, 2) + '\n', 'utf-8')
console.log(`✅ Updated en.meta.json – last_updated_commit: ${currentCommitHash}`)
}
export function makeEnMetaJson(
oldMetaEnJson: EnMetaJson,
newEnJson: EnJson,
latestCommitHash: string,
): EnMetaJson {
const newFlat = dot.dot(newEnJson) as Record<string, string>
const oldMetaFlat = dot.dot(oldMetaEnJson) as Record<string, string>
const metaFlat: Record<string, MetaEntry> = {}
for (const key in newFlat) {
const newText = newFlat[key]
const lastSeenText = oldMetaFlat[`${key}.text`]
const lastCommit = oldMetaFlat[`${key}.commit`]
if (newText === lastSeenText) {
metaFlat[key] = { text: newText, commit: lastCommit ?? latestCommitHash }
} else {
metaFlat[key] = { text: newText, commit: latestCommitHash }
}
}
return dot.object(metaFlat) as EnMetaJson
}