From 5410239245b4a6fe8c1976f8aa33c970923f9f40 Mon Sep 17 00:00:00 2001 From: ST-DDT Date: Wed, 4 Oct 2023 18:13:00 +0200 Subject: feat: support custom randomizer (#2284) --- scripts/apidoc/fakerClass.ts | 46 +++++++++++++++++++++++++++----------------- scripts/apidoc/generate.ts | 3 ++- 2 files changed, 30 insertions(+), 19 deletions(-) (limited to 'scripts') diff --git a/scripts/apidoc/fakerClass.ts b/scripts/apidoc/fakerClass.ts index d7206cf8..ea190cc7 100644 --- a/scripts/apidoc/fakerClass.ts +++ b/scripts/apidoc/fakerClass.ts @@ -21,23 +21,33 @@ export async function processFakerClasses( return Promise.all(fakerClasses.map(processClass)); } +export async function processFakerRandomizer( + project: ProjectReflection +): Promise { + const randomizerClass = project + .getChildrenByKind(ReflectionKind.Interface) + .find((clazz) => clazz.name === 'Randomizer'); + + return processClass(randomizerClass); +} + async function processClass( - fakerClass: DeclarationReflection + clazz: DeclarationReflection ): Promise { - const { name } = fakerClass; - const moduleFieldName = extractModuleFieldName(fakerClass); + const { name } = clazz; + const moduleFieldName = extractModuleFieldName(clazz); console.log(`Processing ${name} class`); - const { comment, deprecated, examples } = analyzeModule(fakerClass); + const { comment, deprecated, examples } = analyzeModule(clazz); const methods: Method[] = []; - console.debug(`- constructor`); - methods.push(await processConstructor(fakerClass)); + if (hasConstructor(clazz)) { + console.debug(`- constructor`); + methods.push(await processConstructor(clazz)); + } - methods.push( - ...(await processModuleMethods(fakerClass, `${moduleFieldName}.`)) - ); + methods.push(...(await processModuleMethods(clazz, `${moduleFieldName}.`))); return writeApiDocsModule( name, @@ -49,20 +59,20 @@ async function processClass( ); } +function hasConstructor(clazz: DeclarationReflection): boolean { + return clazz + .getChildrenByKind(ReflectionKind.Constructor) + .some((constructor) => constructor.signatures.length > 0); +} + async function processConstructor( - fakerClass: DeclarationReflection + clazz: DeclarationReflection ): Promise { - const constructor = fakerClass.getChildrenByKind( - ReflectionKind.Constructor - )[0]; + const constructor = clazz.getChildrenByKind(ReflectionKind.Constructor)[0]; const signature = selectApiSignature(constructor); - const method = await analyzeSignature( - signature, - '', - `new ${fakerClass.name}` - ); + const method = await analyzeSignature(signature, '', `new ${clazz.name}`); return { ...method, diff --git a/scripts/apidoc/generate.ts b/scripts/apidoc/generate.ts index d1fefe8f..f047cc49 100644 --- a/scripts/apidoc/generate.ts +++ b/scripts/apidoc/generate.ts @@ -5,7 +5,7 @@ import { writeApiSearchIndex, writeSourceBaseUrl, } from './apiDocsWriter'; -import { processFakerClasses } from './fakerClass'; +import { processFakerClasses, processFakerRandomizer } from './fakerClass'; import { processFakerUtilities } from './fakerUtilities'; import { processModules } from './moduleMethods'; import { loadProject } from './typedoc'; @@ -27,6 +27,7 @@ export async function generate(): Promise { ...(await processModules(project)).sort((a, b) => a.text.localeCompare(b.text) ), + await processFakerRandomizer(project), processFakerUtilities(project), ]); await writeApiPagesIndex(pages.map(({ text, link }) => ({ text, link }))); -- cgit v1.2.3