-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.ts
134 lines (105 loc) · 3.53 KB
/
main.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
import fs from "fs";
import path from "path";
import { simpleGit } from "simple-git";
import configuration from "./app-config.json";
const reposPath = "repos";
const docsPath = "Developer Documentation";
interface Repository {
name: string;
url: string;
}
function createFolderIfNotExists(path: string) {
fs.existsSync(path) || fs.mkdirSync(path, { recursive: true });
}
function checkIfRepositoryExists(name: string) {
if (!fs.existsSync(`${reposPath}/${name}`)) return false;
return simpleGit(`${reposPath}/${name}`).checkIsRepo();
}
function prepareFolders() {
fs.rmSync(docsPath, { recursive: true, force: true });
createFolderIfNotExists(reposPath);
createFolderIfNotExists(docsPath);
}
async function fetchLatestDocuments(repositories: Array<Repository>) {
prepareFolders();
console.log("Fetching repositories documents...");
await Promise.all(
repositories.map(async (repository: Repository) => {
const { name, url } = repository;
if (checkIfRepositoryExists(name)) {
await simpleGit(`${reposPath}/${name}`).pull();
} else {
await simpleGit(reposPath).clone(url, name);
}
findMarkdownFilesAndCopyToDocs(`${reposPath}/${name}`);
})
);
fs.copyFileSync("README.md", `${docsPath}/README.md`);
console.log("Documents fetched and placed to the docs folder.");
}
function findMarkdownFilesAndCopyToDocs(dir: string) {
const output: string[] = [];
searchFiles(dir, ".md");
function searchFiles(dir: string, fileName: string) {
const files = fs.readdirSync(dir);
for (const file of files) {
const filePath = path.join(dir, file);
const fileStat = fs.statSync(filePath);
if (fileStat.isDirectory()) {
searchFiles(filePath, fileName);
} else if (file.endsWith(fileName)) {
output.push(filePath);
}
}
}
output.forEach((file) => {
let destFileDivs = file.split("/");
destFileDivs.splice(0, 1, docsPath);
const destFile = destFileDivs.join("/");
const destFolder = destFileDivs.slice(0, -1).join("/");
createFolderIfNotExists(destFolder);
fs.copyFileSync(file, destFile);
formatFileContent(destFile);
});
}
function formatFileContent(file: string) {
let fileChanged = false;
let content = fs.readFileSync(file, "utf8");
const ghBlobUrlRegex =
/https:\/\/github\.com\/[A-Za-z]+\/([A-Za-z]+(-[A-Za-z]+)+)\/blob\/[A-Za-z0-9]+\/([A-Za-z]+(\/[A-Za-z]+)+)\.[A-Za-z]+#L[0-9]+-L[0-9]+/g;
for (const match of content.matchAll(ghBlobUrlRegex)) {
const url = match[0];
const codeBlockForUrl = `{% @github-files/github-code-block url="${url}" %}`;
if (
content.includes(codeBlockForUrl) ||
content[content.indexOf(url) - 1] !== "\n" // checking if the url is used in a link
)
continue;
content = content.replace(url, codeBlockForUrl);
fileChanged = true;
}
if (fileChanged) fs.writeFileSync(file, content);
}
function getRepositories() {
return configuration.repoUrls.map((url) => ({
url,
name: url.split("/").pop()!,
}));
}
// Main function
await (async function () {
if (!Bun.env.GH_TOKEN) {
console.error("GitHub access token is required!");
process.exit(1);
}
const appGit = simpleGit(process.cwd())
.removeRemote("origin")
.addRemote(
"origin",
`https://${Bun.env.GH_TOKEN}@github.com/aeternity/docs`
)
.pull("origin", "master");
await fetchLatestDocuments(getRepositories());
await appGit.add(docsPath).commit("Update docs").push("origin", "master");
console.log("Docs synced successfully!");
})();