Skip to content
This repository has been archived by the owner on Mar 10, 2024. It is now read-only.

#12 Does not work with secondary entrypoints without a package.json #13

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions libs/multi-entrypoint/one/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# @multi/one

Secondary entry point of `@multi`. It can be used by importing from `@multi/one`.
5 changes: 5 additions & 0 deletions libs/multi-entrypoint/one/ng-package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"lib": {
"entryFile": "src/index.ts"
}
}
3 changes: 3 additions & 0 deletions libs/multi-entrypoint/one/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

export * from './lib/one.module';

7 changes: 7 additions & 0 deletions libs/multi-entrypoint/one/src/lib/one.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';

@NgModule({
imports: [CommonModule],
})
export class OneModule {}
2 changes: 1 addition & 1 deletion libs/multi-entrypoint/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "@multi",
"name": "@multi/test",
"version": "0.0.1",
"peerDependencies": {
"@angular/common": "^13.2.0",
Expand Down
8 changes: 7 additions & 1 deletion libs/multi-entrypoint/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,13 @@
"options": {
"lintFilePatterns": [
"libs/multi-entrypoint/src/**/*.ts",
"libs/multi-entrypoint/src/**/*.html"
"libs/multi-entrypoint/src/**/*.html",
"libs/multi-entrypoint/one/**/*.ts",
"libs/multi-entrypoint/one/**/*.html",
"libs/multi-entrypoint/two/**/*.ts",
"libs/multi-entrypoint/two/**/*.html",
"libs/multi-entrypoint/three/**/*.ts",
"libs/multi-entrypoint/three/**/*.html"
]
}
},
Expand Down
3 changes: 3 additions & 0 deletions libs/multi-entrypoint/three/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# @multi/three

Secondary entry point of `@multi`. It can be used by importing from `@multi/three`.
5 changes: 5 additions & 0 deletions libs/multi-entrypoint/three/ng-package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"lib": {
"entryFile": "src/index.ts"
}
}
3 changes: 3 additions & 0 deletions libs/multi-entrypoint/three/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

export * from './lib/three.module';

7 changes: 7 additions & 0 deletions libs/multi-entrypoint/three/src/lib/three.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';

@NgModule({
imports: [CommonModule],
})
export class ThreeModule {}
3 changes: 3 additions & 0 deletions libs/multi-entrypoint/two/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# @multi/two

Secondary entry point of `@multi`. It can be used by importing from `@multi/two`.
5 changes: 5 additions & 0 deletions libs/multi-entrypoint/two/ng-package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"lib": {
"entryFile": "src/index.ts"
}
}
3 changes: 3 additions & 0 deletions libs/multi-entrypoint/two/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

export * from './lib/two.module';

7 changes: 7 additions & 0 deletions libs/multi-entrypoint/two/src/lib/two.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';

@NgModule({
imports: [CommonModule],
})
export class TwoModule {}
3 changes: 2 additions & 1 deletion libs/ngx-doc-gen/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@
"fast-glob": "^3.2.11",
"fs-extra": "^10.1.0",
"highlight.js": "^11.5.1",
"jsonc-parser": "^3.0.0"
"jsonc-parser": "^3.0.0",
"ng-packagr": ">= 13.0.0"
},
"ng-add": {
"save": "devDependencies"
Expand Down
25 changes: 15 additions & 10 deletions libs/ngx-doc-gen/src/generate/dgeni-engine.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Dgeni, Package } from 'dgeni';
import { ReadTypeScriptModules } from 'dgeni-packages/typescript/processors/readTypeScriptModules';
import { TsParser } from 'dgeni-packages/typescript/services/TsParser';
import { discoverPackages } from 'ng-packagr/lib/ng-package/discover-packages';
import * as path from 'canonical-path';

import { HighlightNunjucksExtension } from '../extensions/nunjucks';
import { NgPackage } from '../interfaces/ng-package';
import { AsyncFunctionsProcessor } from '../processors/async-functions';
import { categorizer } from '../processors/categorizer';
import { DocsPrivateFilter } from '../processors/docs-private-filter';
Expand All @@ -15,18 +15,24 @@ import { MergeInheritedProperties, mergeInheritedProperties } from '../processor
import { ResolveInheritedDocs, resolveInheritedDocs } from '../processors/resolve-inherited-docs';
import { NgxDocGenOptions } from '../schema/ngx-doc-gen.options';
import { LogLevel } from '../types/log-level';
import { discoverNgPackage } from '../utils/discovery';
import { collectEntrypoints, createDgeniPackage } from '../utils/package-utils';
import { patchLogService } from '../utils/patch-log-service';
import { NgPackage } from 'ng-packagr/lib/ng-package/package';

export const generate = async (options: NgxDocGenOptions, workingDirectory: string, projectRoot: string): Promise<string> => {
const ngPackage = await discoverNgPackage(workingDirectory, projectRoot);
let projectPath = path.join(workingDirectory, projectRoot);
projectPath = path.isAbsolute(projectPath) ? projectPath : path.resolve(projectPath);

const ngPackage = await discoverPackages({
project: projectPath
});

return await generateDocumentation(ngPackage, workingDirectory, options);
};

const generateDocumentation = async (ngPackge: NgPackage, workingDirectory: string, options: NgxDocGenOptions): Promise<string> => {
const outputDir = path.join(workingDirectory, options.outputPath);
const dgeniPackage = createDgeniPackage(ngPackge.packageName);
const outputDir = path.join(workingDirectory, options.outputPath)
const dgeniPackage = createDgeniPackage(ngPackge.primary.moduleId);

setProcessors(dgeniPackage);

Expand All @@ -42,7 +48,7 @@ const generateDocumentation = async (ngPackge: NgPackage, workingDirectory: stri

return await new Dgeni([dgeniPackage])
.generate()
.then(() => ngPackge.packageName);
.then(() => ngPackge.primary.moduleId);
};

/**
Expand Down Expand Up @@ -194,7 +200,7 @@ const configureTypeScriptModule = (
writeFilesProcessor: any,
readFilesProcessor: any) {

const packagePath = ngPackage.basePath;
const packagePath = ngPackage.src;
// Set the base path for the "readFilesProcessor" to the execroot. This is necessary because
// otherwise the "writeFilesProcessor" is not able to write to the specified output path.
readFilesProcessor.basePath = workingDirectory;
Expand All @@ -210,11 +216,10 @@ const configureTypeScriptModule = (

const packageEntrypoints = collectEntrypoints(ngPackage);
packageEntrypoints.forEach(ep => {
const entrypointIndexPath = `${ep.basePath}/${ep.ngPackageJson.lib?.entryFile}`;
const entrypointIndexPath = `${ep.entryFilePath}`;

entryPointGrouper.entryPoints.push(ep);

tsParser.options.paths[`${ep.packageJson.name}`] = [entrypointIndexPath];
tsParser.options.paths[`${ep.moduleId}`] = [entrypointIndexPath];
readTypeScriptModules.sourceFiles.push(entrypointIndexPath);
});

Expand Down
9 changes: 0 additions & 9 deletions libs/ngx-doc-gen/src/interfaces/ng-entrypoint.ts

This file was deleted.

6 changes: 0 additions & 6 deletions libs/ngx-doc-gen/src/interfaces/ng-package-json.ts

This file was deleted.

9 changes: 0 additions & 9 deletions libs/ngx-doc-gen/src/interfaces/ng-package.ts

This file was deleted.

7 changes: 0 additions & 7 deletions libs/ngx-doc-gen/src/interfaces/package-json.ts

This file was deleted.

89 changes: 39 additions & 50 deletions libs/ngx-doc-gen/src/processors/entry-point-grouper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import { ConstExportDoc } from 'dgeni-packages/typescript/api-doc-types/ConstExp
import { FunctionExportDoc } from 'dgeni-packages/typescript/api-doc-types/FunctionExportDoc';
import { InterfaceExportDoc } from 'dgeni-packages/typescript/api-doc-types/InterfaceExportDoc';
import { TypeAliasExportDoc } from 'dgeni-packages/typescript/api-doc-types/TypeAliasExportDoc';
import { computeApiDocumentUrl } from '../common/compute-api-url';
import { NgEntryPoint } from 'ng-packagr/lib/ng-package/entry-point/entry-point';
import * as path from 'canonical-path';

import { isDeprecatedDoc, isPrimaryExportDoc } from '../common/decorators';
import { CategorizedClassDoc } from '../common/dgeni-definitions';
import { NgEntrypoint } from '../interfaces/ng-entrypoint';

export interface ModuleInfo {
/** Name of the module (e.g. toolbar, drag-drop, ripple, slider-testing) */
Expand Down Expand Up @@ -91,69 +92,64 @@ export class EntryPointGrouper implements Processor {
name = 'entryPointGrouper';
$runBefore = ['docs-processed'];
$runAfter = ['docs-private-filter'];
entryPoints: NgEntrypoint[] = [];
entryPoints: NgEntryPoint[] = [];

$process(docs: DocCollection) {
const entryPoints = new Map<string, EntryPointDoc>();
const entryPointsDocs = new Map<string, EntryPointDoc>();

docs.forEach(doc => {
const moduleInfo = this._getModulePackageInfo(doc);

const packageName = moduleInfo.packageName;
// const packageDisplayName = packageName === 'cdk' ? 'CDK' : 'Material';
const docEntryPoint = this._getDocumentEntryPoint(doc);

const moduleImportPath = `${packageName}/${moduleInfo.entryPointName}`;
const entryPointName = packageName + '-' + moduleInfo.name;
const entryPointName = docEntryPoint.flatModuleFile;

// Compute a public URL that refers to the document. This is helpful if we want to
// make references to other API documents. e.g. showing the extended class.
doc.publicUrl = computeApiDocumentUrl(doc, moduleInfo);
// TODO
// doc.publicUrl = computeApiDocumentUrl(doc, docEntryPoint);

// Get the entry-point for this doc, or, if one does not exist, create it.
let entryPoint: EntryPointDoc;
if (entryPoints.has(entryPointName)) {
entryPoint = entryPoints.get(entryPointName);
let entryPointDoc: EntryPointDoc;
if (entryPointsDocs.has(entryPointName)) {
entryPointDoc = entryPointsDocs.get(entryPointName);
} else {
entryPoint = new EntryPointDoc(entryPointName);
entryPoints.set(entryPointName, entryPoint);
entryPointDoc = new EntryPointDoc(entryPointName);
entryPointsDocs.set(entryPointName, entryPointDoc);
}

entryPoint.displayName = moduleInfo.name;
entryPoint.moduleImportPath = moduleImportPath;
entryPoint.packageName = packageName;
// entryPoint.packageDisplayName = packageName;
entryPointDoc.displayName = docEntryPoint.moduleId;
entryPointDoc.moduleImportPath = docEntryPoint.moduleId;

// Put this doc into the appropriate list in the entry-point doc.
if (doc.isDirective) {
entryPoint.directives.push(doc);
entryPointDoc.directives.push(doc);
} else if (doc.isService) {
entryPoint.services.push(doc);
entryPointDoc.services.push(doc);
} else if (doc.isNgModule) {
entryPoint.exportedNgModules.push(doc);
entryPointDoc.exportedNgModules.push(doc);
} else if (doc.docType === 'class') {
entryPoint.classes.push(doc);
entryPointDoc.classes.push(doc);
if (doc.isTestHarness) {
entryPoint.testHarnesses.push(doc);
entryPointDoc.testHarnesses.push(doc);
}
} else if (doc.docType === 'interface') {
entryPoint.interfaces.push(doc);
entryPointDoc.interfaces.push(doc);
} else if (doc.docType === 'type-alias') {
entryPoint.typeAliases.push(doc);
entryPointDoc.typeAliases.push(doc);
} else if (doc.docType === 'function') {
entryPoint.functions.push(doc);
entryPointDoc.functions.push(doc);
} else if (doc.docType === 'const') {
entryPoint.constants.push(doc);
entryPointDoc.constants.push(doc);
}

if (isPrimaryExportDoc(doc)) {
entryPoint.primaryExportName = doc.name;
entryPointDoc.primaryExportName = doc.name;
}
});

// For each entry-point where no explicit primary export has been specified
// through the "@docs-primary-export" tag, we determine a primary export by
// looking for possible "NgModule" classes or test harnesses.
entryPoints.forEach(entryPoint => {
entryPointsDocs.forEach(entryPoint => {
if (entryPoint.primaryExportName !== null) {
return;
}
Expand All @@ -169,7 +165,7 @@ export class EntryPointGrouper implements Processor {
}
});

return Array.from(entryPoints.values());
return Array.from(entryPointsDocs.values());
}

/**
Expand All @@ -189,32 +185,25 @@ export class EntryPointGrouper implements Processor {
return null;
}

/** Resolves module package information of the given Dgeni document. */
private _getModulePackageInfo(doc: Document): ModuleInfo {
// Full path to the file for this doc.
/**
* Resolves entrypoint of the given Dgeni document.
*/
private _getDocumentEntryPoint(doc: Document): NgEntryPoint | never {
const basePath: string = doc.fileInfo.basePath;
const foundEntryPoint = this._findMatchingEntryPoint(basePath);
const filePath: string = doc.fileInfo.filePath;
const relativeFilePath = path.relative(basePath, filePath).replace(/\\/g, '/');
const foundEntryPoint = this._findMatchingEntryPoint(relativeFilePath);

if (!foundEntryPoint) {
throw Error(`Could not determine entry-point for: ${doc.name} in ${basePath}`);
}

return {
name: foundEntryPoint.entrypointName,
packageName: foundEntryPoint.packageJson.name,
entryPointName: foundEntryPoint.entrypointName,
};
return foundEntryPoint;
}

/** Finds the matching entry-point of the given file path. */
private _findMatchingEntryPoint(basePath: string): NgEntrypoint | null {
let foundEntryPoint: NgEntrypoint | null = null;
for (const entryPoint of this.entryPoints) {
if (entryPoint.basePath !== basePath) {
continue;
}
foundEntryPoint = entryPoint;
}
return foundEntryPoint;
private _findMatchingEntryPoint(relativeFilePath: string): NgEntryPoint | null {
return this.entryPoints.find(ep => relativeFilePath.startsWith(path.basename(ep.basePath)))
|| this.entryPoints.find(ep => ep.isSecondaryEntryPoint === false) || null;
}
}
Loading