-
Notifications
You must be signed in to change notification settings - Fork 3
/
util.js
145 lines (121 loc) · 3.74 KB
/
util.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
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
const express = require("express");
const axios = require("axios");
const fs = require("fs");
const hasha = require("hasha");
const docsify = require("./docsify");
const plugins = require("./plugins");
let mainConfig = require("./config.json");
// Compares local and remote readme for changes
function isDiffReadme(user, repo, readmeData) {
if (fs.existsSync(`./_docs/${user}-${repo}/README.md`)) {
const localReadmeHash = hasha.fromFileSync(
`./_docs/${user}-${repo}/README.md`,
{ algorithm: "md5" }
);
const remoteReadmeHash = hasha(readmeData, { algorithm: "md5" });
if (localReadmeHash == remoteReadmeHash) {
return false;
}
}
return true;
}
// Writes all the required files
async function writeDocs(user, repo, readmeData) {
const config = await getConfig(user, repo);
if (config) {
writeDocsWithConfig(user, repo, readmeData, config);
} else {
writeDocsWithDefaultConfig(user, repo, readmeData);
}
}
async function writeDocsWithDefaultConfig(user, repo, readmeData) {
const htmlData = docsify.generateHTML(user, repo);
fs.writeFileSync(`./_docs/${user}-${repo}/README.md`, readmeData, function (
err
) {
if (err) throw err;
console.log("Generated ReadMe!");
});
fs.writeFileSync(`./_docs/${user}-${repo}/index.html`, htmlData, function (
err
) {
if (err) throw err;
console.log("Generated HTML!");
});
}
async function writeDocsWithConfig(user, repo, readmeData, config) {
try {
Object.keys(config).forEach((key) => {
mainConfig[`${key}`] = config[key];
});
mainConfig.enablePlugins = false;
if (mainConfig.plugins.length > 0) {
mainConfig.enablePlugins = true;
}
if (mainConfig.enablePlugins) {
const pluginConfig = plugins.addPlugins(mainConfig.plugins);
mainConfig.plugins = pluginConfig;
}
const htmlData = docsify.generateHtmlWithConfig(mainConfig);
fs.writeFileSync(`./_docs/${user}-${repo}/README.md`, readmeData, function (
err
) {
if (err) throw err;
console.log("Generated ReadMe!");
});
fs.writeFileSync(`./_docs/${user}-${repo}/index.html`, htmlData, function (
err
) {
if (err) throw err;
console.log("Generated HTML!");
});
} catch (error) {
writeDocsWithDefaultConfig(user, repo, readmeData);
}
}
// Gets readme contents and generates /_docs/<files> with docsify enabled to render
async function getReadme(user, repo) {
mainConfig.user = user;
mainConfig.repo = repo;
const readme = await axios.get(
`https://api.github.com/repos/${user}/${repo}/readme`
);
const readmeUrl = readme.data.download_url;
const readmeData = await axios.get(readmeUrl);
if (!fs.existsSync("./_docs/")) {
fs.mkdirSync("./_docs/");
}
if (!fs.existsSync(`./_docs/${user}-${repo}/`)) {
fs.mkdirSync(`./_docs/${user}-${repo}/`);
}
// Write file only if github readme is different from local copy
if (isDiffReadme(user, repo, readmeData.data)) {
await writeDocs(user, repo, readmeData.data);
}
}
// Serves the compiled docs
function serveDocs(app, user, repo) {
app.use(`/docs/${user}/${repo}`, express.static(`./_docs/${user}-${repo}`));
}
async function getConfig(user, repo) {
const gitRepo = await axios.get(
`https://api.github.com/repos/${user}/${repo}/contents`
);
const gitRepoFiles = gitRepo.data;
let configUrl = "";
for (let i = 0; i < gitRepoFiles.length; i++) {
if (gitRepoFiles[i].name == ".fastdocs.json") {
configUrl = gitRepoFiles[i].download_url;
break;
}
}
if (configUrl != "") {
const config = await axios.get(configUrl);
return config.data;
}
return "";
}
async function compileDocs(user, repo) {
await getReadme(user, repo);
}
module.exports = { compileDocs, serveDocs };