Skip to content

Commit a691789

Browse files
feat(generator/orama-db): introduce (#213)
1 parent 2448be0 commit a691789

File tree

8 files changed

+167
-3
lines changed

8 files changed

+167
-3
lines changed

.github/dependabot.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,7 @@ updates:
2929
- '@eslint/*'
3030
- 'globals'
3131
- 'stylelint-*'
32+
orama:
33+
patterns:
34+
- '@orama/*'
3235
open-pull-requests-limit: 10

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,6 @@ Options:
4040
-o, --output <path> Specify the relative or absolute output directory
4141
-v, --version <semver> Specify the target version of Node.js, semver compliant (default: "v22.6.0")
4242
-c, --changelog <url> Specify the path (file: or https://) to the CHANGELOG.md file (default: "https://raw.githubusercontent.com/nodejs/node/HEAD/CHANGELOG.md")
43-
-t, --target [mode...] Set the processing target modes (choices: "json-simple", "legacy-html", "legacy-html-all", "man-page", "legacy-json", "legacy-json-all", "addon-verify", "api-links")
43+
-t, --target [mode...] Set the processing target modes (choices: "json-simple", "legacy-html", "legacy-html-all", "man-page", "legacy-json", "legacy-json-all", "addon-verify", "api-links", "orama-db")
4444
-h, --help display help for command
4545
```

package-lock.json

Lines changed: 37 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,13 @@
3333
"prettier": "3.5.2"
3434
},
3535
"dependencies": {
36-
"acorn": "^8.14.0",
3736
"@actions/core": "^1.11.1",
37+
"@orama/orama": "^3.1.3",
38+
"@orama/plugin-data-persistence": "^3.1.3",
39+
"acorn": "^8.14.0",
3840
"commander": "^13.1.0",
39-
"estree-util-visit": "^2.0.0",
4041
"dedent": "^1.5.3",
42+
"estree-util-visit": "^2.0.0",
4143
"github-slugger": "^2.0.0",
4244
"glob": "^11.0.1",
4345
"hast-util-to-string": "^3.0.1",

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: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
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+
};

src/generators/orama-db/types.d.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { Orama } from '@orama/orama';
2+
3+
/**
4+
* Schema for the Orama database entry
5+
*/
6+
export interface OramaDbEntry {
7+
name: string;
8+
type: string;
9+
desc: string;
10+
stability: number;
11+
stabilityText: string;
12+
meta: {
13+
changes: string[];
14+
added: string[];
15+
napiVersion: string[];
16+
deprecated: string[];
17+
removed: string[];
18+
};
19+
}
20+
21+
/**
22+
* Represents the Orama database for API docs
23+
*/
24+
export type OramaDb = Orama<OramaDbEntry>;

0 commit comments

Comments
 (0)