Repository navigation
Expand file tree
/
Copy pathexplanations.mjs
More file actions
70 lines (63 loc) · 2.5 KB
/
Copy pathexplanations.mjs
File metadata and controls
70 lines (63 loc) · 2.5 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
63
64
65
66
67
68
69
70
#!/usr/bin/env node
import { readFileSync, writeFileSync } from 'node:fs'
import { homedir } from 'node:os'
const API = 'https://hosted.weblate.org/api/'
const FILE = new URL('./explanations.json', import.meta.url)
const apply = process.argv.includes('--apply')
const pull = process.argv.includes('--pull')
const token =
process.env.WLC_TOKEN ??
readFileSync(`${homedir()}/.config/weblate`, 'utf8').match(
/^https:\/\/hosted\.weblate\.org\/api\/\s*=\s*(\S+)/m
)?.[1]
if (!token) throw new Error('no Weblate token in $WLC_TOKEN or ~/.config/weblate')
async function api(url, init = {}) {
const res = await fetch(url, {
...init,
headers: { Authorization: `Token ${token}`, 'Content-Type': 'application/json' },
})
if (!res.ok) throw new Error(`${init.method ?? 'GET'} ${url}: ${res.status} ${await res.text()}`)
return res.json()
}
async function units(path) {
const out = []
for (let url = `${API}${path}`; url; ) {
const page = await api(url)
out.push(...page.results)
url = page.next
}
return out
}
const byKey = (a, b) => (a[0] < b[0] ? -1 : 1)
const file = JSON.parse(readFileSync(FILE, 'utf8'))
const explained = await units(
`units/?page_size=1000&q=${encodeURIComponent('project:compcon AND language:en AND has:explanation')}`
)
for (const unit of explained) {
const component = unit.translation.split('/').at(-3)
if (file[component]?.[unit.context] !== undefined) continue
console.log(`${component}: ${pull ? 'pulled' : 'only on Weblate'} ${unit.context}`)
if (pull) (file[component] ??= {})[unit.context] = unit.explanation
}
if (pull) {
const sorted = Object.entries(file)
.sort(byKey)
.map(([component, keys]) => [component, Object.fromEntries(Object.entries(keys).sort(byKey))])
writeFileSync(FILE, JSON.stringify(Object.fromEntries(sorted), null, 4) + '\n')
}
let changes = 0
for (const [component, wanted] of Object.entries(file)) {
const live = new Map(
(await units(`translations/compcon/${component}/en/units/?page_size=1000`)).map(unit => [unit.context, unit])
)
for (const [key, explanation] of Object.entries(wanted)) {
const unit = live.get(key)
if (!unit) console.log(`${component}: no string for ${key}`)
else if (unit.explanation !== explanation) {
changes++
console.log(`${component}: ${apply ? 'set' : 'would set'} ${key}`)
if (apply) await api(`${API}units/${unit.id}/`, { method: 'PATCH', body: JSON.stringify({ explanation }) })
}
}
}
console.log(`${changes} ${apply ? 'set' : 'to set (dry run, pass --apply)'}`)