-
Notifications
You must be signed in to change notification settings - Fork 0
/
inject.ts
42 lines (37 loc) · 929 Bytes
/
inject.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
import * as fs from "fs/promises";
import { extractI18nContent, extractI18nIndexes } from "./extract";
export const inject = async (
message: string,
{
filePath,
content,
}: {
filePath: string;
content: string;
},
lang: string
) => {
const adapt = (message: string): string => {
if (message.startsWith("```yaml\n")) {
message = message.replace(
"```yaml\n",
`# Automated ${lang} translation\n`
);
}
if (message.endsWith("```")) {
message = message.replace("```", "");
}
return message.trim();
};
const i18nContent = extractI18nContent(content);
const { start, end } = extractI18nIndexes(content)!;
const [fileStart, _, fileEnd] = [
content.slice(0, start),
content.slice(start, end),
content.slice(end),
];
await fs.writeFile(
filePath,
fileStart + i18nContent + "\n" + adapt(message) + "\n" + fileEnd
);
};