aboutsummaryrefslogtreecommitdiff
path: root/scripts/apidoc/directMethods.ts
blob: 616226ee5228b15f559fab27c880cef1cb5bd188 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import * as TypeDoc from 'typedoc';
import { writeApiDocsData, writeApiDocsDirectPage } from './apiDocsWriter';
import { analyzeSignature } from './signature';
import type { Page, PageIndex } from './utils';

/**
 * Analyzes and writes the documentation for direct methods such as `faker.fake()`.
 *
 * @param project The project used to extract the direct methods.
 * @returns The generated pages.
 */
export function processDirectMethods(
  project: TypeDoc.ProjectReflection
): PageIndex {
  const pages: PageIndex = [];

  const directs = project
    .getChildrenByKind(TypeDoc.ReflectionKind.Class)
    .filter((ref) => ref.name === 'Faker')[0]
    .getChildrenByKind(TypeDoc.ReflectionKind.Property)
    .filter((ref) => ['fake', 'unique'].includes(ref.name));

  for (const direct of directs) {
    pages.push(processDirectMethod(direct));
  }

  return pages;
}

/**
 * Analyzes and writes the documentation for a direct method such as `faker.fake()`.
 *
 * @param direct The direct method to process.
 * @returns The generated pages.
 */
export function processDirectMethod(
  direct: TypeDoc.DeclarationReflection
): Page {
  const methodName = direct.name;
  const upperMethodName =
    methodName.substring(0, 1).toUpperCase() + methodName.substring(1);
  console.log(`Processing Direct: ${upperMethodName}`);

  const signature = (direct.type as TypeDoc.ReflectionType).declaration
    .signatures[0];

  writeApiDocsDirectPage(methodName);
  writeApiDocsData(methodName, [
    analyzeSignature(signature, undefined, methodName),
  ]);

  return {
    text: upperMethodName,
    link: `/api/${methodName}.html`,
  };
}