|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +import { writeFile } from 'node:fs/promises'; |
| 4 | +import { join } from 'node:path'; |
| 5 | +import { groupNodesByModule } from '../../utils/generators.mjs'; |
| 6 | +import { createSectionBuilder } from './utils/buildSection.mjs'; |
| 7 | + |
| 8 | +/** |
| 9 | + * This generator is responsible for generating the legacy JSON files for the |
| 10 | + * legacy API docs for retro-compatibility. It is to be replaced while we work |
| 11 | + * on the new schema for this file. |
| 12 | + * |
| 13 | + * This is a top-level generator, intaking the raw AST tree of the api docs. |
| 14 | + * It generates JSON files to the specified output directory given by the |
| 15 | + * config. |
| 16 | + * |
| 17 | + * @typedef {Array<ApiDocMetadataEntry>} Input |
| 18 | + * |
| 19 | + * @type {import('../types.d.ts').GeneratorMetadata<Input, import('./types.d.ts').Section[]>} |
| 20 | + */ |
| 21 | +export default { |
| 22 | + name: 'legacy-json', |
| 23 | + |
| 24 | + version: '1.0.0', |
| 25 | + |
| 26 | + description: 'Generates the legacy version of the JSON API docs.', |
| 27 | + |
| 28 | + dependsOn: 'ast', |
| 29 | + |
| 30 | + /** |
| 31 | + * Generates a legacy JSON file. |
| 32 | + * |
| 33 | + * @param {Input} input |
| 34 | + * @param {Partial<GeneratorOptions>} options |
| 35 | + */ |
| 36 | + async generate(input, { output }) { |
| 37 | + const buildSection = createSectionBuilder(); |
| 38 | + |
| 39 | + // This array holds all the generated values for each module |
| 40 | + const generatedValues = []; |
| 41 | + |
| 42 | + const groupedModules = groupNodesByModule(input); |
| 43 | + |
| 44 | + // Gets the first nodes of each module, which is considered the "head" |
| 45 | + const headNodes = input.filter(node => node.heading.depth === 1); |
| 46 | + |
| 47 | + /** |
| 48 | + * @param {ApiDocMetadataEntry} head |
| 49 | + * @returns {import('./types.d.ts').ModuleSection} |
| 50 | + */ |
| 51 | + const processModuleNodes = head => { |
| 52 | + const nodes = groupedModules.get(head.api); |
| 53 | + |
| 54 | + const section = buildSection(head, nodes); |
| 55 | + generatedValues.push(section); |
| 56 | + |
| 57 | + return section; |
| 58 | + }; |
| 59 | + |
| 60 | + await Promise.all( |
| 61 | + headNodes.map(async node => { |
| 62 | + // Get the json for the node's section |
| 63 | + const section = processModuleNodes(node); |
| 64 | + |
| 65 | + // Write it to the output file |
| 66 | + if (output) { |
| 67 | + await writeFile( |
| 68 | + join(output, `${node.api}.json`), |
| 69 | + JSON.stringify(section) |
| 70 | + ); |
| 71 | + } |
| 72 | + }) |
| 73 | + ); |
| 74 | + |
| 75 | + return generatedValues; |
| 76 | + }, |
| 77 | +}; |
0 commit comments