-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate-readme.js
60 lines (50 loc) · 2 KB
/
generate-readme.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
// Import necessary modules
const fs = require("fs");
const ejs = require("ejs");
// Define file paths
const tsDeclarationFile = "dist/index.d.ts";
const readmeTemplateFile = "read-me-template.md";
const readmeFile = "README.md";
const initialReadmeFile = "README.md";
// Read the TypeScript declaration file
fs.promises
.readFile(tsDeclarationFile, "utf8")
.then((data) => {
// Parse the TypeScript declaration file to extract function details
const functionDetails = parseTypeScriptDeclarationFile(data);
// Generate the function content based on the extracted details
const functionContent = functionDetails
.map((detail) => `\`${detail.name}\`: ${detail.description}\n\n`)
.join("");
// Render the README template with the generated content
return ejs.renderFile(readmeTemplateFile, { functionContent });
})
.then((readmeContent) =>
// Write the generated README content to the file
fs.promises.writeFile(readmeFile, readmeContent, "utf8")
)
.then(() => console.log("README.md updated successfully!"))
.catch((err) => {
console.error(err, "README.md failed to update");
// If there's an error, return the initial README.md file
return fs.promises
.readFile(initialReadmeFile, "utf8")
.then((initialReadmeContent) =>
fs.promises.writeFile(readmeFile, initialReadmeContent, "utf8")
)
.then(() => console.log("Initial README.md restored."));
});
// Parse the TypeScript declaration file and extract function details
function parseTypeScriptDeclarationFile(data) {
const functionRegex = /declare const (\w+):/g;
const descriptionRegex = /\/\*\*\n\s\* (.+?)\n\s\* @/g;
let functionDetails = [];
let match;
while ((match = functionRegex.exec(data)) !== null) {
const functionName = match[1];
const descriptionMatch = descriptionRegex.exec(data);
const description = descriptionMatch ? descriptionMatch[1].trim() : "";
functionDetails.push({ name: functionName, description });
}
return functionDetails;
}