-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathclient.ts
More file actions
305 lines (272 loc) · 9.57 KB
/
client.ts
File metadata and controls
305 lines (272 loc) · 9.57 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
import Crowdin, {
type Credentials,
type Pagination,
type PatchRequest,
type ResponseList,
type SourceFilesModel,
type SourceStringsModel,
type StringTranslationsModel,
type UploadStorageModel,
} from '@crowdin/crowdin-api-client'
import { ALL_CROWDIN_LANGUAGES, type CrowdinLanguage, type Dir, type ItemSet } from './common'
const PROJECT_ID = 516822
const MAIN_BRANCH_NAME = 'main'
/**
* The iterator of response
*/
class PaginationIterator<T> {
private offset = 0
private limit = 25
private isEnd = false
private buf: T[] = []
private cursor = 0
private query: (pagination: Pagination) => Promise<ResponseList<T>>
constructor(query: (pagination: Pagination) => Promise<ResponseList<T>>) {
this.query = query
}
reset(): void {
this.offset = 0
this.isEnd = false
this.buf = []
this.cursor = 0
}
async findFirst(predicate: (ele: T) => boolean): Promise<T | undefined> {
while (true) {
const data = await this.next()
if (!data) {
break
}
if (data && predicate(data)) {
return data
}
}
return undefined
}
async findAll(predicate?: ((ele: T) => boolean)): Promise<T[]> {
const result: T[] = []
while (true) {
const data = await this.next()
if (!data) {
break
}
if (predicate ? predicate(data) : true) {
result.push(data)
}
}
return result
}
async next(): Promise<T | undefined> {
if (this.isEnd) {
return undefined
}
if (this.cursor >= this.buf.length) {
await this.processBuf()
}
if (this.isEnd) {
return undefined
}
return this.buf[this.cursor++]
}
private async processBuf() {
const pagination: Pagination = { offset: this.offset, limit: this.limit }
const list = await this.query(pagination)
const data = list?.data
if (!data?.length) {
this.isEnd = true
} else {
this.buf = data.map(obj => obj.data)
this.cursor = 0
this.offset += this.buf.length
}
}
}
/**
* Key of crowdin file/directory
*/
export type NameKey = {
name: Dir
branchId: number
}
export type TranslationKey = {
stringId: number
lang: CrowdinLanguage
}
/**
* The wrapper of client with auth
*/
export class CrowdinClient {
crowdin: Crowdin
constructor(token: string) {
const credentials: Credentials = {
token: token
}
this.crowdin = new Crowdin(credentials)
console.info("Initialized client successfully")
}
async createStorage(fileName: string, content: any): Promise<UploadStorageModel.Storage> {
const response = await this.crowdin.uploadStorageApi.addStorage(fileName, content)
return response.data
}
/**
* Get the main branch
*
* @returns main branch or undefined
*/
async getMainBranch(): Promise<SourceFilesModel.Branch | undefined> {
return new PaginationIterator(
pagination => this.crowdin.sourceFilesApi.listProjectBranches(PROJECT_ID, { ...pagination })
).findFirst(e => e.name === MAIN_BRANCH_NAME)
}
/**
* Create the main branch
*/
async createMainBranch(): Promise<SourceFilesModel.Branch> {
const request: SourceFilesModel.CreateBranchRequest = {
name: MAIN_BRANCH_NAME
}
const res = await this.crowdin.sourceFilesApi.createBranch(PROJECT_ID, request)
return res.data
}
async getOrCreateMainBranch(): Promise<SourceFilesModel.Branch> {
let branch = await this.getMainBranch()
if (!branch) {
branch = await this.createMainBranch()
}
console.info("getOrCreateMainBranch: " + JSON.stringify(branch))
return branch
}
async createFile(
directoryId: number,
storage: UploadStorageModel.Storage,
fileName: string
): Promise<SourceFilesModel.File> {
const request: SourceFilesModel.CreateFileRequest = {
name: fileName,
storageId: storage.id,
directoryId,
type: 'json',
}
const response = await this.crowdin.sourceFilesApi.createFile(PROJECT_ID, request)
return response.data
}
async restoreFile(storage: UploadStorageModel.Storage, existFile: SourceFilesModel.File): Promise<SourceFilesModel.File> {
const response = await this.crowdin.sourceFilesApi.updateOrRestoreFile(PROJECT_ID, existFile.id, { storageId: storage.id })
return response.data
}
getFileByName(param: NameKey): Promise<SourceFilesModel.File | undefined> {
return new PaginationIterator(
p => this.crowdin.sourceFilesApi.listProjectFiles(PROJECT_ID, { ...p, branchId: param.branchId })
).findFirst(t => t.name === param.name)
}
getDirByName(param: NameKey): Promise<SourceFilesModel.Directory | undefined> {
return new PaginationIterator(
p => this.crowdin.sourceFilesApi.listProjectDirectories(PROJECT_ID, { ...p, branchId: param.branchId })
).findFirst(d => d.name === param.name)
}
async createDirectory(param: NameKey): Promise<SourceFilesModel.Directory> {
const res = await this.crowdin.sourceFilesApi.createDirectory(PROJECT_ID, {
name: param.name,
branchId: param.branchId,
})
return res.data
}
listFilesByDirectory(directoryId: number) {
return new PaginationIterator(
p => this.crowdin.sourceFilesApi.listProjectFiles(PROJECT_ID, { ...p, directoryId: directoryId })
).findAll()
}
listStringsByFile(fileId: number): Promise<SourceStringsModel.String[]> {
return new PaginationIterator(
p => this.crowdin.sourceStringsApi.listProjectStrings(PROJECT_ID, { ...p, fileId: fileId })
).findAll()
}
async batchCreateString(
fileId: number,
content: ItemSet,
): Promise<void> {
for (const [path, value] of Object.entries(content)) {
const request: SourceStringsModel.CreateStringRequest = {
fileId,
text: value,
identifier: path,
}
console.log(`Try to create new string: ${JSON.stringify(request)}`)
await this.crowdin.sourceStringsApi.addString(PROJECT_ID, request)
}
}
async batchUpdateIfNecessary(
content: ItemSet,
existStringsKeyMap: { [path: string]: SourceStringsModel.String }
): Promise<void> {
console.log("=========start to update strings========")
console.log("Content length: " + Object.keys(content).length)
for (const [path, value] of Object.entries(content)) {
const string = existStringsKeyMap[path]
const patch: PatchRequest[] = []
string?.text !== value && patch.push({
op: 'replace',
path: '/text',
value: value
})
if (!patch.length) {
continue
}
console.log('Try to edit string: ' + string.identifier)
await this.crowdin.sourceStringsApi.editString(PROJECT_ID, string.id, patch)
}
console.log("=========end to update strings========")
}
async batchDeleteString(stringIds: number[]): Promise<void> {
console.log("=========start to delete strings========")
for (const stringId of stringIds) {
await this.crowdin.sourceStringsApi.deleteString(PROJECT_ID, stringId)
console.log("Delete string: id=" + stringId)
}
console.log("=========end to delete strings========")
}
async listTranslationByStringAndLang(transKey: TranslationKey): Promise<StringTranslationsModel.StringTranslation[]> {
const { stringId, lang } = transKey
return await new PaginationIterator(
p => this.crowdin.stringTranslationsApi.listStringTranslations(PROJECT_ID, stringId, lang, { ...p })
).findAll()
}
async deleteTranslation(translationId: number): Promise<void> {
await this.crowdin.stringTranslationsApi.deleteTranslation(PROJECT_ID, translationId)
}
async createTranslation(transKey: TranslationKey, text: string) {
const { stringId, lang } = transKey
const request: StringTranslationsModel.AddStringTranslationRequest = {
stringId,
languageId: lang,
text
}
await this.crowdin.stringTranslationsApi.addTranslation(PROJECT_ID, request)
}
async buildProjectTranslation(branchId: number) {
const buildRes = await this.crowdin.translationsApi.buildProject(PROJECT_ID, {
branchId,
targetLanguageIds: [...ALL_CROWDIN_LANGUAGES],
skipUntranslatedStrings: true,
})
const buildId = buildRes?.data?.id
while (true) {
// Wait finished
const res = await this.crowdin.translationsApi.downloadTranslations(PROJECT_ID, buildId)
const url = res?.data?.url
if (url) return url
}
}
}
/**
* Get the client from environment variable [TIMER_CROWDIN_AUTH]
*
* @returns client
*/
export function getClientFromEnv(): CrowdinClient {
const envVar = process.env?.TIMER_CROWDIN_AUTH
if (!envVar) {
console.error("Failed to get the variable named [TIMER_CROWDIN_AUTH]")
process.exit(1)
}
return new CrowdinClient(envVar)
}