forked from Slimefun/Wiki
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmissing.js
151 lines (123 loc) · 4.39 KB
/
missing.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
146
147
148
149
150
151
// Imports from default node modules
const fs = require('fs');
const http = require("https");
// This is the URL to the deployed wiki
const url = "https://github.com/Slimefun/Slimefun4/wiki/";
// The regular expression to check for links that lead to a wiki page
const regex = /\(https:\/\/github\.com\/Slimefun\/Slimefun4\/wiki\/[A-Za-z-]+\)/g;
/**
* These are the options for our PATCH request to update Issue #2
* @type {Object}
*/
const options = {
method: "PATCH",
hostname: "api.github.com",
path: "/repos/Slimefun/Wiki/issues/2",
headers: {
"User-Agent": "Slimefun Wiki Action",
"authorization": `token ${process.env.ACCESS_TOKEN}`,
"content-type": "application/x-www-form-urlencoded"
}
}
// The queue of file scan tasks
const queue = [];
// All found pages
const pages = [];
// All missing pages
const missing = [];
// This is our placeholder text for any page that is missing
const missingPageTemplate = fs.readFileSync("pages/missing.md", "UTF-8");
// This will be the body for our issue
var issueBody = `## :spider_web: The following pages are still missing!
Help us out by contributing to the wiki and create one or more of these pages, it would be awesome! :heart:<br>
We have a detailed guide on how to submit changes to the wiki for you right here:
https://github.com/Slimefun/Slimefun4/wiki/Expanding-the-Wiki
## :books: List of missing pages
`;
// Read the contents of our /pages/ directory as an async promise
fs.promises.readdir("pages").then(files => {
// Loop through all files in that directory
for (let i in files) {
// Queue another task to find linked pages
queue.push(fs.promises.readFile(`pages/${files[i]}`, "UTF-8").then(scanFile));
}
// Finish working off the queue and evaluate afterwards
Promise.all(queue).then(() => {
// Create a new request to update our issue
let request = http.request(options, connection => {
let response = [];
// Keep appending any data we receive
connection.on("data", data => response.push(data));
// Print out the output at the end
connection.on("end", () => {
let message = Buffer.concat(response).toString();
console.log(message);
});
});
// Sort the array alphabetically
missing.sort();
// Go over all missing pages
for (let i in missing) {
// Add the missing page to our bullet list
issueBody += `* ${missing[i]} \n`;
// Replace the missing file with our "Missing page" placeholder text
fs.writeFile(`pages/${missing[i]}`, missingPageTemplate, err => {
if (err) {
throw err;
}
});
}
// A wholesome message at the end <3
issueBody += "\nHelp is much appreciated :heart:";
// Update the issue via a web request
request.write(JSON.stringify({
title: "Missing Pages: " + missing.length,
body: issueBody,
state: "open"
}));
request.end();
});
});
/**
* This method scans the given input for links to wiki pages.
*
* @param {string} content The file content
*/
function scanFile(content) {
let match;
// This scans the document for any linked pages.
// This continues as long as there are matches for our regular expression
do {
// Update our match variable
match = regex.exec(content);
// If we found a match, handle it
if (match) {
// We will crop out the url portion of the match
let page = match[0].substring(1 + url.length, match[0].length - 1);
// Start an attempt to find the .md file for the linked page
findFile(`${page}.md`);
}
} while(match);
}
/**
* This method attempts to find the given page.
* If the page could not be found, it will push it to the missing pages array.
*
* @param {string} page The page to find
*/
function findFile(page) {
// We don't wanna check a page we already visited.
if (pages.includes(page)) {
return;
}
// Add it to the pages array
pages.push(page);
// Check if a file for this page exists
if (fs.existsSync("pages/" + page)) {
console.log(`${page} : OK`);
}
else {
console.log(`${page} : MISSING`);
missing.push(page);
};
}