-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathindex.mjs
94 lines (78 loc) · 2.4 KB
/
index.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
'use strict';
import { create } from '@orama/orama';
import { persistToFile } from '@orama/plugin-data-persistence/server';
import { groupNodesByModule } from '../../utils/generators.mjs';
import { createSectionBuilder } from '../legacy-json/utils/buildSection.mjs';
/**
* This generator is responsible for generating the Orama database for the
* API docs. It is based on the legacy-json generator.
*
* @typedef {Array<ApiDocMetadataEntry>} Input
*
* @type {import('../types.d.ts').GeneratorMetadata<Input, import('./types.d.ts').OramaDb>}
*/
export default {
name: 'orama-db',
version: '1.0.0',
description: 'Generates the Orama database for the API docs.',
dependsOn: 'ast',
/**
* Generates the Orama database.
*
* @param {Input} input
* @param {Partial<GeneratorOptions>} options
*/
async generate(input, { output, version }) {
const buildSection = createSectionBuilder();
// Create the Orama instance with the schema
const db = create({
schema: {
name: 'string',
type: 'string',
desc: 'string',
stability: 'number',
stabilityText: 'string',
meta: {
changes: 'string[]',
added: 'string[]',
napiVersion: 'string[]',
deprecated: 'string[]',
removed: 'string[]',
},
},
});
const groupedModules = groupNodesByModule(input);
// Gets the first nodes of each module, which is considered the "head"
const headNodes = input.filter(node => node.heading.depth === 1);
/**
* @param {ApiDocMetadataEntry} head
* @returns {void}
*/
const processModuleNodes = head => {
const nodes = groupedModules.get(head.api);
const section = buildSection(head, nodes);
// Insert data into the Orama instance
db.insert({
name: section.name,
type: section.type,
desc: section.desc,
stability: section.stability,
stabilityText: section.stabilityText,
meta: {
changes: section.meta.changes,
added: section.meta.added,
napiVersion: section.meta.napiVersion,
deprecated: section.meta.deprecated,
removed: section.meta.removed,
},
});
return section;
};
headNodes.map(processModuleNodes);
await persistToFile(
db,
'json',
`${output}/${version.raw.replaceAll('.', '-')}-orama-db.json`
);
},
};