@@ -6,8 +6,16 @@ import { checkForCache, exists, run, toAbsolutePath } from './common';
6
6
import { createSpinner } from './oraLog' ;
7
7
import type { Spec } from './pre-gen/setHostsOptions' ;
8
8
9
+ const ALGOLIASEARCH_LITE_OPERATIONS = [
10
+ 'search' ,
11
+ 'multipleQueries' ,
12
+ 'searchForFacetValues' ,
13
+ 'post' ,
14
+ ] ;
15
+
9
16
async function propagateTagsToOperations (
10
- bundledPath : string
17
+ bundledPath : string ,
18
+ client : string
11
19
) : Promise < boolean > {
12
20
if ( ! ( await exists ( bundledPath ) ) ) {
13
21
throw new Error ( `Bundled file not found ${ bundledPath } .` ) ;
@@ -17,17 +25,9 @@ async function propagateTagsToOperations(
17
25
await fsp . readFile ( bundledPath , 'utf8' )
18
26
) as Spec ;
19
27
20
- if ( bundledSpec . tags . length === 0 ) {
21
- throw new Error (
22
- `No tags defined for ${ bundledPath } , tags are required to properly generate a client.`
23
- ) ;
24
- }
25
-
26
- const tagsName = bundledSpec . tags . map ( ( tag ) => tag . name ) ;
27
-
28
28
for ( const pathMethods of Object . values ( bundledSpec . paths ) ) {
29
29
for ( const specMethod of Object . values ( pathMethods ) ) {
30
- specMethod . tags = tagsName ;
30
+ specMethod . tags = [ client ] ;
31
31
}
32
32
}
33
33
@@ -74,22 +74,77 @@ async function lintCommon(verbose: boolean, useCache: boolean): Promise<void> {
74
74
spinner . succeed ( ) ;
75
75
}
76
76
77
+ /**
78
+ * Creates a lite search spec with the `ALGOLIASEARCH_LITE_OPERATIONS` methods
79
+ * from the `search` spec.
80
+ */
81
+ async function buildLiteSpec (
82
+ spec : string ,
83
+ bundledPath : string ,
84
+ outputFormat : string ,
85
+ verbose : boolean
86
+ ) : Promise < void > {
87
+ const searchSpec = yaml . load (
88
+ await fsp . readFile ( toAbsolutePath ( bundledPath ) , 'utf8' )
89
+ ) as Spec ;
90
+
91
+ searchSpec . paths = Object . entries ( searchSpec . paths ) . reduce (
92
+ ( acc , [ path , operations ] ) => {
93
+ for ( const [ method , operation ] of Object . entries ( operations ) ) {
94
+ if (
95
+ method === 'post' &&
96
+ ALGOLIASEARCH_LITE_OPERATIONS . includes ( operation . operationId )
97
+ ) {
98
+ return { ...acc , [ path ] : { post : operation } } ;
99
+ }
100
+ }
101
+
102
+ return acc ;
103
+ } ,
104
+ { } as Spec [ 'paths' ]
105
+ ) ;
106
+
107
+ const liteBundledPath = `specs/bundled/${ spec } .${ outputFormat } ` ;
108
+ await fsp . writeFile ( toAbsolutePath ( liteBundledPath ) , yaml . dump ( searchSpec ) ) ;
109
+
110
+ if (
111
+ ! ( await propagateTagsToOperations ( toAbsolutePath ( liteBundledPath ) , spec ) )
112
+ ) {
113
+ throw new Error (
114
+ `Unable to propage tags to operations for \`${ spec } \` spec.`
115
+ ) ;
116
+ }
117
+
118
+ await run ( `yarn specs:fix bundled/${ spec } .${ outputFormat } ` , {
119
+ verbose,
120
+ } ) ;
121
+ }
122
+
77
123
async function buildSpec (
78
- client : string ,
124
+ spec : string ,
79
125
outputFormat : string ,
80
126
verbose : boolean ,
81
127
useCache : boolean
82
128
) : Promise < void > {
83
- createSpinner ( `'${ client } ' spec` , verbose ) . start ( ) . info ( ) ;
129
+ const shouldBundleLiteSpec = spec === 'algoliasearch-lite' ;
130
+ const client = shouldBundleLiteSpec ? 'search' : spec ;
84
131
const cacheFile = toAbsolutePath ( `specs/dist/${ client } .cache` ) ;
85
132
let hash = '' ;
86
133
134
+ createSpinner ( `'${ client } ' spec` , verbose ) . start ( ) . info ( ) ;
135
+
87
136
if ( useCache ) {
137
+ const generatedFiles = [ `bundled/${ client } .yml` ] ;
138
+
139
+ if ( shouldBundleLiteSpec ) {
140
+ generatedFiles . push ( `bundled/${ spec } .yml` ) ;
141
+ }
142
+
88
143
const { cacheExists, hash : newCache } = await checkForCache (
89
144
{
90
145
job : `'${ client } ' specs` ,
91
146
folder : toAbsolutePath ( 'specs/' ) ,
92
- generatedFiles : [ `bundled/ ${ client } .yml` ] ,
147
+ generatedFiles,
93
148
filesToCache : [ client , 'common' ] ,
94
149
cacheFile,
95
150
} ,
@@ -110,9 +165,7 @@ async function buildSpec(
110
165
{ verbose }
111
166
) ;
112
167
113
- if (
114
- ( await propagateTagsToOperations ( toAbsolutePath ( bundledPath ) ) ) === false
115
- ) {
168
+ if ( ! ( await propagateTagsToOperations ( toAbsolutePath ( bundledPath ) , client ) ) ) {
116
169
spinner . fail ( ) ;
117
170
throw new Error (
118
171
`Unable to propage tags to operations for \`${ client } \` spec.`
@@ -130,6 +183,11 @@ async function buildSpec(
130
183
spinner . text = `linting '${ client } ' bundled spec` ;
131
184
await run ( `yarn specs:fix bundled/${ client } .${ outputFormat } ` , { verbose } ) ;
132
185
186
+ if ( shouldBundleLiteSpec ) {
187
+ spinner . text = `Building and linting '${ spec } ' spec` ;
188
+ await buildLiteSpec ( spec , bundledPath , outputFormat , verbose ) ;
189
+ }
190
+
133
191
if ( hash ) {
134
192
spinner . text = `storing ${ client } spec cache` ;
135
193
await fsp . writeFile ( cacheFile , hash ) ;
0 commit comments