|
1 | 1 | import { readFileSync } from "fs";
|
2 |
| -import { Component, RendererComponent } from "typedoc/dist/lib/output/components"; |
3 |
| -import { RendererEvent } from "typedoc/dist/lib/output/events"; |
| 2 | +import path from "path"; |
| 3 | +import { BindOption, Context, Converter, Logger, Options, Reflection, Renderer, RendererEvent } from "typedoc"; |
4 | 4 |
|
5 |
| -import { getCurrentClientDirectory } from "./utils"; |
| 5 | +import { isClientModel } from "./utils"; |
6 | 6 |
|
7 | 7 | /**
|
8 | 8 | * Correct the package name in the navigator.
|
9 | 9 | */
|
10 |
| -@Component({ name: "SdkClientRenameProject" }) |
11 |
| -export class SdkClientRenameProjectPlugin extends RendererComponent { |
| 10 | +export class SdkClientRenameProjectPlugin { |
12 | 11 | private projectName: string | undefined = undefined;
|
13 |
| - initialize() { |
14 |
| - this.listenTo(this.owner, { |
15 |
| - [RendererEvent.BEGIN]: this.onRenderedBegin, |
16 |
| - }); |
| 12 | + |
| 13 | + @BindOption("defaultServiceSuffix") |
| 14 | + readonly defaultServiceSuffix: string; |
| 15 | + |
| 16 | + constructor(public readonly options: Options, public readonly logger: Logger, private readonly renderer: Renderer) { |
| 17 | + this.renderer.application.converter.on(Converter.EVENT_CREATE_DECLARATION, this.onCreateDeclaration); |
| 18 | + this.renderer.on(Renderer.EVENT_BEGIN, this.onRendererBegin); |
17 | 19 | }
|
18 | 20 |
|
19 |
| - onRenderedBegin(event: RendererEvent) { |
| 21 | + /** |
| 22 | + * Triggered when the converter creates the declaration of a project. |
| 23 | + * |
| 24 | + * @param context The context object describing the current state the converter is in. |
| 25 | + */ |
| 26 | + private onCreateDeclaration = (_context: Context, reflection: Reflection) => { |
| 27 | + const defaultCommentTag = reflection.comment?.getTag("@default") || reflection.comment?.getTag("@defaultValue"); |
| 28 | + if (isClientModel(reflection) && reflection.name === "serviceId" && defaultCommentTag) { |
| 29 | + const defaultValue = defaultCommentTag.content[0]?.text?.replace(/"/g, ""); |
| 30 | + if (defaultValue && !this.projectName) { |
| 31 | + this.projectName = `${defaultValue} Client - ${this.defaultServiceSuffix}`; |
| 32 | + } |
| 33 | + } |
| 34 | + }; |
| 35 | + |
| 36 | + /** |
| 37 | + * Triggered when the renderer is starting. |
| 38 | + * |
| 39 | + * @param event The project the renderer is currently processing. |
| 40 | + */ |
| 41 | + private onRendererBegin = (event: RendererEvent) => { |
20 | 42 | if (!this.projectName) {
|
21 |
| - const clientDirectory = getCurrentClientDirectory(event); |
22 |
| - const metadataDir = clientDirectory.files.filter((sourceFile) => |
23 |
| - sourceFile.fileName.endsWith("/package.json") |
24 |
| - )?.[0]?.fullFileName; |
25 |
| - const { name } = metadataDir || JSON.parse(readFileSync(metadataDir).toString()); |
26 |
| - const serviceIdReflection = clientDirectory.directories.src.files |
27 |
| - ?.filter((sourceFile) => sourceFile.fileName.endsWith("/runtimeConfig.shared.ts"))?.[0] |
28 |
| - .reflections.filter((reflection) => reflection.name === "serviceId")?.[0]; |
29 |
| - this.projectName = serviceIdReflection /* serviceIdReflection.defaultValue looks like '"S3"' */ |
30 |
| - ? `${(serviceIdReflection as any).defaultValue.match(/"(.*)"/)[1]} Client - AWS SDK for JavaScript v3` |
31 |
| - : name; |
| 43 | + const children = Object.values(event.project.reflections).filter(isClientModel); |
| 44 | + const clientDirectory = path.dirname(path.dirname(children[0].sources[0].fullFileName)); |
| 45 | + const metadataPath = path.join(clientDirectory, "package.json"); |
| 46 | + const { name } = JSON.parse(readFileSync(metadataPath).toString()); |
| 47 | + this.projectName = name; |
32 | 48 | }
|
33 | 49 | event.project.name = this.projectName;
|
34 |
| - } |
| 50 | + }; |
35 | 51 | }
|
0 commit comments