-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathbuild-indices.ts
288 lines (242 loc) · 9.04 KB
/
build-indices.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
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
import fs from 'fs-extra';
import path from 'path';
import glob from 'glob';
import yaml from 'yaml';
import day from 'dayjs';
import utc from 'dayjs/plugin/utc';
import customParseFormat from 'dayjs/plugin/customParseFormat';
import metadataParser from 'markdown-yaml-metadata-parser';
import markdownToTxt from 'markdown-to-txt';
import { Expert as LocalExpert } from './src/schema/expert-local';
import { ExpertMini, ExternalExpertMini } from './src/schema/expert-mini';
import { Blog } from './src/schema/blog';
import { getGlobalExpertInfo } from './src/services/campus-expert';
import { SITE_URL, PAGE_SIZE, DATE_FORMAT, UTC_OFFSET } from './src/constants';
fs.ensureDirSync(path.join(__dirname, 'public', 'local'));
fs.ensureDirSync(path.join(__dirname, 'public', 'users'));
fs.ensureDirSync(path.join(__dirname, 'public', 'resources'));
fs.emptyDirSync(path.join(__dirname, 'public', 'resources'));
day.extend(utc);
day.extend(customParseFormat);
day().utcOffset(UTC_OFFSET);
const state: {
users?: (ExpertMini | ExternalExpertMini)[],
blogs?: Blog[]
} = {};
const summarize = (markdown: string, maxLength: number = 150) => {
const text = markdownToTxt(markdown);
return text.length > maxLength ? text.slice(0, maxLength) + '...' : text;
};
const indexUsers = () => new Promise(async done => {
const p = path.join(__dirname, 'public', 'users', '*') + path.sep;
glob(p, {}, async (e: Error | null, files: string[]) => {
if (e != null)
throw e;
try {
const users: (ExpertMini | ExternalExpertMini)[] = await Promise.all(files.map(async f => {
const o = {
username: path.basename(f),
path: path.relative(path.join(__dirname, 'public'), f)
};
const filesToCheck = [
'about.md',
'info.yml'
];
if (!filesToCheck.map(file => fs.existsSync(path.join(f, file))).reduce((a, b) => a && b))
throw new Error(`Invalid contents of directory '${path.join('public', o.path)}'!`);
const infoFile = path.join(f, 'info.yml');
const info: LocalExpert = yaml.parse((await fs.readFile(infoFile)).toString());
if (
!['name', 'university', 'location', 'country', 'avatar', 'email'].map(k => info[k] != null).reduce((a, b) => a && b)
||
!['communities', 'skills', 'social'].map(k => info[k] == null || Array.isArray(info[k])).reduce((a, b) => a && b)
)
throw new Error(`Invalid values for '${o.username}'.`);
const user: ExpertMini = {
username: o.username,
name: info.name,
university: info.university,
location: info.location,
country: info.country,
avatar: info.avatar,
communities: info.communities || [],
local: true
};
return user;
}));
const externalIndexFile = path.join(__dirname, 'public', 'local', 'index.yml');
if (fs.existsSync(externalIndexFile)) {
const localUsernames: string[] = users.map(u => u.username);
const { users: externalUsers }: {
users: string[]
} = yaml.parse((await fs.readFile(externalIndexFile)).toString());
const externalIndex: ExternalExpertMini[] = await Promise.all(externalUsers
.filter(u => localUsernames.indexOf(u) < 0)
.map(async username => {
try {
const data = await getGlobalExpertInfo(username);
const user: ExternalExpertMini = {
name: data.name,
username: data.username,
picture: data.picture,
university: data.university,
location: data.location,
local: false
};
return user;
} catch (e) {
throw new Error(`Error for username: ${username}! Official profile not found on githubcampus.expert.`);
}
}));
for (const user of externalIndex)
if (user != null)
users.push(user);
}
users.sort((a, b) => a.username.localeCompare(b.username));
state.users = users;
done(null);
} catch (e: any) {
console.error((e as Error).message, e);
process.exit(1);
}
});
});
const indexBlogs = () => new Promise(async done => {
const p = path.join(__dirname, 'public', 'users', '**', 'blogs', '*.md');
glob(p, {}, async (e: Error | null, files: string[]) => {
if (e != null)
throw e;
try {
const blogs: Blog[] = await Promise.all(files.map(async f => {
const split = f.split(path.sep);
const filename = split[split.length - 1];
const username = split[split.length - 3];
const id = path.basename(filename, '.md')
const rawData = (await fs.readFile(f)).toString();
const {
metadata: info,
content: markdown
} = metadataParser(rawData);
if (
!['title'].map(k => !!info[k]).reduce((a, b) => a && b)
||
!['date'].map(k => day(info[k], DATE_FORMAT).isValid()).reduce((a, b) => a && b)
)
throw new Error(`Invalid values for '${username}/blogs/${id}'.`);
const user = state.users.find(u => u.username === username);
user.blogs = true;
return {
id,
user: username,
title: info.title,
date: day(info.date, DATE_FORMAT).toDate(),
data: markdown.trim()
};
}));
blogs.sort((a, b) => b.date.valueOf() - a.date.valueOf());
state.blogs = blogs;
done(null);
} catch (e: any) {
console.error((e as Error).message, e);
process.exit(1);
}
});
});
const writeIndices = async () => {
const { users, blogs } = state;
const routes = [
'',
'discover',
'about',
'contact',
'blog',
...users.map(u => u.username),
...blogs.slice(0, 25).map(b => `blog/${b.user}/${encodeURIComponent(b.id)}`)
];
const sitemap = path.join(__dirname, 'public', 'sitemap.txt');
await fs.writeFile(sitemap, '', { flag: 'w+', encoding: 'utf-8' });
for (const route of routes)
await fs.writeFile(sitemap, SITE_URL + (route === '' ? '' : ('?/' + route)) + '\n', { flag: 'a+', encoding: 'utf-8' });
console.log('Sitemap generated.');
for (const u of users.filter(u => u.blogs)) {
delete u.blogs;
const userBlogs = blogs.filter(b => b.user === u.username);
const pageCount = Math.floor(userBlogs.length / PAGE_SIZE);
const leftItemCount = userBlogs.length % PAGE_SIZE;
await fs.ensureDir(path.join(__dirname, 'public', 'users', u.username, 'blogs', 'index'));
await fs.emptyDir(path.join(__dirname, 'public', 'users', u.username, 'blogs', 'index'));
for (let i = 0; i < pageCount; i++) {
const init = i * PAGE_SIZE;
const final = (i + 1) * PAGE_SIZE;
let pageBlogs: any[] = userBlogs.slice(init, final);
pageBlogs = pageBlogs.map(b => ({
id: b.id,
title: b.title,
date: day(b.date).format(DATE_FORMAT),
summary: summarize(b.data)
}));
const ymlDataObject = { blogs: pageBlogs, lastPage: i === pageCount - 1 && leftItemCount === 0 };
const ymlData = yaml.stringify(ymlDataObject);
await fs.writeFile(path.join(__dirname, 'public', 'users', u.username, 'blogs', 'index', `${i}.yml`), ymlData);
}
if (leftItemCount > 0) {
let pageBlogs: any[] = userBlogs.slice(-leftItemCount);
pageBlogs = pageBlogs.map(b => ({
id: b.id,
title: b.title,
date: day(b.date).format(DATE_FORMAT),
summary: summarize(b.data)
}));
const ymlDataObject = { blogs: pageBlogs, lastPage: true };
const ymlData = yaml.stringify(ymlDataObject);
await fs.writeFile(path.join(__dirname, 'public', 'users', u.username, 'blogs', 'index', `${pageCount}.yml`), ymlData);
}
console.log(`${pageCount + (leftItemCount > 0 ? 1 : 0)} blog page(s) generated for '${u.username}'`);
}
const index = yaml.stringify({ users });
await fs.writeFile(path.join(__dirname, 'public', 'resources', 'index.yml'), index);
console.log(`${users.length} campus experts indexed.`);
{
const pageCount = Math.floor(blogs.length / PAGE_SIZE);
const leftItemCount = blogs.length % PAGE_SIZE;
await fs.ensureDir(path.join(__dirname, 'public', 'resources', 'blogs'));
await fs.emptyDir(path.join(__dirname, 'public', 'resources', 'blogs'));
for (let i = 0; i < pageCount; i++) {
const init = i * PAGE_SIZE;
const final = (i + 1) * PAGE_SIZE;
let pageBlogs: any[] = blogs.slice(init, final);
pageBlogs = pageBlogs.map(b => ({
id: b.id,
title: b.title,
author: b.user,
date: day(b.date).format(DATE_FORMAT),
summary: summarize(b.data)
}));
const ymlDataObject = { blogs: pageBlogs, lastPage: i === pageCount - 1 && leftItemCount === 0 };
const ymlData = yaml.stringify(ymlDataObject);
await fs.writeFile(path.join(__dirname, 'public', 'resources', 'blogs', `${i}.yml`), ymlData);
}
if (leftItemCount > 0) {
let pageBlogs: any[] = blogs.slice(-leftItemCount);
pageBlogs = pageBlogs.map(b => ({
id: b.id,
title: b.title,
author: b.user,
date: day(b.date).format(DATE_FORMAT),
summary: summarize(b.data)
}));
const ymlDataObject = { blogs: pageBlogs, lastPage: true };
const ymlData = yaml.stringify(ymlDataObject);
await fs.writeFile(path.join(__dirname, 'public', 'resources', 'blogs', `${pageCount}.yml`), ymlData);
}
console.log(`${pageCount + (leftItemCount > 0 ? 1 : 0)} global blog page(s) generated.`);
}
};
if (require.main === module)
(async () => {
await indexUsers();
await indexBlogs();
await writeIndices();
console.log(`Successfully built all indices!`);
process.exit(0);
})();