|
1 | | -import { SourceFilesModel } from "@crowdin/crowdin-api-client" |
2 | | -import { CrowdinClient, getClientFromEnv } from "./client" |
3 | | -import { ALL_DIRS, ALL_TRANS_LOCALES, RSC_FILE_SUFFIX, checkMainBranch, crowdinLangOf, mergeMessage } from "./common" |
| 1 | +import decompress from "decompress" |
| 2 | +import { existsSync, readdirSync, readFileSync, rm, writeFile } from "fs" |
| 3 | +import { join } from "path" |
| 4 | +import { getClientFromEnv } from "./client" |
| 5 | +import { ALL_DIRS, ALL_TRANS_LOCALES, checkMainBranch, crowdinLangOf, Dir, ItemSet, mergeMessage, RSC_FILE_SUFFIX, transMsg } from "./common" |
4 | 6 |
|
5 | | -async function processFile(client: CrowdinClient, file: SourceFilesModel.File, dir: Dir): Promise<void> { |
6 | | - const itemSets: Partial<Record<timer.Locale, ItemSet>> = {} |
| 7 | +const TEMP_FILE_NAME = join(process.cwd(), ".crowdin-temp.zip") |
| 8 | +const TEMP_DIR = join(process.cwd(), ".crowdin-temp") |
| 9 | + |
| 10 | +async function processDir(dir: Dir): Promise<void> { |
| 11 | + const fileSets: Record<string, Partial<Record<timer.Locale, ItemSet>>> = {} |
7 | 12 | for (const locale of ALL_TRANS_LOCALES) { |
8 | | - const lang = crowdinLangOf(locale) |
9 | | - const items: ItemSet = await client.downloadTranslations(file.id, lang) |
10 | | - items && Object.keys(items).length && (itemSets[locale] = items) |
| 13 | + const crowdinLang = crowdinLangOf(locale) |
| 14 | + const dirPath = join(TEMP_DIR, crowdinLang, dir) |
| 15 | + const files = readdirSync(dirPath) |
| 16 | + for (const fileName of files) { |
| 17 | + const json = readFileSync(join(dirPath, fileName)).toString() |
| 18 | + const itemSets = fileSets[fileName] || {} |
| 19 | + itemSets[locale] = transMsg(JSON.parse(json)) |
| 20 | + fileSets[fileName] = itemSets |
| 21 | + } |
| 22 | + } |
| 23 | + for (const [fileName, itemSets] of Object.entries(fileSets)) { |
| 24 | + await mergeMessage(dir, fileName.replace('.json', RSC_FILE_SUFFIX), itemSets) |
11 | 25 | } |
12 | | - await mergeMessage(dir, file.name.replace('.json', RSC_FILE_SUFFIX), itemSets) |
13 | 26 | } |
14 | 27 |
|
15 | | -async function processDir(client: CrowdinClient, branch: SourceFilesModel.Branch, dir: Dir): Promise<void> { |
16 | | - const directory = await client.getDirByName({ name: dir, branchId: branch.id }) |
17 | | - const files = await client.listFilesByDirectory(directory.id) |
18 | | - for (const file of files) { |
19 | | - processFile(client, file, dir) |
| 28 | +async function downloadProjectZip(url: string): Promise<void> { |
| 29 | + const res = await fetch(url) |
| 30 | + const blob = await res.blob() |
| 31 | + const buffer = Buffer.from(await blob.arrayBuffer()) |
| 32 | + await new Promise(resolve => writeFile(TEMP_FILE_NAME, buffer, resolve)) |
| 33 | +} |
| 34 | + |
| 35 | +async function compressProjectZip(): Promise<void> { |
| 36 | + if (existsSync(TEMP_DIR)) { |
| 37 | + await new Promise(resolve => rm(TEMP_DIR, { recursive: true }, resolve)) |
20 | 38 | } |
| 39 | + await decompress(TEMP_FILE_NAME, TEMP_DIR) |
| 40 | +} |
| 41 | + |
| 42 | +async function clearTempFile() { |
| 43 | + await new Promise(resolve => rm(TEMP_FILE_NAME, resolve)) |
| 44 | + await new Promise(resolve => rm(TEMP_DIR, { recursive: true }, resolve)) |
21 | 45 | } |
22 | 46 |
|
23 | 47 | async function main() { |
24 | 48 | const client = getClientFromEnv() |
25 | 49 | const branch = await checkMainBranch(client) |
26 | | - for (const dir of ALL_DIRS) { |
27 | | - await processDir(client, branch, dir) |
| 50 | + const zipUrl = await client.buildProjectTranslation(branch.id) |
| 51 | + console.log("Built project translations") |
| 52 | + console.log(zipUrl) |
| 53 | + await downloadProjectZip(zipUrl) |
| 54 | + console.log("Downloaded project zip file") |
| 55 | + try { |
| 56 | + await compressProjectZip() |
| 57 | + console.log("Compressed zip file") |
| 58 | + for (const dir of ALL_DIRS) { |
| 59 | + await processDir(dir) |
| 60 | + console.log("Processed dir: " + dir) |
| 61 | + } |
| 62 | + } finally { |
| 63 | + clearTempFile() |
| 64 | + console.log("Cleaned temp files") |
28 | 65 | } |
29 | 66 | } |
30 | 67 |
|
|
0 commit comments