Repository navigation
Expand file tree
/
Copy pathcollectProgramData.js
More file actions
224 lines (199 loc) · 5.57 KB
/
Copy pathcollectProgramData.js
File metadata and controls
224 lines (199 loc) · 5.57 KB
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
import fetch from "node-fetch";
import fs from "fs";
import dateFns from "date-fns";
import matter from "gray-matter";
import { remark } from "remark";
import html from "remark-html";
export default async function collectProgramData(
OWNER,
PROGRAM_REPO,
PROGRAM_REPO_FOLDER,
TOKEN
) {
const programFileDir = "program-data/program";
await fs.promises.mkdir(programFileDir, { recursive: true });
const languages = await fetch(
`https://raw.githubusercontent.com/codinasion/program/master/data/languages.json`,
{
method: "GET",
headers: {
Authorization: `token ${TOKEN}`,
},
}
).then((res) => res.json());
const programList = [];
const pathsData = await fetch(
`https://api.github.com/repos/${OWNER}/${PROGRAM_REPO}/git/trees/master?recursive=1`,
{
method: "GET",
headers: {
Authorization: `token ${TOKEN}`,
},
}
)
.then((res) => res.json())
.then((res) => res.tree)
.catch((error) => {
console.log(error);
});
const filesData = [];
for (const data of pathsData) {
if (
data.path.startsWith(PROGRAM_REPO_FOLDER + "/") &&
data.path !== `${PROGRAM_REPO_FOLDER}/README.md` &&
!data.path.endsWith(".png") &&
!data.path.endsWith(".jpg") &&
!data.path.endsWith(".jpeg") &&
!data.path.endsWith(".gif") &&
!data.path.endsWith(".svg")
) {
let path = data.path.replace(PROGRAM_REPO_FOLDER + "/", "");
if (path.includes("/")) {
let slug = path.split("/")[0];
let file = path.split("/")[1];
// check if slug is already in the filesData array
let index = filesData.findIndex(
(programfile) => programfile.slug === slug
);
if (index === -1) {
// if slug is not in the array, add it
filesData.push({
slug,
files: [file],
});
} else {
// if slug is in the array, add the file to the files array
filesData[index].files.push(file);
}
}
}
}
for (const data of filesData) {
let slug = data.slug;
let readme_text = await fetch(
`https://raw.githubusercontent.com/${OWNER}/${PROGRAM_REPO}/master/${PROGRAM_REPO_FOLDER}/${slug}/README.md`,
{
method: "GET",
headers: {
Authorization: `token ${TOKEN}`,
},
}
)
.then((res) => res.text())
.catch((error) => console.log(error));
var files = data.files;
// remove README.md from files array
files = files.filter((programfile) => programfile !== "README.md");
const tags = [];
for (const programfile of files) {
let extension = programfile.split(".")[1];
// find name of language from extension
let language = languages.filter(
(language) => language.fileExtension === extension
)[0];
if (language) {
tags.push(language.name);
}
}
let code_text = `
<CodeBlock slug="${slug}" >
`;
for (const programfile of files) {
let response_text = await fetch(
`https://raw.githubusercontent.com/${OWNER}/${PROGRAM_REPO}/master/${PROGRAM_REPO_FOLDER}/${slug}/${programfile}`,
{
method: "GET",
headers: {
Authorization: `token ${TOKEN}`,
},
}
)
.then((res) => res.text())
.catch((error) => console.log(error));
code_text += `
\`\`\`${programfile.split(".")[1]}
${response_text.trim()}
\`\`\`
`;
}
code_text += `
</CodeBlock>
`;
let source = `${readme_text.trim()}
${code_text}
`;
let matterResult = await matter(source);
let processedContent = await remark()
.use(html)
.process(matterResult.content);
let contentHtml = processedContent.toString();
let latestUpdateDate = await fetch(
`https://api.github.com/repos/${OWNER}/${PROGRAM_REPO}/commits?path=${PROGRAM_REPO_FOLDER}/${slug}`,
{
method: "GET",
headers: {
Authorization: `token ${TOKEN}`,
},
}
)
.then((res) => res.json())
.then((res) => res[0].commit.author.date)
.catch((error) => {
console.log(error);
return dateFns.date.now();
});
let contributors = await fetch(
`https://api.github.com/repos/${OWNER}/${PROGRAM_REPO}/commits?path=${PROGRAM_REPO_FOLDER}/${slug}`,
{
method: "GET",
headers: {
Authorization: `token ${TOKEN}`,
},
}
)
.then((res) => res.json())
.then((res) => res.map((commit) => commit.author.login))
.catch((error) => {
console.log(error);
});
// remove duplicate contributors
contributors = [...new Set(contributors)];
let programData = {
slug,
title: matterResult.data.title,
trackId: matterResult.data.trackId,
tags,
contributors,
contentHtml,
markdown: matterResult.content,
latestUpdateDate,
};
// save program data to file
const filePath = `${programFileDir}/${slug}.json`;
await fs.promises.writeFile(
filePath,
JSON.stringify(programData),
(err) => {
if (err) {
console.log(err);
}
}
);
await programList.push({
slug,
title: matterResult.data.title,
trackId: matterResult.data.trackId,
tags,
contributors,
latestUpdateDate,
});
}
await console.log(programList);
// save program list to file
const filePath = `program-data/programList.json`;
await fs.promises.writeFile(filePath, JSON.stringify(programList), (err) => {
if (err) {
console.log(err);
}
});
}