-
-
Notifications
You must be signed in to change notification settings - Fork 33
/
translate.ts
125 lines (109 loc) · 3.51 KB
/
translate.ts
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
import fs from 'fs/promises'
import path from 'path'
import { glob } from 'glob'
import { context } from '@actions/github'
import { info } from '@actions/core'
import { gptTranslate } from './gpt'
import { generatePRBody, isPR } from './utils'
import {
gitCheckout,
gitCommitPush,
gitCreateBranch,
gitCreatePullRequest,
gitPostComment,
gitSetConfig,
} from './git'
import { createFile, generateOutputFilePaths, isFileExists } from './file'
export const translateByCommand = async (
inputFilePath: string,
outputFilePath: string,
targetLang: string,
) => {
await gitSetConfig()
const branch = isPR() ? await gitCheckout() : await gitCreateBranch()
const inputFilePaths = await glob(inputFilePath)
if (inputFilePaths.length === 0) {
throw new Error('No input files found.')
}
const outputFilePaths = generateOutputFilePaths(
inputFilePaths,
outputFilePath,
)
await createTranslatedFiles(inputFilePaths, outputFilePaths, targetLang)
await gitCommitPush(branch, outputFilePaths)
if (isPR()) {
await gitPostComment('🎉Translation completed!')
return
}
const issueNumber = context.issue.number
const title = '🌐 Add LLM Translations'
const body = generatePRBody(
inputFilePaths,
outputFilePaths,
targetLang,
issueNumber,
)
await gitCreatePullRequest(branch, title, body)
await gitPostComment('🎉Translation PR created!')
}
export const translateByManual = async (
inputFiles: string[],
outputFiles: string[],
languages: string[],
) => {
if (!inputFiles.length) {
info('No input files specified. Skip translation.')
return
}
if (!inputFiles.length || !outputFiles.length || !languages.length) {
throw new Error(
'Error: For push execution, all three parameters (inputFiles, outputFiles and language ) are required',
)
}
if (outputFiles.length !== languages.length) {
throw new Error('Error: outputFiles and language must be same length.')
}
const outputFilePaths: string[][] = outputFiles.map((outputFile) => {
return generateOutputFilePaths(inputFiles, outputFile)
})
// TODO: Dealing with token limit
await Promise.all(
languages.map(async (language, index) => {
return createTranslatedFiles(inputFiles, outputFilePaths[index], language)
}),
)
await gitSetConfig()
const branch = await gitCreateBranch()
const title = '🌐 Add LLM Translations'
await gitCommitPush(branch, outputFilePaths.flat())
const body = generatePRBody(inputFiles, outputFilePaths, languages)
await gitCreatePullRequest(branch, title, body)
}
/*
* Parallel creation of translation files
* inputFilePaths and outputFilePaths must be same length and same order
*/
export const createTranslatedFiles = async (
inputFilePaths: string[],
outputFilePaths: string[],
targetLang: string,
) => {
const processFiles = inputFilePaths.map(async (inputFile, i) => {
const content = await fs.readFile(inputFile, 'utf-8')
const ext = path.extname(inputFile)
const translated = await gptTranslate(content, targetLang, ext)
// Check if the translation is same as the original
if (await isFileExists(outputFilePaths[i])) {
const fileContent = await fs.readFile(outputFilePaths[i], 'utf-8')
if (fileContent === translated) {
info(
'⛔ The result of translation was same as the existed output file.',
)
return
}
}
info(`Create Translated File ${outputFilePaths[i]}`)
await createFile(translated, outputFilePaths[i])
})
await Promise.all(processFiles)
}