-
Notifications
You must be signed in to change notification settings - Fork 2
/
calculate.ts
158 lines (130 loc) · 4.5 KB
/
calculate.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
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
// deno run --allow-read --allow-write --allow-env --allow-net calculate.ts
async function fetchProgress(): Promise<any> {
const projectId = "645542";
const apiKey = Deno.env.get("API_KEY");
if (!apiKey) {
console.error("API_KEY environment variable is not set.");
return [];
}
const url = `https://api.crowdin.com/api/v2/projects/${projectId}/languages/progress`;
try {
const response = await fetch(url, {
method: "GET",
headers: {
Authorization: `Bearer ${apiKey}`,
"Content-Type": "application/json",
},
});
if (response.ok) {
const responseData = await response.json();
return responseData.data;
} else {
throw new Error(`Failed to fetch progress: ${response.statusText}`);
}
} catch (error) {
console.error("Error fetching progress:", error);
return [];
}
}
async function readReadmeTemplate(): Promise<string> {
try {
const template = await Deno.readTextFile("README.template.md");
return template;
} catch (error) {
console.error("Error reading README template:", error);
return "";
}
}
async function writeToReadme(content: string): Promise<void> {
try {
await Deno.writeTextFile("README.md", content);
} catch (error) {
console.error("Error writing to README.md:", error);
}
}
import { Language, LanguageEntry, Languages } from "./Languages.ts";
async function main() {
const template = await readReadmeTemplate();
if (!template) {
return;
}
const progressData = await fetchProgress();
if (progressData.length === 0) {
return;
}
let table = `| | Language | Translation Progress | Approval Progress |
|:-:|---|---|---|
`;
const fileData: any = {
incomplete: [],
verified: [],
};
for (const item of progressData) {
const languageId = item.data.languageId;
let mappedLanguageId = languageId;
console.log("Language ID: " + languageId);
// Map Brazilian Portuguese (pt-BR) to Portuguese (pt)
if (languageId === "pt-BR") {
mappedLanguageId = "pt";
}
console.log("Mapped Language ID: " + mappedLanguageId);
if (Languages[mappedLanguageId]) {
const translationProgress = item.data.translationProgress;
const approvalProgress = item.data.approvalProgress;
if (translationProgress === 100) {
if (approvalProgress === 100) {
fileData.verified.push(mappedLanguageId);
await updateLanguagesEntry(mappedLanguageId, { verified: true });
} else if (approvalProgress < 100) {
fileData.incomplete.push(mappedLanguageId);
await updateLanguagesEntry(mappedLanguageId, { incomplete: true });
}
} else if (translationProgress < 100) {
fileData.incomplete.push(mappedLanguageId);
await updateLanguagesEntry(mappedLanguageId, { incomplete: true });
}
const languageData = Languages[mappedLanguageId];
table += `| ${languageData.emoji} | ${languageData.display} | ${item.data.translationProgress}% | ${item.data.approvalProgress}% |\n`;
}
}
async function updateLanguagesEntry(
languageId: Language,
updates: Partial<LanguageEntry>
) {
const originalContent = await Deno.readTextFile("./Languages.ts");
const objectRegex = new RegExp(`${languageId}:\\s*{[^}]+}`, "g");
const match = originalContent.match(objectRegex);
if (match) {
const objectString = match[0];
const updatedObjectString = objectString.replace(
/{([^}]+)}/,
(_, content) => {
const currentObject: LanguageEntry = eval(`({${content}})`); // Unsafe, but fine for this purpose
console.log("Current Object:", currentObject); // Log the current object
const updatedObject: LanguageEntry = { ...currentObject, ...updates };
const updatedProperties = Object.entries(updatedObject)
.map(([key, value]) => ` ${key}: ${JSON.stringify(value)}`)
.join(",\n");
return `{\n${updatedProperties}\n}`;
}
);
const updatedContent = originalContent.replace(
objectRegex,
updatedObjectString
);
await Deno.writeTextFile("./Languages.ts", updatedContent);
}
}
await Promise.all([
Deno.writeTextFile(
"verified.js",
"export default " + JSON.stringify(fileData.verified, undefined, "\t")
),
Deno.writeTextFile(
"incomplete.js",
"export default " + JSON.stringify(fileData.incomplete, undefined, "\t")
),
]);
await writeToReadme(template.replace("{{TABLE}}", table));
}
main();