forked from CopilotKit/CopilotKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdoc.ts
More file actions
94 lines (80 loc) · 3.16 KB
/
Copy pathdoc.ts
File metadata and controls
94 lines (80 loc) · 3.16 KB
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
import { AnnotatedDoc } from "./mdx";
import { SourceFile } from "./source";
import { Comments } from "./comments";
const fs = require("fs");
export class Documentation {
constructor(private readonly annotation: AnnotatedDoc) {}
async generate() {
const generatedDocumentation = await this.generatedDocs();
if (generatedDocumentation) {
fs.writeFileSync(this.annotation.path, generatedDocumentation);
console.log(
`Successfully autogenerated ${this.annotation.path} from ${this.annotation.sourcePath}`,
);
}
}
async generatedDocs(): Promise<string | null> {
const source = new SourceFile(this.annotation.sourcePath);
await source.parse();
const comment = Comments.getFirstCommentBlock(source.sourceFile);
if (!comment) {
console.warn(`No comment found for ${this.annotation.sourcePath}`);
console.warn("Skipping...");
return null;
}
const [description, ...rest] = comment.split("\n");
const remarks = rest.join("\n");
const arg0Interface = await source.getArg0Interface(this.annotation.name);
let result: string = "";
result += `---\n`;
if (this.annotation.type === "component") {
result += `title: "<${this.annotation.name} />"\n`;
} else {
result += `title: "${this.annotation.name}"\n`;
}
result += `description: "${description}"\n---\n\n`;
result += `{/* GENERATE-DOCS ${this.annotation.comment} */}\n`;
result += `${remarks}\n\n`;
if (this.annotation.type === "hook") {
result += `## Parameters\n\n`;
} else if (this.annotation.type === "component") {
result += `## Props\n\n`;
} else if (this.annotation.type === "class") {
result += `## Constructor\n\n`;
}
if (arg0Interface) {
for (const property of arg0Interface.properties) {
if (property.comment.includes("@deprecated")) {
continue;
}
result += `<ResponseField name="${property.name}" type="${property.type}" ${property.required ? "required" : ""}>\n`;
result += `${property.comment}\n`;
result += `</ResponseField>\n\n`;
}
} else if (this.annotation.type === "class") {
const constr = source.getConstructorDefinition(this.annotation.name);
if (constr) {
result += `## ${constr.signature}\n\n`;
result += `${constr.comment}\n\n`;
for (const param of constr.parameters) {
result += `<ResponseField name="${param.name}" type="${param.type}" ${param.required ? "required" : ""}>\n`;
result += `${param.comment}\n`;
result += `</ResponseField>\n\n`;
}
}
}
if (this.annotation.type === "class") {
const methodDefinitions = await source.getPublicMethodDefinitions(this.annotation.name);
for (const method of methodDefinitions) {
result += `## ${method.signature}\n\n`;
result += `${method.comment}\n\n`;
for (const param of method.parameters) {
result += `<ResponseField name="${param.name}" type="${param.type}" ${param.required ? "required" : ""}>\n`;
result += `${param.comment}\n`;
result += `</ResponseField>\n\n`;
}
}
}
return result;
}
}