cc-translate 0.5.1 → 0.5.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.d.ts.map +1 -1
- package/index.js +3 -6
- package/lib/cache/index.js +1 -1
- package/lib/utils.d.ts +1 -0
- package/lib/utils.d.ts.map +1 -1
- package/lib/utils.js +23 -0
- package/package.json +1 -1
- package/src/index.ts +3 -9
- package/src/lib/utils.ts +23 -0
- package/test.mjs +13 -12
package/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["src/index.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAEhC,OAAO,EACL,uBAAuB,EACvB,KAAK,EACL,aAAa,EACb,mBAAmB,EACnB,gBAAgB,EAChB,cAAc,EACd,sBAAsB,EACtB,mBAAmB,EACpB,MAAM,SAAS,CAAC;AAgBjB,OAAO,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAC;AAClD,OAAO,EAAE,aAAa,EAAE,gBAAgB,EAAE,MAAM,sBAAsB,CAAC;AAMvE,qBAAa,gBAAgB;IAC3B,uBAAuB;IACvB,OAAO,CAAC,UAAU,CAAS;IAC3B,MAAM;IACN,eAAe,EAAE,MAAM,CAAC;IACxB,eAAe,EAAE,MAAM,CAAC;IACxB,iBAAiB;IACjB,eAAe,EAAE,mBAAmB,CAAC;IACrC,gBAAgB,EAAE,MAAM,GAAG,SAAS,CAAC;IACrC,SAAS,EAAE,mBAAmB,EAAE,CAAC;IACjC,MAAM,EAAE,MAAM,GAAG,IAAI,CAAQ;IAC7B,2BAA2B;IAC3B,YAAY,EAAE,aAAa,CAAC;IAC5B,QAAQ,EAAE,MAAM,EAAE,CAAC;gBAEP,MAAM,EAAE,uBAAuB;IAY3C,IAAI,gBAAgB,kCAOnB;IAED,IAAI,UAAU,WAEb;IAED,cAAc,CAAC,IAAI,EAAE,m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,CA6B3B;IAEF;;;OAGG;IACH,kBAAkB,WAAkB,mBAAmB,mBAsCrD;CACH"}
|
package/index.js
CHANGED
|
@@ -12,7 +12,7 @@ import fs from "fs";
|
|
|
12
12
|
import colors from "ansi-colors";
|
|
13
13
|
import { OpenAI } from "openai";
|
|
14
14
|
import cliProgress from "cli-progress";
|
|
15
|
-
import {
|
|
15
|
+
import { dequeue, getRandomNumber, notExistsToCreateFile, readFileOfDirSync, readJsonFileSync, } from "./lib/utils.js";
|
|
16
16
|
import { getCacheFileSync, registerLanguageCacheFile, translateJSONDiffToJson, } from "./lib/cache/index.js";
|
|
17
17
|
import { logErrorToFile } from "./lib/log/index.js";
|
|
18
18
|
import { SUPPORT_LANGUAGE_MAP } from "./lib/support.js";
|
|
@@ -70,10 +70,7 @@ export class CwalletTranslate {
|
|
|
70
70
|
}));
|
|
71
71
|
}
|
|
72
72
|
}
|
|
73
|
-
|
|
74
|
-
for (const chunk of promises) {
|
|
75
|
-
yield Promise.all(chunk.map((fn) => fn()));
|
|
76
|
-
}
|
|
73
|
+
yield dequeue(arr);
|
|
77
74
|
multiBar.stop();
|
|
78
75
|
console.log("🚀 翻译完毕");
|
|
79
76
|
});
|
|
@@ -174,7 +171,7 @@ export class CwalletTranslate {
|
|
|
174
171
|
value: (_c = (_b = chatCompletion === null || chatCompletion === void 0 ? void 0 : chatCompletion.choices[0]) === null || _b === void 0 ? void 0 : _b.message.content) !== null && _c !== void 0 ? _c : value,
|
|
175
172
|
index,
|
|
176
173
|
});
|
|
177
|
-
}), getRandomNumber(
|
|
174
|
+
}), getRandomNumber(120, 130));
|
|
178
175
|
}
|
|
179
176
|
catch (error) {
|
|
180
177
|
logErrorToFile({ error: error, key, fileName, language });
|
package/lib/cache/index.js
CHANGED
|
@@ -81,7 +81,7 @@ export const generateCache = (params) => __awaiter(void 0, void 0, void 0, funct
|
|
|
81
81
|
languages.forEach((language) => {
|
|
82
82
|
// 获取当前语言的文件数据
|
|
83
83
|
const currentFileData = readAllFilesOfFolder(path.join(sourceFolderPath, language));
|
|
84
|
-
currentFileData.forEach(({ data, fileName
|
|
84
|
+
currentFileData.forEach(({ data, fileName }) => {
|
|
85
85
|
const translated = {};
|
|
86
86
|
// 遍历文件中的已经翻译的内容
|
|
87
87
|
Object.entries(data).forEach(([key, value]) => {
|
package/lib/utils.d.ts
CHANGED
|
@@ -23,4 +23,5 @@ export declare const isDirectoryPath: (path: string) => boolean;
|
|
|
23
23
|
export declare const readFileOfDirSync: (dirPath: string) => string[];
|
|
24
24
|
export declare function chunkArray<T extends object>(array: T[], chunkSize: number): T[][];
|
|
25
25
|
export declare function intersection<T>(arr1: T[], arr2: T[]): T[];
|
|
26
|
+
export declare const dequeue: (queue: (() => Promise<void>)[]) => Promise<void>;
|
|
26
27
|
//# sourceMappingURL=utils.d.ts.map
|
package/lib/utils.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/lib/utils.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,qBAAqB,EAAE,MAAM,UAAU,CAAC;
|
|
1
|
+
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/lib/utils.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,qBAAqB,EAAE,MAAM,UAAU,CAAC;AAGjD;;;GAGG;AACH,eAAO,MAAM,qBAAqB,SAAU,MAAM,SAGjD,CAAC;AAEF;;;;GAIG;AACH,eAAO,MAAM,gBAAgB,SAAgB,MAAM,iBAUlD,CAAC;AAEF;;;;;;GAMG;AACH,eAAO,MAAM,cAAc,WAAY,qBAAqB,SAS3D,CAAC;AAEF,eAAO,MAAM,eAAe,QAAS,MAAM,OAAO,MAAM,WAEvD,CAAC;AAEF,eAAO,MAAM,eAAe,SAAU,MAAM,YAG3C,CAAC;AAEF,eAAO,MAAM,iBAAiB,YAAa,MAAM,aAKhD,CAAC;AAEF,wBAAgB,UAAU,CAAC,CAAC,SAAS,MAAM,EAAE,KAAK,EAAE,CAAC,EAAE,EAAE,SAAS,EAAE,MAAM,SASzE;AAED,wBAAgB,YAAY,CAAC,CAAC,EAAE,IAAI,EAAE,CAAC,EAAE,EAAE,IAAI,EAAE,CAAC,EAAE,GAAG,CAAC,EAAE,CAczD;AAED,eAAO,MAAM,OAAO,UAAiB,CAAC,MAAM,OAAO,CAAC,IAAI,CAAC,CAAC,EAAE,kBAoB3D,CAAC"}
|
package/lib/utils.js
CHANGED
|
@@ -9,6 +9,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
9
9
|
};
|
|
10
10
|
import path from "path";
|
|
11
11
|
import fs from "fs";
|
|
12
|
+
import { logErrorToFile } from "./log/index.js";
|
|
12
13
|
/**
|
|
13
14
|
* 不存在的文件夹则创建
|
|
14
15
|
* @param {string} path
|
|
@@ -86,3 +87,25 @@ export function intersection(arr1, arr2) {
|
|
|
86
87
|
}
|
|
87
88
|
return [...intersectionSet];
|
|
88
89
|
}
|
|
90
|
+
export const dequeue = (queue) => __awaiter(void 0, void 0, void 0, function* () {
|
|
91
|
+
while (queue.length) {
|
|
92
|
+
const requestPromiseFactory = queue.shift(); // 出列
|
|
93
|
+
if (!requestPromiseFactory) {
|
|
94
|
+
dequeue(queue);
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
try {
|
|
98
|
+
yield requestPromiseFactory();
|
|
99
|
+
}
|
|
100
|
+
catch (err) {
|
|
101
|
+
logErrorToFile({
|
|
102
|
+
error: err,
|
|
103
|
+
language: "ar",
|
|
104
|
+
fileName: "",
|
|
105
|
+
key: "",
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
finally {
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
});
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -14,7 +14,7 @@ import {
|
|
|
14
14
|
SupportLanguageType,
|
|
15
15
|
} from "./types";
|
|
16
16
|
import {
|
|
17
|
-
|
|
17
|
+
dequeue,
|
|
18
18
|
getRandomNumber,
|
|
19
19
|
notExistsToCreateFile,
|
|
20
20
|
readFileOfDirSync,
|
|
@@ -139,13 +139,7 @@ export class CwalletTranslate {
|
|
|
139
139
|
);
|
|
140
140
|
}
|
|
141
141
|
}
|
|
142
|
-
|
|
143
|
-
promises = chunkArray(arr, 8);
|
|
144
|
-
|
|
145
|
-
for (const chunk of promises) {
|
|
146
|
-
await Promise.all(chunk.map((fn) => fn()));
|
|
147
|
-
}
|
|
148
|
-
|
|
142
|
+
await dequeue(arr);
|
|
149
143
|
multiBar.stop();
|
|
150
144
|
console.log("🚀 翻译完毕");
|
|
151
145
|
};
|
|
@@ -264,7 +258,7 @@ export class CwalletTranslate {
|
|
|
264
258
|
value: chatCompletion?.choices[0]?.message.content ?? value,
|
|
265
259
|
index,
|
|
266
260
|
});
|
|
267
|
-
}, getRandomNumber(
|
|
261
|
+
}, getRandomNumber(120, 130));
|
|
268
262
|
} catch (error) {
|
|
269
263
|
logErrorToFile({ error: error as Error, key, fileName, language });
|
|
270
264
|
resolve({
|
package/src/lib/utils.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import path from "path";
|
|
2
2
|
import fs from "fs";
|
|
3
3
|
import { ICreateJsonFileParams } from "../types";
|
|
4
|
+
import { logErrorToFile } from "./log/index.js";
|
|
4
5
|
|
|
5
6
|
/**
|
|
6
7
|
* 不存在的文件夹则创建
|
|
@@ -88,3 +89,25 @@ export function intersection<T>(arr1: T[], arr2: T[]): T[] {
|
|
|
88
89
|
|
|
89
90
|
return [...intersectionSet];
|
|
90
91
|
}
|
|
92
|
+
|
|
93
|
+
export const dequeue = async (queue: (() => Promise<void>)[]) => {
|
|
94
|
+
while (queue.length) {
|
|
95
|
+
const requestPromiseFactory = queue.shift(); // 出列
|
|
96
|
+
if (!requestPromiseFactory) {
|
|
97
|
+
dequeue(queue);
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
try {
|
|
102
|
+
await requestPromiseFactory();
|
|
103
|
+
} catch (err) {
|
|
104
|
+
logErrorToFile({
|
|
105
|
+
error: err as Error,
|
|
106
|
+
language: "ar",
|
|
107
|
+
fileName: "",
|
|
108
|
+
key: "",
|
|
109
|
+
});
|
|
110
|
+
} finally {
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
};
|
package/test.mjs
CHANGED
|
@@ -1,16 +1,17 @@
|
|
|
1
1
|
import path from "path";
|
|
2
|
-
import { deleteBatchCache, generateCache } from "./index.js";
|
|
2
|
+
import { deleteBatchCache, generateCache, CwalletTranslate } from "./index.js";
|
|
3
3
|
|
|
4
|
-
// deleteBatchCache({
|
|
5
|
-
// keys: ["10001", "10002"],
|
|
6
|
-
// cacheFolderPath: path.resolve("./src/cache"),
|
|
7
|
-
// cacheFileName: "common.json",
|
|
8
|
-
// languages: [],
|
|
4
|
+
// // deleteBatchCache({
|
|
5
|
+
// // keys: ["10001", "10002"],
|
|
6
|
+
// // cacheFolderPath: path.resolve("./src/cache"),
|
|
7
|
+
// // cacheFileName: "common.json",
|
|
8
|
+
// // languages: [],
|
|
9
|
+
// // });
|
|
10
|
+
|
|
11
|
+
// generateCache({
|
|
12
|
+
// sourceFolderPath: path.resolve("./src/langs"),
|
|
13
|
+
// sourceLanguage: "en",
|
|
14
|
+
// exportFolderPath: path.resolve("./src/cache"),
|
|
15
|
+
// languages: ["ja", "zh-CN"],
|
|
9
16
|
// });
|
|
10
17
|
|
|
11
|
-
generateCache({
|
|
12
|
-
sourceFolderPath: path.resolve("./src/langs"),
|
|
13
|
-
sourceLanguage: "en",
|
|
14
|
-
exportFolderPath: path.resolve("./src/cache"),
|
|
15
|
-
languages: ["ja", "zh-CN"],
|
|
16
|
-
});
|