(fileNames: string[], options: ts.CompilerOptions)
| 61 | } |
| 62 | |
| 63 | function generateDocumentation(fileNames: string[], options: ts.CompilerOptions): void { |
| 64 | // Build a program using the set of root file names in fileNames |
| 65 | const program = ts.createProgram(fileNames, options) |
| 66 | |
| 67 | // Get the checker, we will use it to find more about classes |
| 68 | const checker = program.getTypeChecker() |
| 69 | const globalVariables: VariableDefinition[] = [] |
| 70 | const globalFunctions: FunctionDefinition[] = [] |
| 71 | const typeDefinitions: TypeDefinition[] = [] |
| 72 | const typeAliases: TypeAlias[] = [] |
| 73 | |
| 74 | // Visit every sourceFile in the program |
| 75 | for (const sourceFile of program.getSourceFiles()) { |
| 76 | ts.forEachChild(sourceFile, visit) |
| 77 | } |
| 78 | |
| 79 | // print out the stuff we've collected |
| 80 | console.log('Writing public/static/generated/ts_global_variables.json') |
| 81 | fs.writeFileSync('public/static/generated/ts_global_variables.json', JSON.stringify(globalVariables, undefined, 4)) |
| 82 | |
| 83 | console.log('Writing public/static/generated/ts_global_functions.json') |
| 84 | fs.writeFileSync('public/static/generated/ts_global_functions.json', JSON.stringify(globalFunctions, undefined, 4)) |
| 85 | |
| 86 | console.log('Writing public/static/generated/ts_types.json') |
| 87 | fs.writeFileSync('public/static/generated/ts_types.json', JSON.stringify(typeDefinitions, undefined, 4)) |
| 88 | |
| 89 | console.log('Writing public/static/generated/ts_type_aliases.json') |
| 90 | fs.writeFileSync('public/static/generated/ts_type_aliases.json', JSON.stringify(typeAliases, undefined, 4)) |
| 91 | return |
| 92 | |
| 93 | function serializeDocumentation(documentation): string { |
| 94 | return documentation |
| 95 | .map((documentationPart) => { |
| 96 | if (documentationPart.kind === 'text') { |
| 97 | return documentationPart.text |
| 98 | } |
| 99 | return '' |
| 100 | }) |
| 101 | .join('\n') |
| 102 | } |
| 103 | |
| 104 | function visit(node: ts.Node) { |
| 105 | if (ts.isVariableStatement(node)) { |
| 106 | const varStatement = node as ts.VariableStatement |
| 107 | varStatement.declarationList.declarations.forEach((declaration) => { |
| 108 | const varName = (declaration.name as ts.Identifier).escapedText |
| 109 | if (allowedGlobals.includes(varName.toString())) { |
| 110 | globalVariables.push(serializeVariableDeclaration(declaration)) |
| 111 | const varType = checker.getTypeAtLocation(declaration.name) |
| 112 | serializeType(varType) |
| 113 | } |
| 114 | if (manualTypeVariables.includes(varName.toString())) { |
| 115 | const varType = checker.getTypeAtLocation(declaration.name) |
| 116 | serializeType(varType) |
| 117 | } |
| 118 | }) |
| 119 | } |
| 120 | if (ts.isFunctionDeclaration(node)) { |
no test coverage detected