import type { DeclarationReflection, ProjectReflection, SignatureReflection, } from 'typedoc'; import type { Method } from '../../docs/.vitepress/components/api-docs/method'; import { writeApiDocsModule } from './apiDocsWriter'; import { analyzeSignature } from './signature'; import { extractDeprecated, extractDescription, extractModuleFieldName, extractModuleName, selectApiMethodSignatures, selectApiModules, } from './typedoc'; import type { ModuleSummary } from './utils'; import { adjustUrls } from './utils'; /** * Analyzes and writes the documentation for modules and their methods such as `faker.animal.cat()`. * * @param project The project used to extract the modules. * @returns The generated pages. */ export function processModules(project: ProjectReflection): ModuleSummary[] { return selectApiModules(project).map(processModule); } /** * Analyzes and writes the documentation for a module and its methods such as `faker.animal.cat()`. * * @param module The module to process. * @returns The generated pages. */ function processModule(module: DeclarationReflection): ModuleSummary { const moduleName = extractModuleName(module); console.log(`Processing Module ${moduleName}`); const moduleFieldName = extractModuleFieldName(module); const { comment, deprecated } = analyzeModule(module); const methods = processModuleMethods(module, `faker.${moduleFieldName}.`); return writeApiDocsModule( moduleName, moduleFieldName, comment, deprecated, methods ); } /** * Analyzes the documentation for a class. * * @param module The class to process. * @returns The class information. */ export function analyzeModule(module: DeclarationReflection): { comment: string; deprecated: string | undefined; } { return { comment: adjustUrls(extractDescription(module)), deprecated: extractDeprecated(module), }; } /** * Processes all api methods of the given class. This does not include the constructor. * * @param module The module to process. * @param accessor The code used to access the methods within the module. * @returns A list containing the documentation for the api methods in the given module. */ export function processModuleMethods( module: DeclarationReflection, accessor: string ): Method[] { return processMethods(selectApiMethodSignatures(module), accessor); } /** * Processes all api methods. * * @param signatures The signatures to process. * @param accessor The code used to access the methods. * @returns A list containing the documentation for the api methods. */ export function processMethods( signatures: Record, accessor: string = '' ): Method[] { const methods: Method[] = []; for (const [methodName, signature] of Object.entries(signatures)) { console.debug(`- ${methodName}`); methods.push(analyzeSignature(signature, accessor, methodName)); } return methods; }