-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathindex.mjs
More file actions
91 lines (73 loc) · 2.64 KB
/
index.mjs
File metadata and controls
91 lines (73 loc) · 2.64 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
'use strict';
import { writeFile } from 'node:fs/promises';
import { join } from 'node:path';
import { createSectionBuilder } from './utils/buildSection.mjs';
import { groupNodesByModule } from '../../utils/generators.mjs';
const buildSection = createSectionBuilder();
/**
* 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
* @typedef {Array<import('./types.d.ts').Section>} Output
*
* @type {GeneratorMetadata<Input, Output>}
*/
export default {
name: 'legacy-json',
version: '1.0.0',
description: 'Generates the legacy version of the JSON API docs.',
dependsOn: 'metadata',
/**
* Process a chunk of items in a worker thread.
* Builds JSON sections - FS operations happen in generate().
*
* Each item is pre-grouped {head, nodes} - no need to
* recompute groupNodesByModule for every chunk.
*
* @param {Array<{ head: ApiDocMetadataEntry, nodes: Array<ApiDocMetadataEntry> }>} slicedInput - Pre-sliced module data
* @param {number[]} itemIndices - Indices into the sliced array
* @returns {Promise<Output>} JSON sections for each processed module
*/
async processChunk(slicedInput, itemIndices) {
const results = [];
for (const idx of itemIndices) {
const { head, nodes } = slicedInput[idx];
results.push(buildSection(head, nodes));
}
return results;
},
/**
* Generates a legacy JSON file.
*
* @param {Input} input
* @param {Partial<GeneratorOptions>} options
* @returns {AsyncGenerator<Output>}
*/
async *generate(input, { output, worker }) {
const groupedModules = groupNodesByModule(input);
const headNodes = input.filter(node => node.heading.depth === 1);
// Create sliced input: each item contains head + its module's entries
// This avoids sending all 4900+ entries to every worker
const entries = headNodes.map(head => ({
head,
nodes: groupedModules.get(head.api),
}));
for await (const chunkResult of worker.stream(entries, entries)) {
if (output) {
for (const section of chunkResult) {
const out = join(output, `${section.api}.json`);
// eslint-disable-next-line no-unused-vars
const { api, ...content } = section;
await writeFile(out, JSON.stringify(content, null, 2));
}
}
yield chunkResult;
}
},
};