| 83 | ); |
| 84 | |
| 85 | async function addInterfaceProperties(filePath: string, name: string) { |
| 86 | const outputFile = resolve(outputDir, `${name}.json`); |
| 87 | const json = JSON.parse( |
| 88 | await readFile(outputFile, "utf-8") |
| 89 | ) as ImperativeHandleMetadata; |
| 90 | |
| 91 | const sourceFile = ts.createSourceFile( |
| 92 | filePath, |
| 93 | await readFile(filePath, "utf-8"), |
| 94 | ts.ScriptTarget.Latest, |
| 95 | true |
| 96 | ); |
| 97 | const interfaceDeclaration = sourceFile.statements.find( |
| 98 | (statement): statement is ts.InterfaceDeclaration => |
| 99 | ts.isInterfaceDeclaration(statement) && statement.name.text === name |
| 100 | ); |
| 101 | if (!interfaceDeclaration) { |
| 102 | throw Error(`Interface ${name} not found in ${filePath}`); |
| 103 | } |
| 104 | |
| 105 | // Keep the declaration order |
| 106 | const methods: ImperativeHandleMetadata["methods"] = []; |
| 107 | for (const member of interfaceDeclaration.members) { |
| 108 | const memberName = member.name?.getText(sourceFile) ?? ""; |
| 109 | if (ts.isPropertySignature(member) || ts.isGetAccessor(member)) { |
| 110 | const comment = ts |
| 111 | .getJSDocCommentsAndTags(member) |
| 112 | .filter(ts.isJSDoc) |
| 113 | .map((jsDoc) => ts.getTextOfJSDocComment(jsDoc.comment) ?? "") |
| 114 | .join("\n\n"); |
| 115 | if (!comment) { |
| 116 | throw Error(`Missing JSDoc comment for ${name}.${memberName}`); |
| 117 | } |
| 118 | |
| 119 | methods.push({ |
| 120 | description: await parseDescription(comment), |
| 121 | html: await syntaxHighlight(member.getText(sourceFile), "TS"), |
| 122 | name: memberName |
| 123 | }); |
| 124 | } else { |
| 125 | const method = json.methods.find((method) => method.name === memberName); |
| 126 | if (method) { |
| 127 | methods.push(method); |
| 128 | } |
| 129 | } |
| 130 | } |
| 131 | json.methods = methods; |
| 132 | |
| 133 | await writeFile(outputFile, JSON.stringify(json, null, 2)); |
| 134 | } |