1- import decompress from "decompress"
2- import { existsSync , readdirSync , readFileSync , rm , writeFile } from "fs"
1+ import { readdirSync , readFileSync } from "fs"
32import { join } from "path"
43import { getClientFromEnv } from "./client"
54import {
6- ALL_DIRS , ALL_TRANS_LOCALES ,
7- checkMainBranch ,
8- crowdinLangOf ,
9- type Dir ,
10- type ItemSet ,
11- mergeMessage ,
12- RSC_FILE_SUFFIX ,
5+ ALL_DIRS , ALL_TRANS_LOCALES , checkMainBranch , crowdinLangOf , type Dir , type ItemSet , mergeMessage , RSC_FILE_SUFFIX ,
136 transMsg
147} from "./common"
8+ import { clearTempFile , downloadProjectZip } from "./download"
159
16- const TEMP_FILE_NAME = join ( process . cwd ( ) , ".crowdin-temp.zip" )
17- const TEMP_DIR = join ( process . cwd ( ) , ".crowdin-temp" )
18-
19- async function processDir ( dir : Dir ) : Promise < void > {
10+ async function processDir ( tmpDir : string , dir : Dir ) : Promise < void > {
2011 const fileSets : Record < string , Partial < Record < tt4b . Locale , ItemSet > > > = { }
2112 for ( const locale of ALL_TRANS_LOCALES ) {
2213 const crowdinLang = crowdinLangOf ( locale )
23- const dirPath = join ( TEMP_DIR , crowdinLang , dir )
14+ const dirPath = join ( tmpDir , crowdinLang , dir )
2415 const files = readdirSync ( dirPath )
2516 for ( const fileName of files ) {
2617 const json = readFileSync ( join ( dirPath , fileName ) ) . toString ( )
@@ -34,44 +25,18 @@ async function processDir(dir: Dir): Promise<void> {
3425 }
3526}
3627
37- async function downloadProjectZip ( url : string ) : Promise < void > {
38- const res = await fetch ( url )
39- const blob = await res . blob ( )
40- const buffer = Buffer . from ( await blob . arrayBuffer ( ) )
41- await new Promise ( resolve => writeFile ( TEMP_FILE_NAME , buffer , resolve ) )
42- }
43-
44- async function compressProjectZip ( ) : Promise < void > {
45- if ( existsSync ( TEMP_DIR ) ) {
46- await new Promise ( resolve => rm ( TEMP_DIR , { recursive : true } , resolve ) )
47- }
48- await decompress ( TEMP_FILE_NAME , TEMP_DIR )
49- }
50-
51- async function clearTempFile ( ) {
52- await new Promise ( resolve => rm ( TEMP_FILE_NAME , resolve ) )
53- await new Promise ( resolve => rm ( TEMP_DIR , { recursive : true } , resolve ) )
54- }
55-
5628async function main ( ) {
5729 const client = getClientFromEnv ( )
5830 const branch = await checkMainBranch ( client )
5931 const zipUrl = await client . buildProjectTranslation ( branch . id )
6032 console . log ( "Built project translations" )
6133 console . log ( zipUrl )
62- await downloadProjectZip ( zipUrl )
63- console . log ( "Downloaded project zip file" )
64- try {
65- await compressProjectZip ( )
66- console . log ( "Compressed zip file" )
67- for ( const dir of ALL_DIRS ) {
68- await processDir ( dir )
69- console . log ( "Processed dir: " + dir )
70- }
71- } finally {
72- clearTempFile ( )
73- console . log ( "Cleaned temp files" )
34+ const tmpDir = await downloadProjectZip ( zipUrl )
35+
36+ for ( const dir of ALL_DIRS ) {
37+ await processDir ( tmpDir , dir )
38+ console . log ( "Processed dir: " + dir )
7439 }
7540}
7641
77- main ( )
42+ main ( ) . finally ( clearTempFile )
0 commit comments