Skip to content

Commit c824195

Browse files
Add Orama db generator
1 parent f51c583 commit c824195

File tree

3 files changed

+114
-0
lines changed

3 files changed

+114
-0
lines changed

src/generators.mjs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@
22

33
import publicGenerators from './generators/index.mjs';
44
import astJs from './generators/ast-js/index.mjs';
5+
import oramaDb from './generators/orama-db/index.mjs';
56

67
const availableGenerators = {
78
...publicGenerators,
89
// This one is a little special since we don't want it to run unless we need
910
// it and we also don't want it to be publicly accessible through the CLI.
1011
'ast-js': astJs,
12+
'orama-db': oramaDb,
1113
};
1214

1315
/**

src/generators/index.mjs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import legacyJson from './legacy-json/index.mjs';
88
import legacyJsonAll from './legacy-json-all/index.mjs';
99
import addonVerify from './addon-verify/index.mjs';
1010
import apiLinks from './api-links/index.mjs';
11+
import oramaDb from './orama-db/index.mjs';
1112

1213
export default {
1314
'json-simple': jsonSimple,
@@ -18,4 +19,5 @@ export default {
1819
'legacy-json-all': legacyJsonAll,
1920
'addon-verify': addonVerify,
2021
'api-links': apiLinks,
22+
'orama-db': oramaDb,
2123
};

src/generators/orama-db/index.mjs

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
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

Comments
 (0)