-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathindex.mjs
More file actions
77 lines (62 loc) · 2.06 KB
/
index.mjs
File metadata and controls
77 lines (62 loc) · 2.06 KB
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
'use strict';
import { writeFile } from 'node:fs/promises';
import { join } from 'node:path';
import { groupNodesByModule } from '../../utils/generators.mjs';
import { createSectionBuilder } from './utils/buildSection.mjs';
/**
* This generator is responsible for generating the legacy JSON files for the
* legacy API docs for retro-compatibility. It is to be replaced while we work
* on the new schema for this file.
*
* This is a top-level generator, intaking the raw AST tree of the api docs.
* It generates JSON files to the specified output directory given by the
* config.
*
* @typedef {Array<ApiDocMetadataEntry>} Input
*
* @type {GeneratorMetadata<Input, import('./types.d.ts').Section[]>}
*/
export default {
name: 'legacy-json',
version: '1.0.0',
description: 'Generates the legacy version of the JSON API docs.',
dependsOn: 'ast',
/**
* Generates a legacy JSON file.
*
* @param {Input} input
* @param {Partial<GeneratorOptions>} options
*/
async generate(input, { output }) {
const buildSection = createSectionBuilder();
// This array holds all the generated values for each module
const generatedValues = [];
const groupedModules = groupNodesByModule(input);
// Gets the first nodes of each module, which is considered the "head"
const headNodes = input.filter(node => node.heading.depth === 1);
/**
* @param {ApiDocMetadataEntry} head
* @returns {import('./types.d.ts').ModuleSection}
*/
const processModuleNodes = head => {
const nodes = groupedModules.get(head.api);
const section = buildSection(head, nodes);
generatedValues.push(section);
return section;
};
await Promise.all(
headNodes.map(async node => {
// Get the json for the node's section
const section = processModuleNodes(node);
// Write it to the output file
if (output) {
await writeFile(
join(output, `${node.api}.json`),
JSON.stringify(section)
);
}
})
);
return generatedValues;
},
};