forked from sheepzh/time-tracker-4-browser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexport-translation.ts
More file actions
77 lines (70 loc) · 2.51 KB
/
export-translation.ts
File metadata and controls
77 lines (70 loc) · 2.51 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
71
72
73
74
75
76
77
import decompress from "decompress"
import { existsSync, readdirSync, readFileSync, rm, writeFile } from "fs"
import { join } from "path"
import { getClientFromEnv } from "./client"
import {
ALL_DIRS, ALL_TRANS_LOCALES,
checkMainBranch,
crowdinLangOf,
type Dir,
type ItemSet,
mergeMessage,
RSC_FILE_SUFFIX,
transMsg
} from "./common"
const TEMP_FILE_NAME = join(process.cwd(), ".crowdin-temp.zip")
const TEMP_DIR = join(process.cwd(), ".crowdin-temp")
async function processDir(dir: Dir): Promise<void> {
const fileSets: Record<string, Partial<Record<timer.Locale, ItemSet>>> = {}
for (const locale of ALL_TRANS_LOCALES) {
const crowdinLang = crowdinLangOf(locale)
const dirPath = join(TEMP_DIR, crowdinLang, dir)
const files = readdirSync(dirPath)
for (const fileName of files) {
const json = readFileSync(join(dirPath, fileName)).toString()
const itemSets = fileSets[fileName] || {}
itemSets[locale] = transMsg(JSON.parse(json))
fileSets[fileName] = itemSets
}
}
for (const [fileName, itemSets] of Object.entries(fileSets)) {
await mergeMessage(dir, fileName.replace('.json', RSC_FILE_SUFFIX), itemSets)
}
}
async function downloadProjectZip(url: string): Promise<void> {
const res = await fetch(url)
const blob = await res.blob()
const buffer = Buffer.from(await blob.arrayBuffer())
await new Promise(resolve => writeFile(TEMP_FILE_NAME, buffer, resolve))
}
async function compressProjectZip(): Promise<void> {
if (existsSync(TEMP_DIR)) {
await new Promise(resolve => rm(TEMP_DIR, { recursive: true }, resolve))
}
await decompress(TEMP_FILE_NAME, TEMP_DIR)
}
async function clearTempFile() {
await new Promise(resolve => rm(TEMP_FILE_NAME, resolve))
await new Promise(resolve => rm(TEMP_DIR, { recursive: true }, resolve))
}
async function main() {
const client = getClientFromEnv()
const branch = await checkMainBranch(client)
const zipUrl = await client.buildProjectTranslation(branch.id)
console.log("Built project translations")
console.log(zipUrl)
await downloadProjectZip(zipUrl)
console.log("Downloaded project zip file")
try {
await compressProjectZip()
console.log("Compressed zip file")
for (const dir of ALL_DIRS) {
await processDir(dir)
console.log("Processed dir: " + dir)
}
} finally {
clearTempFile()
console.log("Cleaned temp files")
}
}
main()