import { IApiPackage, ApiItem, ApiJsonFile, IApiItemReference, IApiNameMap } from '@microsoft/api-extractor'; import { Parser } from './parser'; import { writeFileSync } from 'fs'; import {join} from 'path'; import * as mkdirp from 'mkdirp'; import * as rimraf from 'rimraf'; import dashify = require('dashify'); export class BootstrapParser extends Parser { private _apiPackages = new Map(); constructor( private _basePath: string, private _layout: string ){ super(); } public async loadFromFile(fileName: string): Promise { const apiFile = ApiJsonFile.loadFromFile(fileName); this._apiPackages.set(apiFile.name, apiFile); } async parse(): Promise{ for(const pack of Array.from(this._apiPackages.values())) { const apiPackage: IApiPackage = pack; rimraf.sync(this._basePath); mkdirp.sync(this._basePath); await this._genEntryFile(apiPackage); console.dir(apiPackage, {colors: true, depth: 1}); for(const exp in apiPackage.exports) { await this._genExportFile(apiPackage.name, exp, apiPackage.exports[exp]); } } await this._genIndexFile(); } private async _genIndexFile() { const entries = []; for(const pack of Array.from(this._apiPackages.values())) { const apiPackage: IApiPackage = pack; const members = []; for(const item in apiPackage.exports) { members.push({name: item, kind: apiPackage.exports[item].kind}); } entries.push({name: apiPackage.name, members, summary: apiPackage.summary}) } const result = `--- layout: ${this._layout} ---
${entries.map(e => { return `

${e.name}

${e.members.map(m => `
${this._getSmallKindBadge(m.kind)}${m.name}
`).join('')}
` }).join('')}
` const path = join(this._basePath, 'index.html'); writeFileSync(path, result) } private async _genEntryFile(apiPackage: IApiPackage) { // console.dir(apiPackage, {colors: true}); } private async _genExportFile(moduleName: string, name: string, exp: ApiItem) { const result = `--- layout: ${this._layout} ---
${await this._getContent(moduleName, name, exp)}
` const path = join(this._basePath, dashify(name) + '.html'); writeFileSync(path, result) } private async _getContent(moduleName: string, name: string, exp: ApiItem) { const members = []; for(const item in exp.members) { members.push({name: item, item: exp.members[item]}); } return `

${name}${this._getKindBadge(exp.kind)}${this._getBetaBadge(exp.isBeta)}

${ this._getLead(exp.extends, exp.implements)}
npm Package ${moduleName}
module import { ${name} } from "${moduleName}"

Overview

class ${name} {
  ${members.map(item => `${item.item.signature}`).join('\n  ')}
}

Members

${members.map(item => `

${item.name}${this._getBetaBadge(item.item.isBeta)}

${item.item.signature}
`).join('')} ` } private _getBetaBadge(isBeta: boolean) { return !!isBeta ? ' Beta' : ''; } private _getLead(ext: string, imp: string) { return ext || imp ? `

${ext ? 'extends: ' + ext : ''} ${imp ? 'implements: ' + imp : ''}

`: '' } private _getKindBadge(kind: string) { switch(kind) { case 'interface': return ' INTERFACE '; case 'class': return ' CLASS '; case 'function': return ' FUNCTION '; default: throw new Error('UnknownKind: ' + kind); } } private _getSmallKindBadge(kind: string) { switch(kind) { case 'interface': return 'I '; case 'class': return 'C '; case 'function': return 'F '; default: throw new Error('UnknownKind: ' + kind); } } }