1
1
'use strict' ;
2
2
3
- import { create } from '@orama/orama' ;
3
+ import { create , insert } from '@orama/orama' ;
4
4
import { persistToFile } from '@orama/plugin-data-persistence/server' ;
5
5
6
+ import { enforceArray } from '../../utils/array.mjs' ;
6
7
import { groupNodesByModule } from '../../utils/generators.mjs' ;
7
8
import { createSectionBuilder } from '../legacy-json/utils/buildSection.mjs' ;
8
9
10
+ /**
11
+ * Schema definition for the Orama database
12
+ */
13
+ const ORAMA_SCHEMA = {
14
+ name : 'string' ,
15
+ type : 'string' ,
16
+ desc : 'string' ,
17
+ stability : 'number' ,
18
+ stabilityText : 'string' ,
19
+ meta : {
20
+ changes : 'string[]' ,
21
+ added : 'string[]' ,
22
+ napiVersion : 'string[]' ,
23
+ deprecated : 'string[]' ,
24
+ removed : 'string[]' ,
25
+ } ,
26
+ } ;
27
+
28
+ /**
29
+ * Transforms a section into the format expected by Orama
30
+ * @param {import('../legacy-json/types.d.ts').ModuleSection } node - The section to transform
31
+ */
32
+ function transformSectionForOrama ( node ) {
33
+ return {
34
+ name : node . name ,
35
+ type : node . type ,
36
+ desc : node . desc ,
37
+ // Account for duplicate stability nodes
38
+ stability : enforceArray ( node . stability ) [ 0 ] ,
39
+ stabilityText : enforceArray ( node . stabilityText ) [ 0 ] ,
40
+ meta : {
41
+ changes :
42
+ node . meta ?. changes ?. map (
43
+ c => `${ enforceArray ( c . version ) . join ( ', ' ) } : ${ c . description } `
44
+ ) ?? [ ] ,
45
+ added : node . meta ?. added ?? [ ] ,
46
+ napiVersion : node . meta ?. napiVersion ?? [ ] ,
47
+ deprecated : node . meta ?. deprecated ?? [ ] ,
48
+ removed : node . meta ?. removed ?? [ ] ,
49
+ } ,
50
+ } ;
51
+ }
52
+
9
53
/**
10
54
* This generator is responsible for generating the Orama database for the
11
55
* API docs. It is based on the legacy-json generator.
@@ -16,11 +60,8 @@ import { createSectionBuilder } from '../legacy-json/utils/buildSection.mjs';
16
60
*/
17
61
export default {
18
62
name : 'orama-db' ,
19
-
20
63
version : '1.0.0' ,
21
-
22
64
description : 'Generates the Orama database for the API docs.' ,
23
-
24
65
dependsOn : 'ast' ,
25
66
26
67
/**
@@ -30,65 +71,35 @@ export default {
30
71
* @param {Partial<GeneratorOptions> } options
31
72
*/
32
73
async generate ( input , { output, version } ) {
33
- const buildSection = createSectionBuilder ( ) ;
74
+ if ( ! input ?. length ) {
75
+ throw new Error ( 'Input data is required and must not be empty' ) ;
76
+ }
34
77
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
- } ) ;
78
+ if ( ! output || ! version ) {
79
+ throw new Error ( 'Output path and version are required' ) ;
80
+ }
52
81
82
+ const db = create ( { schema : ORAMA_SCHEMA } ) ;
83
+ const buildSection = createSectionBuilder ( ) ;
53
84
const groupedModules = groupNodesByModule ( input ) ;
85
+ const headNodes = input . filter ( node => node . heading ?. depth === 1 ) ;
54
86
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 ) ;
87
+ // Process each head node and insert into database
88
+ headNodes . forEach ( headNode => {
89
+ const nodes = groupedModules . get ( headNode . api ) ;
64
90
65
- const section = buildSection ( head , nodes ) ;
91
+ const section = buildSection ( headNode , nodes ) ;
92
+ const node = ( section . modules || section . globals || section . miscs ) [ 0 ] ;
93
+ if ( ! node ) return ;
66
94
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
- } ;
95
+ const oramaData = transformSectionForOrama ( node ) ;
96
+ insert ( db , oramaData ) ;
97
+ } ) ;
85
98
86
- headNodes . map ( processModuleNodes ) ;
99
+ // Generate output filename and persist database
100
+ const sanitizedVersion = version . raw . replaceAll ( '.' , '-' ) ;
101
+ const outputFilename = `${ output } /${ sanitizedVersion } -orama-db.json` ;
87
102
88
- await persistToFile (
89
- db ,
90
- 'json' ,
91
- `${ output } /${ version . raw . replaceAll ( '.' , '-' ) } -orama-db.json`
92
- ) ;
103
+ await persistToFile ( db , 'json' , outputFilename ) ;
93
104
} ,
94
105
} ;
0 commit comments