|
| 1 | +import * as ts from 'typescript'; |
| 2 | +import * as fsx from 'fs-extra'; |
| 3 | +import * as path from 'path'; |
| 4 | +import ApiPackage from './definitions/ApiPackage'; |
| 5 | +import DocItemLoader from './DocItemLoader'; |
| 6 | + |
| 7 | +export type ApiErrorHandler = (message: string, fileName: string, lineNumber: number) => void; |
| 8 | + |
| 9 | +/** |
| 10 | + * Options for Extractor contructor. |
| 11 | + */ |
| 12 | +export interface IExtractorOptions { |
| 13 | + /** |
| 14 | + * Configuration for the TypeScript compiler. The most important options to set are: |
| 15 | + * |
| 16 | + * - target: ts.ScriptTarget.ES5 |
| 17 | + * - module: ts.ModuleKind.CommonJS |
| 18 | + * - moduleResolution: ts.ModuleResolutionKind.NodeJs |
| 19 | + * - rootDir: inputFolder |
| 20 | + */ |
| 21 | + compilerOptions: ts.CompilerOptions; |
| 22 | + |
| 23 | + errorHandler?: ApiErrorHandler; |
| 24 | +} |
| 25 | + |
| 26 | +/** |
| 27 | + * Options for Extractor.analyze() |
| 28 | + */ |
| 29 | +export interface IApiAnalyzerOptions { |
| 30 | + /** |
| 31 | + * The entry point for the project. This should correspond to the "main" field |
| 32 | + * from NPM's package.json file. If it is a relative path, it will be relative to |
| 33 | + * the project folder described by IApiAnalyzerOptions.compilerOptions. |
| 34 | + */ |
| 35 | + entryPointFile: string; |
| 36 | + |
| 37 | + /** |
| 38 | + * This can be used to specify other files that should be processed by the TypeScript compiler |
| 39 | + * for some reason, e.g. a "typings/tsd.d.ts" file. It is NOT necessary to specify files that |
| 40 | + * are explicitly imported/required by the entryPointFile, since the compiler will trace |
| 41 | + * (the transitive closure of) ordinary dependencies. |
| 42 | + */ |
| 43 | + otherFiles?: string[]; |
| 44 | +} |
| 45 | + |
| 46 | +/** |
| 47 | + * The main entry point for the "api-extractor" utility. The Analyzer object invokes the |
| 48 | + * TypeScript Compiler API to analyze a project, and constructs the ApiItem |
| 49 | + * abstract syntax tree. |
| 50 | + */ |
| 51 | +export default class Extractor { |
| 52 | + public errorHandler: ApiErrorHandler; |
| 53 | + public typeChecker: ts.TypeChecker; |
| 54 | + public package: ApiPackage; |
| 55 | + /** |
| 56 | + * One DocItemLoader is needed per analyzer to look up external API members |
| 57 | + * as needed. |
| 58 | + */ |
| 59 | + public docItemLoader: DocItemLoader; |
| 60 | + |
| 61 | + private _compilerOptions: ts.CompilerOptions; |
| 62 | + |
| 63 | + /** |
| 64 | + * The default implementation of ApiErrorHandler, which merely writes to console.log(). |
| 65 | + */ |
| 66 | + public static defaultErrorHandler(message: string, fileName: string, lineNumber: number): void { |
| 67 | + console.log(`ERROR: [${fileName}:${lineNumber}] ${message}`); |
| 68 | + } |
| 69 | + |
| 70 | + constructor(options: IExtractorOptions) { |
| 71 | + this._compilerOptions = options.compilerOptions; |
| 72 | + this.docItemLoader = new DocItemLoader(options.compilerOptions.rootDir); |
| 73 | + this.errorHandler = options.errorHandler || Extractor.defaultErrorHandler; |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Analyzes the specified project. |
| 78 | + */ |
| 79 | + public analyze(options: IApiAnalyzerOptions): void { |
| 80 | + const rootFiles: string[] = [options.entryPointFile].concat(options.otherFiles || []); |
| 81 | + |
| 82 | + const program: ts.Program = ts.createProgram(rootFiles, this._compilerOptions); |
| 83 | + |
| 84 | + // This runs a full type analysis, and then augments the Abstract Syntax Tree (i.e. declarations) |
| 85 | + // with semantic information (i.e. symbols). The "diagnostics" are a subset of the everyday |
| 86 | + // compile errors that would result from a full compilation. |
| 87 | + for (const diagnostic of program.getSemanticDiagnostics()) { |
| 88 | + this.reportError('TypeScript: ' + diagnostic.messageText, diagnostic.file, diagnostic.start); |
| 89 | + } |
| 90 | + |
| 91 | + this.typeChecker = program.getTypeChecker(); |
| 92 | + |
| 93 | + const rootFile: ts.SourceFile = program.getSourceFile(options.entryPointFile); |
| 94 | + if (!rootFile) { |
| 95 | + throw new Error('Unable to load file: ' + options.entryPointFile); |
| 96 | + } |
| 97 | + |
| 98 | + this.package = new ApiPackage(this, rootFile); |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * Reports an error message to the registered ApiErrorHandler. |
| 103 | + */ |
| 104 | + public reportError(message: string, sourceFile: ts.SourceFile, start: number): void { |
| 105 | + const lineNumber: number = sourceFile.getLineAndCharacterOfPosition(start).line; |
| 106 | + this.errorHandler(message, sourceFile.fileName, lineNumber); |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * Scans for external package api files and loads them into the docItemLoader member before |
| 111 | + * any API analyzation begins. |
| 112 | + * |
| 113 | + * @param externalJsonCollectionPath - path to to the folder that contains all the external api json files. |
| 114 | + * Ex: if externalJsonPath is './resources', then in that folder |
| 115 | + * are 'es6-collections.api.json', 'es6-promise.api.json', etc. |
| 116 | + */ |
| 117 | + public loadExternalPackages(externalJsonCollectionPath: string): void { |
| 118 | + if (!externalJsonCollectionPath) { |
| 119 | + return; |
| 120 | + } |
| 121 | + |
| 122 | + const files: string[] = fsx.readdirSync(externalJsonCollectionPath); |
| 123 | + files.forEach(file => { |
| 124 | + const externalJsonFilePath: string = path.join(externalJsonCollectionPath, file); |
| 125 | + this.docItemLoader.loadPackageIntoCache(externalJsonFilePath); |
| 126 | + }); |
| 127 | + } |
| 128 | +} |
0 commit comments