|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +import { create } from '@orama/orama'; |
| 4 | +import { persistToFile } from '@orama/plugin-data-persistence/server'; |
| 5 | + |
| 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, version }) { |
| 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 {void} |
| 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 | + headNodes.map(processModuleNodes); |
| 87 | + |
| 88 | + await persistToFile( |
| 89 | + db, |
| 90 | + 'json', |
| 91 | + `${output}/${version.raw.replaceAll('.', '-')}-orama-db.json` |
| 92 | + ); |
| 93 | + }, |
| 94 | +}; |
0 commit comments