-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: update doc api to use generated doc
- Loading branch information
Orange Mi
committed
Jan 5, 2022
1 parent
58f8a3c
commit d5b0449
Showing
8 changed files
with
362 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
[ | ||
{ | ||
"name": "MongodbCollectionRecordStorageBllImpl", | ||
"documentation": "", | ||
"type": "typeof MongodbCollectionRecordStorageBllImpl", | ||
"constructors": [ | ||
{ | ||
"parameters": [ | ||
{ | ||
"name": "options", | ||
"documentation": "", | ||
"type": "{ dbClient?: MongoClient; }" | ||
} | ||
], | ||
"returnType": "MongodbCollectionRecordStorageBllImpl", | ||
"documentation": "" | ||
} | ||
] | ||
}, | ||
{ | ||
"name": "MongodbCollectionRecordQueryBllImpl", | ||
"documentation": "", | ||
"type": "typeof MongodbCollectionRecordQueryBllImpl", | ||
"constructors": [ | ||
{ | ||
"parameters": [ | ||
{ | ||
"name": "options", | ||
"documentation": "", | ||
"type": "{ dbClient?: MongoClient; }" | ||
} | ||
], | ||
"returnType": "MongodbCollectionRecordQueryBllImpl", | ||
"documentation": "" | ||
} | ||
] | ||
}, | ||
{ | ||
"name": "RecordBllImpl", | ||
"documentation": "", | ||
"type": "typeof RecordBllImpl", | ||
"constructors": [ | ||
{ | ||
"parameters": [ | ||
{ | ||
"name": "options", | ||
"documentation": "", | ||
"type": "{ recordStorageBll?: RecordStorageBll; recordQueryBll?: RecordQueryBll<any, any>; }" | ||
} | ||
], | ||
"returnType": "RecordBllImpl", | ||
"documentation": "" | ||
} | ||
] | ||
}, | ||
{ | ||
"name": "RecordAuthBll", | ||
"documentation": "", | ||
"type": "typeof RecordAuthBll", | ||
"constructors": [ | ||
{ | ||
"parameters": [], | ||
"returnType": "RecordAuthBll", | ||
"documentation": "" | ||
} | ||
] | ||
}, | ||
{ | ||
"name": "RecordAPI", | ||
"documentation": "Function2 2023423i", | ||
"type": "typeof RecordAPI", | ||
"constructors": [ | ||
{ | ||
"parameters": [ | ||
{ | ||
"name": "options", | ||
"documentation": "", | ||
"type": "{ recordBll?: RecordStorageBll & RecordQueryBll<any, any>; }" | ||
} | ||
], | ||
"returnType": "RecordAPI", | ||
"documentation": "" | ||
} | ||
] | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
import * as ts from "typescript" | ||
import * as fs from "fs" | ||
|
||
interface DocEntry { | ||
name?: string | ||
fileName?: string | ||
documentation?: string | ||
type?: string | ||
constructors?: DocEntry[] | ||
parameters?: DocEntry[] | ||
returnType?: string | ||
} | ||
|
||
/** Generate documentation for all classes in a set of .ts files */ | ||
function generateDocumentation( | ||
fileNames: string[], | ||
options: ts.CompilerOptions | ||
): void { | ||
// Build a program using the set of root file names in fileNames | ||
const program = ts.createProgram(fileNames, options) | ||
|
||
// Get the checker, we will use it to find more about classes | ||
const checker = program.getTypeChecker() | ||
const output: DocEntry[] = [] | ||
|
||
// Visit every sourceFile in the program | ||
for (const sourceFile of program.getSourceFiles()) { | ||
if (!sourceFile.isDeclarationFile) { | ||
// Walk the tree to search for classes | ||
ts.forEachChild(sourceFile, visit) | ||
} | ||
} | ||
|
||
// // print out the doc | ||
fs.writeFileSync("classes.json", JSON.stringify(output, undefined, 4)) | ||
|
||
return | ||
|
||
/** visit nodes finding exported classes */ | ||
function visit(node: ts.Node) { | ||
// Only consider exported nodes | ||
if (!isNodeExported(node)) { | ||
return | ||
} | ||
|
||
if (ts.isClassDeclaration(node) && node.name) { | ||
// This is a top level class, get its symbol | ||
const symbol = checker.getSymbolAtLocation(node.name) | ||
if (symbol) { | ||
output.push(serializeClass(symbol)) | ||
} | ||
// No need to walk any further, class expressions/inner declarations | ||
// cannot be exported | ||
} else if (ts.isModuleDeclaration(node)) { | ||
// This is a namespace, visit its children | ||
ts.forEachChild(node, visit) | ||
} | ||
} | ||
|
||
/** Serialize a symbol into a json object */ | ||
function serializeSymbol(symbol: ts.Symbol): DocEntry { | ||
return { | ||
name: symbol.getName(), | ||
documentation: ts.displayPartsToString(symbol.getDocumentationComment(checker)), | ||
type: checker.typeToString( | ||
checker.getTypeOfSymbolAtLocation(symbol, symbol.valueDeclaration!) | ||
) | ||
} | ||
} | ||
|
||
/** Serialize a class symbol information */ | ||
function serializeClass(symbol: ts.Symbol) { | ||
const details = serializeSymbol(symbol) | ||
|
||
// Get the construct signatures | ||
const constructorType = checker.getTypeOfSymbolAtLocation( | ||
symbol, | ||
symbol.valueDeclaration! | ||
) | ||
details.constructors = constructorType | ||
.getConstructSignatures() | ||
.map(serializeSignature) | ||
return details | ||
} | ||
|
||
/** Serialize a signature (call or construct) */ | ||
function serializeSignature(signature: ts.Signature) { | ||
return { | ||
parameters: signature.parameters.map(serializeSymbol), | ||
returnType: checker.typeToString(signature.getReturnType()), | ||
documentation: ts.displayPartsToString(signature.getDocumentationComment(checker)) | ||
} | ||
} | ||
|
||
/** True if this is visible outside this file, false otherwise */ | ||
function isNodeExported(node: ts.Node): boolean { | ||
return ( | ||
(ts.getCombinedModifierFlags(node as ts.Declaration) & ts.ModifierFlags.Export) !== 0 || | ||
(!!node.parent && node.parent.kind === ts.SyntaxKind.SourceFile) | ||
) | ||
} | ||
} | ||
|
||
generateDocumentation(process.argv.slice(2), { | ||
target: ts.ScriptTarget.ES5, | ||
module: ts.ModuleKind.CommonJS | ||
}) |
Oops, something went wrong.