11import * as ts from 'typescript' ;
2+ import * as fsx from 'fs-extra' ;
3+ import * as path from 'path' ;
24import ApiPackage from './definitions/ApiPackage' ;
35import DocItemLoader from './DocItemLoader' ;
46
57export type ApiErrorHandler = ( message : string , fileName : string , lineNumber : number ) => void ;
68
79/**
8- * Options for Analyzer.analyze()
9- */
10- export interface IApiAnalyzerOptions {
10+ * Options for Extractor contructor.
11+ */
12+ export interface IExtractorOptions {
1113 /**
1214 * Configuration for the TypeScript compiler. The most important options to set are:
1315 *
@@ -18,10 +20,17 @@ export interface IApiAnalyzerOptions {
1820 */
1921 compilerOptions : ts . CompilerOptions ;
2022
23+ errorHandler ?: ApiErrorHandler ;
24+ }
25+
26+ /**
27+ * Options for Extractor.analyze()
28+ */
29+ export interface IExtractorAnalyzeOptions {
2130 /**
2231 * The entry point for the project. This should correspond to the "main" field
2332 * from NPM's package.json file. If it is a relative path, it will be relative to
24- * the project folder described by IApiAnalyzerOptions .compilerOptions.
33+ * the project folder described by IExtractorAnalyzeOptions .compilerOptions.
2534 */
2635 entryPointFile : string ;
2736
@@ -39,7 +48,7 @@ export interface IApiAnalyzerOptions {
3948 * TypeScript Compiler API to analyze a project, and constructs the ApiItem
4049 * abstract syntax tree.
4150 */
42- export default class Analyzer {
51+ export default class Extractor {
4352 public errorHandler : ApiErrorHandler ;
4453 public typeChecker : ts . TypeChecker ;
4554 public package : ApiPackage ;
@@ -49,25 +58,28 @@ export default class Analyzer {
4958 */
5059 public docItemLoader : DocItemLoader ;
5160
61+ private _compilerOptions : ts . CompilerOptions ;
62+
5263 /**
5364 * The default implementation of ApiErrorHandler, which merely writes to console.log().
5465 */
5566 public static defaultErrorHandler ( message : string , fileName : string , lineNumber : number ) : void {
5667 console . log ( `ERROR: [${ fileName } :${ lineNumber } ] ${ message } ` ) ;
5768 }
5869
59- constructor ( errorHandler ?: ApiErrorHandler ) {
60- this . errorHandler = errorHandler || Analyzer . defaultErrorHandler ;
70+ constructor ( options : IExtractorOptions ) {
71+ this . _compilerOptions = options . compilerOptions ;
72+ this . docItemLoader = new DocItemLoader ( options . compilerOptions . rootDir ) ;
73+ this . errorHandler = options . errorHandler || Extractor . defaultErrorHandler ;
6174 }
6275
6376 /**
6477 * Analyzes the specified project.
6578 */
66- public analyze ( options : IApiAnalyzerOptions ) : void {
67- this . docItemLoader = new DocItemLoader ( options . compilerOptions . rootDir ) ;
79+ public analyze ( options : IExtractorAnalyzeOptions ) : void {
6880 const rootFiles : string [ ] = [ options . entryPointFile ] . concat ( options . otherFiles || [ ] ) ;
6981
70- const program : ts . Program = ts . createProgram ( rootFiles , options . compilerOptions ) ;
82+ const program : ts . Program = ts . createProgram ( rootFiles , this . _compilerOptions ) ;
7183
7284 // This runs a full type analysis, and then augments the Abstract Syntax Tree (i.e. declarations)
7385 // with semantic information (i.e. symbols). The "diagnostics" are a subset of the everyday
@@ -93,4 +105,27 @@ export default class Analyzer {
93105 const lineNumber : number = sourceFile . getLineAndCharacterOfPosition ( start ) . line ;
94106 this . errorHandler ( message , sourceFile . fileName , lineNumber ) ;
95107 }
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 - an absolute path to to the folder that contains all the external
114+ * api json files.
115+ * Ex: if externalJsonPath is './resources', then in that folder
116+ * are 'es6-collections.api.json', 'es6-promise.api.json', etc.
117+ */
118+ public loadExternalPackages ( externalJsonCollectionPath : string ) : void {
119+ if ( ! externalJsonCollectionPath ) {
120+ return ;
121+ }
122+
123+ const files : string [ ] = fsx . readdirSync ( externalJsonCollectionPath ) ;
124+ files . forEach ( file => {
125+ if ( path . extname ( file ) === '.json' ) {
126+ const externalJsonFilePath : string = path . join ( externalJsonCollectionPath , file ) ;
127+ this . docItemLoader . loadPackageIntoCache ( externalJsonFilePath ) ;
128+ }
129+ } ) ;
130+ }
96131}
0 commit comments