forked from typescript-cheatsheets/react
-
Notifications
You must be signed in to change notification settings - Fork 0
/
genReadme.js
301 lines (300 loc) · 9.53 KB
/
genReadme.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
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
289
290
291
292
293
294
295
296
297
298
299
300
301
const { Octokit } = require("@octokit/rest");
const Toc = require("markdown-toc");
const Fm = require("front-matter");
const octokit = new Octokit({ auth: `token ${process.env.ENV_GITHUB_TOKEN}` });
const repo_info = process.env.CI_REPOSITORY.split("/");
const repo_details = {
owner: repo_info[0],
repo: repo_info[1],
};
const default_options = {
withKey: "title",
withToc: false,
showHeading: true,
headingLevel: 2,
tabLevel: 1,
prefix: "",
suffix: "",
};
async function getReadme() {
let res = await octokit.repos.getReadme(repo_details);
let encoded = res.data.content;
let decoded = Buffer.from(encoded, "base64").toString("utf8");
return {
content: decoded,
sha: res.data.sha,
};
}
(async function main() {
try {
let readme = await getReadme();
default_options["from"] = readme;
let initialContent = readme.content;
initialContent = await updateSectionWith({
name: "setup",
path: "docs/basic/setup.md",
to: initialContent,
withToc: true,
headingLevel: 1,
prefix: "Section 1: ",
});
initialContent = await updateSectionWith({
name: "basic-type-examples",
path: "docs/basic/getting-started/basic-type-examples.md",
to: initialContent,
});
initialContent = await updateSectionWith({
name: "function-components",
path: "docs/basic/getting-started/function-components.md",
to: initialContent,
});
initialContent = await updateSectionWith({
name: "hooks",
path: "docs/basic/getting-started/hooks.md",
to: initialContent,
});
initialContent = await updateSectionWith({
name: "class-components",
path: "docs/basic/getting-started/class-components.md",
to: initialContent,
});
initialContent = await updateSectionWith({
name: "default-props",
path: "docs/basic/getting-started/default-props.md",
to: initialContent,
showHeading: false,
});
initialContent = await updateSectionWith({
name: "forms-and-events",
path: "docs/basic/getting-started/forms-and-events.md",
to: initialContent,
});
initialContent = await updateSectionWith({
name: "context",
path: "docs/basic/getting-started/context.md",
to: initialContent,
});
initialContent = await updateSectionWith({
name: "forward-create-ref",
path: "docs/basic/getting-started/forward-create-ref.md",
to: initialContent,
});
initialContent = await updateSectionWith({
name: "portals",
path: "docs/basic/getting-started/portals.md",
to: initialContent,
});
initialContent = await updateSectionWith({
name: "error-boundaries",
path: "docs/basic/getting-started/error-boundaries.md",
to: initialContent,
});
initialContent = await updateSectionWith({
name: "concurrent",
path: "docs/basic/getting-started/concurrent.md",
to: initialContent,
});
initialContent = await updateSectionWith({
name: "types",
path: "docs/basic/troubleshooting/types.md",
to: initialContent,
withToc: true,
headingLevel: 1,
});
initialContent = await updateSectionWith({
name: "operators",
path: "docs/basic/troubleshooting/operators.md",
to: initialContent,
headingLevel: 1,
});
initialContent = await updateSectionWith({
name: "utilities",
path: "docs/basic/troubleshooting/utilities.md",
to: initialContent,
headingLevel: 1,
});
initialContent = await updateSectionWith({
name: "ts-config",
path: "docs/basic/troubleshooting/ts-config.md",
to: initialContent,
headingLevel: 1,
});
initialContent = await updateSectionWith({
name: "official-typings-bugs",
path: "docs/basic/troubleshooting/official-typings-bugs.md",
to: initialContent,
headingLevel: 1,
withKey: "sidebar_label",
prefix: "Troubleshooting Handbook: ",
});
initialContent = await updateSectionWith({
name: "non-ts-files",
path: "docs/basic/troubleshooting/non-ts-files.md",
to: initialContent,
headingLevel: 1,
});
initialContent = await updateSectionWith({
name: "non-ts-files",
path: "docs/basic/troubleshooting/learn-ts.md",
to: initialContent,
headingLevel: 1,
});
initialContent = await updateSectionWith({
name: "useful-hooks",
path: "docs/basic/useful-hooks.md",
to: initialContent,
headingLevel: 1,
});
initialContent = await updateSectionWith({
name: "editor-integration",
path: "docs/basic/editor-integration.md",
to: initialContent,
headingLevel: 1,
});
initialContent = await updateSectionWith({
name: "linting",
path: "docs/basic/linting.md",
to: initialContent,
headingLevel: 1,
});
initialContent = await updateSectionWith({
name: "resources",
path: "docs/basic/recommended/resources.md",
to: initialContent,
headingLevel: 1,
});
initialContent = await updateSectionWith({
name: "talks",
path: "docs/basic/recommended/talks.md",
to: initialContent,
headingLevel: 1,
});
initialContent = await updateSectionWith({
name: "codebases",
path: "docs/basic/recommended/codebases.md",
to: initialContent,
headingLevel: 1,
});
initialContent = await updateSectionWith({
name: "learn-ts",
path: "docs/basic/troubleshooting/learn-ts.md",
to: initialContent,
headingLevel: 1,
withKey: "sidebar_label",
});
initialContent = await updateSectionWith({
name: "examples",
path: "docs/basic/examples.md",
to: initialContent,
headingLevel: 1,
});
await octokit.repos.createOrUpdateFile({
...repo_details,
content: Buffer.from(initialContent).toString("base64"),
path: "README.md",
message: `Updated README on ${new Date().toISOString()}`,
sha: readme.sha,
branch: "main",
});
} catch (err) {
console.error(
`🚨 You've encountered a ${err.name} ➜ ${err.message} \n` +
`💡 ProTip ➜ Please ensure your credentials are up-to-date or the path to your file exists.`
);
console.error({ repo_details });
console.error(err);
}
})();
async function updateSectionWith(options) {
let update_options = Object.assign({}, { ...default_options }, options);
let md = await readContentFromPath(update_options.path);
let oldFences = getFenceForSection(update_options.from, update_options.name);
let fence_options = {
name: update_options.name,
content: md,
tabLevel: update_options.tabLevel,
headingLevel: update_options.headingLevel,
showHeading: update_options.showHeading,
withKey: update_options.withKey,
prefix: update_options.prefix,
suffix: update_options.suffix,
};
let newFences = generateContentForSection({
...fence_options,
withToc: false,
});
let oldTocFences = getFenceForSection(
update_options.from,
update_options.name,
(isToc = true)
);
let newTocFences = generateContentForSection({
...fence_options,
withToc: true,
});
let updatedContents = update_options.to.replace(oldFences.regex, newFences);
updatedContents = updatedContents.replace(oldTocFences.regex, newTocFences);
if (update_options.withToc)
console.log(
`✅ 🗜️ Rewrote Table of Contents for '${md.frontmatter.title}'`
);
console.log(`✅ 📝 Rewrote Section for '${md.frontmatter.title}'`);
return updatedContents;
}
async function readContentFromPath(relative_path) {
let MdDoc = await octokit.repos.getContents({
...repo_details,
path: relative_path,
});
let MdContent = Fm(Buffer.from(MdDoc.data.content, "base64").toString());
let TableOfContents = Toc(MdContent.body).content;
return {
frontmatter: MdContent.attributes,
body: MdContent.body,
toc: TableOfContents,
};
}
function generateContentForSection(options) {
let section_options = Object.assign({}, { ...default_options }, options);
let fence = getFence(section_options.name, section_options.withToc);
let fenceContent = fence.start + "\n";
if (section_options.withToc) {
let lines = section_options.content.toc.split("\n");
for (let i = 0, len = lines.length; i < len; i += 1)
fenceContent +=
" ".repeat(section_options.tabLevel) +
lines[i] +
(i !== len - 1 ? "\n" : "");
} else {
fenceContent += section_options.showHeading
? `${"#".repeat(section_options.headingLevel)} ` +
section_options.prefix +
section_options.content.frontmatter[section_options.withKey] +
section_options.suffix +
"\n\n"
: "";
fenceContent += section_options.content.body + "\n";
}
fenceContent += fence.end;
return fenceContent;
}
function getFenceForSection(readme, sectionName, isToc = false) {
try {
let fence = getFence(sectionName, isToc);
let regex = new RegExp(`(${fence.start}[\\s\\S]+${fence.end})`, "gm");
return { regex: regex, content: regex.exec(readme.content) };
} catch (err) {
console.error(
`🚨 You've encountered a ${err.name} ➜ ${err.message} \n` +
`💡 ProTip ➜ Please ensure the comments exist and are separated by a newline.`
);
console.error({ readme, sectionName });
console.error(err);
}
}
function getFence(sectionName, isToc = false) {
let name = isToc ? sectionName + "-toc" : sectionName;
let START_COMMENT = "<!--START-SECTION:" + name + "-->";
let END_COMMENT = "<!--END-SECTION:" + name + "-->";
return { start: START_COMMENT, end: END_COMMENT };
}