|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +import { writeFile } from 'node:fs/promises'; |
| 4 | +import { join } from 'node:path'; |
| 5 | +import { create } from '@orama/orama'; |
| 6 | +import { groupNodesByModule } from '../../utils/generators.mjs'; |
| 7 | +import { createSectionBuilder } from '../legacy-json/utils/buildSection.mjs'; |
| 8 | + |
| 9 | +/** |
| 10 | + * This generator is responsible for generating the Orama database for the |
| 11 | + * API docs. It is based on the legacy-json generator. |
| 12 | + * |
| 13 | + * @typedef {Array<ApiDocMetadataEntry>} Input |
| 14 | + * |
| 15 | + * @type {import('../types.d.ts').GeneratorMetadata<Input, import('./types.d.ts').OramaDb>} |
| 16 | + */ |
| 17 | +export default { |
| 18 | + name: 'orama-db', |
| 19 | + |
| 20 | + version: '1.0.0', |
| 21 | + |
| 22 | + description: 'Generates the Orama database for the API docs.', |
| 23 | + |
| 24 | + dependsOn: 'ast', |
| 25 | + |
| 26 | + /** |
| 27 | + * Generates the Orama database. |
| 28 | + * |
| 29 | + * @param {Input} input |
| 30 | + * @param {Partial<GeneratorOptions>} options |
| 31 | + */ |
| 32 | + async generate(input, { output }) { |
| 33 | + const buildSection = createSectionBuilder(); |
| 34 | + |
| 35 | + // Create the Orama instance with the schema |
| 36 | + const db = create({ |
| 37 | + schema: { |
| 38 | + name: 'string', |
| 39 | + type: 'string', |
| 40 | + desc: 'string', |
| 41 | + stability: 'number', |
| 42 | + stabilityText: 'string', |
| 43 | + meta: { |
| 44 | + changes: 'string[]', |
| 45 | + added: 'string[]', |
| 46 | + napiVersion: 'string[]', |
| 47 | + deprecated: 'string[]', |
| 48 | + removed: 'string[]', |
| 49 | + }, |
| 50 | + }, |
| 51 | + }); |
| 52 | + |
| 53 | + const groupedModules = groupNodesByModule(input); |
| 54 | + |
| 55 | + // Gets the first nodes of each module, which is considered the "head" |
| 56 | + const headNodes = input.filter(node => node.heading.depth === 1); |
| 57 | + |
| 58 | + /** |
| 59 | + * @param {ApiDocMetadataEntry} head |
| 60 | + * @returns {import('./types.d.ts').OramaDbEntry} |
| 61 | + */ |
| 62 | + const processModuleNodes = head => { |
| 63 | + const nodes = groupedModules.get(head.api); |
| 64 | + |
| 65 | + const section = buildSection(head, nodes); |
| 66 | + |
| 67 | + // Insert data into the Orama instance |
| 68 | + db.insert({ |
| 69 | + name: section.name, |
| 70 | + type: section.type, |
| 71 | + desc: section.desc, |
| 72 | + stability: section.stability, |
| 73 | + stabilityText: section.stabilityText, |
| 74 | + meta: { |
| 75 | + changes: section.meta.changes, |
| 76 | + added: section.meta.added, |
| 77 | + napiVersion: section.meta.napiVersion, |
| 78 | + deprecated: section.meta.deprecated, |
| 79 | + removed: section.meta.removed, |
| 80 | + }, |
| 81 | + }); |
| 82 | + |
| 83 | + return section; |
| 84 | + }; |
| 85 | + |
| 86 | + await Promise.all( |
| 87 | + headNodes.map(async node => { |
| 88 | + // Get the json for the node's section |
| 89 | + const section = processModuleNodes(node); |
| 90 | + |
| 91 | + // Write it to the output file |
| 92 | + if (output) { |
| 93 | + await writeFile( |
| 94 | + join(output, `${node.api}.json`), |
| 95 | + JSON.stringify(section) |
| 96 | + ); |
| 97 | + } |
| 98 | + }) |
| 99 | + ); |
| 100 | + |
| 101 | + // Generate the JSON representation of the Orama db |
| 102 | + const oramaDbJson = JSON.stringify(db); |
| 103 | + |
| 104 | + if (output) { |
| 105 | + await writeFile(join(output, 'orama-db.json'), oramaDbJson); |
| 106 | + } |
| 107 | + |
| 108 | + return db; |
| 109 | + }, |
| 110 | +}; |
0 commit comments