Skip to content

Commit

Permalink
fix(core): exclude examples and spec files when parse api docs
Browse files Browse the repository at this point in the history
  • Loading branch information
why520crazy committed Apr 14, 2023
1 parent d355f31 commit bbd009b
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 13 deletions.
23 changes: 17 additions & 6 deletions packages/core/src/builders/library-component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ describe('#library-component', () => {
name: 'alib',
rootDir: 'alib',
include: ['common'],
examplesDir: 'examples',
exclude: '',
categories: [
{
Expand Down Expand Up @@ -425,9 +426,9 @@ describe('#library-component', () => {
properties: []
}
];
spectator.ngDocParseSpy
.withArgs(toolkit.path.getSystemPath(toolkit.path.resolve(buttonDirPath, '**/*.ts')))
.and.returnValue(parsedApiDocs);

const ngParseArguments = createNgParseArguments(buttonDirPath);
spectator.ngDocParseSpy.withArgs(...ngParseArguments).and.returnValue(parsedApiDocs);
await component.build();
spectator.assertCosmiconfigOptions(buttonDirPath);
const enApiDocs = component.getApiDocs('en-us');
Expand Down Expand Up @@ -478,9 +479,7 @@ describe('#library-component', () => {
properties: []
}
];
spectator.ngDocParseSpy
.withArgs(toolkit.path.getSystemPath(toolkit.path.resolve(buttonDirPath, '**/*.ts')))
.and.returnValue(parsedApiDocs);
spectator.ngDocParseSpy.withArgs(...createNgParseArguments(buttonDirPath)).and.returnValue(parsedApiDocs);
await component.build();
const enApiDocs = component.getApiDocs('en-us');
const zhApiDocs = component.getApiDocs('zh-cn');
Expand Down Expand Up @@ -549,3 +548,15 @@ async function expectFiles(host: fs.DocgeniFsHost, files: Record<string, string>
);
}
}

function createNgParseArguments(componentDir: string) {
return [
toolkit.path.getSystemPath(toolkit.path.resolve(componentDir, '**/*.ts')),
{
exclude: [
toolkit.path.getSystemPath(toolkit.path.resolve(componentDir, 'examples/**/*')),
toolkit.path.getSystemPath(toolkit.path.resolve(componentDir, '**/*.spec.ts'))
]
}
];
}
10 changes: 8 additions & 2 deletions packages/core/src/builders/library-component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,14 @@ export class LibraryComponentImpl extends FileEmitter implements LibraryComponen

private async buildApiDocs(): Promise<void> {
const apiSrcPath = toolkit.path.getSystemPath(resolve(this.absPath, `**/*.ts`));
const parseOptions = {
exclude: [
toolkit.path.getSystemPath(resolve(this.absPath, `${this.lib.examplesDir}/**/*`)),
toolkit.path.getSystemPath(resolve(this.absPath, `**/*.spec.ts`))
]
};
if (this.lib.apiMode === 'automatic') {
const apiDocs = this.lib.ngDocParser.parse(apiSrcPath) as ApiDeclaration[];
const apiDocs = this.lib.ngDocParser.parse(apiSrcPath, parseOptions) as ApiDeclaration[];
this.docgeni.config.locales.forEach(locale => {
this.localeApiDocsMap[locale.key] = apiDocs;
});
Expand All @@ -192,7 +198,7 @@ export class LibraryComponentImpl extends FileEmitter implements LibraryComponen
}
});
if (autoLocaleKeys && !toolkit.utils.isEmpty(autoLocaleKeys)) {
const apiAutoDocs = this.lib.ngDocParser.parse(apiSrcPath) as ApiDeclaration[];
const apiAutoDocs = this.lib.ngDocParser.parse(apiSrcPath, parseOptions) as ApiDeclaration[];
debug(`[${this.name}] apiSrcPath is ${apiSrcPath}, api docs length is ${apiAutoDocs.length}`, NAMESPACE);
autoLocaleKeys.forEach(key => {
apiDocs[key] = apiAutoDocs;
Expand Down
10 changes: 5 additions & 5 deletions packages/ngdoc/src/ng-parser.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ts } from './typescript';
import { toolkit, debug } from '@docgeni/toolkit';
import { toolkit, debug, fs } from '@docgeni/toolkit';
import {
NgDirectiveDoc,
NgPropertyDoc,
Expand Down Expand Up @@ -61,8 +61,8 @@ export class NgDocParser {
this.ngParserHost = options.ngParserHost ? options.ngParserHost : createNgParserHost(options);
}

public getSourceFiles(fileGlobs: string) {
const filePaths = toolkit.fs.globSync(fileGlobs);
public getSourceFiles(fileGlobs: string, options?: fs.GetDirsOrFilesOptions) {
const filePaths = toolkit.fs.globSync(fileGlobs, options);
debug(`fileGlobs: ${fileGlobs}, filePaths: ${filePaths.length}`, 'ng-parser');
const sourceFiles = filePaths
.map(filePath => {
Expand All @@ -79,8 +79,8 @@ export class NgDocParser {
return sourceFiles;
}

public parse(fileGlobs: string): NgEntryItemDoc[] {
const sourceFiles = this.getSourceFiles(fileGlobs);
public parse(fileGlobs: string, options?: fs.GetDirsOrFilesOptions): NgEntryItemDoc[] {
const sourceFiles = this.getSourceFiles(fileGlobs, options);
debug(`fileGlobs: ${fileGlobs}, sourceFiles: ${sourceFiles.length}`, 'ng-parser');
const checker = this.ngParserHost.program.getTypeChecker();

Expand Down

0 comments on commit bbd009b

Please sign in to comment.