-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
87 lines (79 loc) · 3.03 KB
/
index.js
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
/******************************************************/
/**
* @author Vedansh (offensive-vk)
* @url https://github.com/offensive-vk/auto-translate/
* @type Github Action for Translating the README.
* @lang JavaScript + Node.js
* @uses Octokit and Actions Core
* @runs Nodejs v20.x
*/
/******************************************************/
const { readFileSync, writeFileSync, readdirSync } = require("fs");
const { join } = require("path");
const core = require("@actions/core");
const github = require("@actions/github");
const githubToken = core.getInput("repo-token", { required: false });
const committer = core.getInput("committer") || "github-actions[bot]";
const commitMessage = core.getInput("commit-message") || `CI: Translation Successful.`;
const commitOptions = core.getInput("commit-options") || "";
const filePath = core.getInput("file") || "README.md";
const lang = core.getInput("language") || "es";
const $ = require("@iamtraction/google-translate");
const simpleGit = require("simple-git");
const git = simpleGit();
const unified = require("unified");
const parse = require("remark-parse");
const stringify = require("remark-stringify");
const visit = require("unist-util-visit");
const toAst = (markdown) => unified().use(parse).parse(markdown);
const toMarkdown = (ast) => unified().use(stringify).stringify(ast);
const mainDir = ".";
let README = readdirSync(mainDir).includes(filePath) ? filePath : "README.md";
const readme = readFileSync(join(mainDir, README), { encoding: "utf8" });
const readmeAST = toAst(readme);
console.log("*** Started Processing ***");
async function translateNode(node) {
if (node.type === "text") {
const translated = await $(node.value, { to: lang });
node.value = translated.text;
}
}
async function translateReadme() {
try {
const nodes = [];
visit(readmeAST, (node) => {
nodes.push(translateNode(node));
});
await Promise.all(nodes);
writeFileSync(
join(mainDir, `README.${lang}.md`),
toMarkdown(readmeAST),
"utf8"
);
core.setCommandEcho(true);
await git.add('*.md');
console.log(`README.${lang}.md Translated.`);
} catch (error) {
console.error("Error during translation:", error);
console.dir(error, {depth: Infinity});
}
}
async function commitChanges() {
try {
console.log("*** Commit and Push ***");
await git.init();
await git.addConfig("user.name", committer, append = true, scope = 'global');
await git.addConfig("user.email", `${committer}@users.noreply.github.com`, append = true, scope = 'global');
await git.add(".");
await git.commit(`${commitMessage} (${commitOptions})`);
console.log("*** Committed Successfully ***");
const fullRepoName = `${github.context.repo.owner}/${github.context.repo.repo}`;
await git.push(`https://${encodeURIComponent(githubToken)}@github.com/${fullRepoName}.git`);
console.log("*** Pushed to Remote Repository ***");
} catch (error) {
console.error("*** Git Push Failed ***", error.message);
console.log(error);
}
}
// Translate
translateReadme();