aboutsummaryrefslogtreecommitdiff
path: root/scripts/apidoc/apiDocsWriter.ts
blob: 1282ad4c96aac9956c585b2bd6abca2ccc04dc70 (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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import { writeFileSync } from 'node:fs';
import { resolve } from 'node:path';
import type { Options } from 'prettier';
import { format } from 'prettier';
import prettierConfig from '../../.prettierrc.cjs';
import type { Method } from '../../docs/.vitepress/components/api-docs/method';
import type { PageIndex } from './utils';
import { pathDocsDir, pathOutputDir } from './utils';

const pathDocsApiPages = resolve(pathDocsDir, '.vitepress', 'api-pages.mjs');

const scriptCommand = 'pnpm run generate:api-docs';

// Moved here because this must not be formatted by prettier
const vitePressInFileOptions = `---
editLink: false
---

`;

const prettierMarkdown: Options = {
  ...prettierConfig,
  parser: 'markdown',
};

const prettierTypescript: Options = {
  ...prettierConfig,
  parser: 'typescript',
};

const prettierBabel: Options = {
  ...prettierConfig,
  parser: 'babel',
};

/**
 * Writes the api page for the given module to the correct location.
 *
 * @param moduleName The name of the module to write the docs for.
 * @param lowerModuleName The lowercase name of the module.
 * @param comment The module comments.
 */
export function writeApiDocsModulePage(
  moduleName: string,
  lowerModuleName: string,
  comment: string
): void {
  // Write api docs page
  let content = `
  <script setup>
  import ApiDocsMethod from '../.vitepress/components/api-docs/method.vue'
  import { ${lowerModuleName} } from './${lowerModuleName}'
  import { ref } from 'vue';

  const methods = ref(${lowerModuleName});
  </script>

  # ${moduleName}

  <!-- This file is automatically generated. -->
  <!-- Run '${scriptCommand}' to update -->

  ::: v-pre

  ${comment}

  :::

  <ul>
    <li v-for="method of methods" :key="method.name">
      <a :href="'#' + method.name">{{ method.title }}</a>
    </li>
  </ul>

  <ApiDocsMethod v-for="method of methods" :key="method.name" :method="method" v-once />
  `.replace(/\n +/g, '\n');

  content = vitePressInFileOptions + format(content, prettierMarkdown);

  writeFileSync(resolve(pathOutputDir, lowerModuleName + '.md'), content);
}

/**
 * Writes the api page for the given method to the correct location.
 *
 * @param methodName The name of the method to write the docs for.
 */
export function writeApiDocsDirectPage(methodName: string): void {
  let content = `
  <script setup>
  import ApiDocsMethod from '../.vitepress/components/api-docs/method.vue'
  import { ${methodName} } from './${methodName}'
  import { ref } from 'vue';

  const methods = ref(${methodName});
  </script>

  <ApiDocsMethod v-for="method of methods" :key="method.name" :method="method" v-once />
  `.replace(/\n +/g, '\n');

  content = vitePressInFileOptions + format(content, prettierMarkdown);

  writeFileSync(resolve(pathOutputDir, methodName + '.md'), content);
}

/**
 * Writes the api docs data to correct location.
 *
 * @param lowerModuleName The lowercase name of the module.
 * @param methods The methods data to save.
 */
export function writeApiDocsData(
  lowerModuleName: string,
  methods: Method[]
): void {
  let contentTs = `
import type { Method } from '../.vitepress/components/api-docs/method';

export const ${lowerModuleName}: Method[] = ${JSON.stringify(
    methods,
    null,
    2
  )}`;

  contentTs = format(contentTs, prettierTypescript);

  writeFileSync(resolve(pathOutputDir, lowerModuleName + '.ts'), contentTs);
}

/**
 * Writes the api docs index to correct location.
 *
 * @param pages The pages to write into the index.
 */
export function writeApiPagesIndex(pages: PageIndex): void {
  // Write api-pages.mjs
  console.log('Updating api-pages.mjs');
  pages.sort((a, b) => a.text.localeCompare(b.text));
  let apiPagesContent = `
    // This file is automatically generated.
    // Run '${scriptCommand}' to update
    export const apiPages = ${JSON.stringify(pages)};
    `.replace(/\n +/, '\n');

  apiPagesContent = format(apiPagesContent, prettierBabel);

  writeFileSync(pathDocsApiPages, apiPagesContent);
}