1
1
import { searchClient } from "@algolia/client-search" ;
2
+ import confirm from "@inquirer/confirm" ;
2
3
import { parseMdx } from "docs-generator/scripts/algolia/parseMdx" ;
3
4
import {
4
5
type HeadingWithDescription ,
@@ -20,15 +21,13 @@ import { getBlogs } from "../src/app/(main)/(markdown)/blog/data.js";
20
21
import { titleCase } from "../src/utils/strings.js" ;
21
22
import { CORE_SRC } from "./constants.js" ;
22
23
23
- const BASE_URL = "https://react-md.dev" ;
24
-
25
- async function indexMdxPages ( ) : Promise < readonly IndexedItem [ ] > {
24
+ async function indexMdxPages ( baseUrl : string ) : Promise < readonly IndexedItem [ ] > {
26
25
const mdxPages = await glob ( "src/app/**/page.mdx" ) ;
27
26
return await Promise . all (
28
27
mdxPages . map ( async ( mdxFilePath ) => {
29
28
try {
30
29
return parseMdx ( {
31
- baseUrl : BASE_URL ,
30
+ baseUrl,
32
31
mdxFilePath,
33
32
} ) ;
34
33
} catch ( e ) {
@@ -45,7 +44,9 @@ interface SassDocGroup {
45
44
variables : Map < string , FormattedVariableItem > ;
46
45
}
47
46
48
- async function indexSassDocPages ( ) : Promise < readonly IndexedItem [ ] > {
47
+ async function indexSassDocPages (
48
+ baseUrl : string
49
+ ) : Promise < readonly IndexedItem [ ] > {
49
50
const { mixins, functions, variables } = await generate ( { src : CORE_SRC } ) ;
50
51
51
52
const grouped = new Map < string , SassDocGroup > ( ) ;
@@ -108,7 +109,7 @@ async function indexSassDocPages(): Promise<readonly IndexedItem[]> {
108
109
grouped . forEach ( ( item , group ) => {
109
110
const groupTitle = titleCase ( group , "-" ) ;
110
111
const pathname = `/sassdoc/${ getGroupName ( group ) } ` ;
111
- const url = `${ BASE_URL } ${ pathname } ` ;
112
+ const url = `${ baseUrl } ${ pathname } ` ;
112
113
items . push ( {
113
114
objectID : url ,
114
115
url,
@@ -129,7 +130,7 @@ async function indexSassDocPages(): Promise<readonly IndexedItem[]> {
129
130
] . forEach ( ( sassdocItem ) => {
130
131
const { name, description, type } = sassdocItem ;
131
132
const itemPathname = getSassDocLink ( sassdocItem ) ;
132
- const url = `${ BASE_URL } ${ itemPathname } ` ;
133
+ const url = `${ baseUrl } ${ itemPathname } ` ;
133
134
const itemType =
134
135
type === "mixin" || type === "function" ? type : "variable" ;
135
136
@@ -149,7 +150,7 @@ async function indexSassDocPages(): Promise<readonly IndexedItem[]> {
149
150
return items ;
150
151
}
151
152
152
- async function indexBlogs ( ) : Promise < readonly IndexedItem [ ] > {
153
+ async function indexBlogs ( baseUrl : string ) : Promise < readonly IndexedItem [ ] > {
153
154
const items : IndexedItem [ ] = [ ] ;
154
155
const blogRoot = resolve ( process . cwd ( ) , "src/app/(main)/(markdown)/blog" ) ;
155
156
const blogs = await getBlogs ( blogRoot ) ;
@@ -169,8 +170,8 @@ async function indexBlogs(): Promise<readonly IndexedItem[]> {
169
170
type : "page" ,
170
171
title : "Blog" ,
171
172
description : "Stay up-to-date with the latest news about react-md" ,
172
- objectID : `${ BASE_URL } /blog` ,
173
- url : `${ BASE_URL } /blog` ,
173
+ objectID : `${ baseUrl } /blog` ,
174
+ url : `${ baseUrl } /blog` ,
174
175
pathname : "/blog" ,
175
176
headings,
176
177
keywords : [ "blog" , "release notes" ] ,
@@ -181,37 +182,94 @@ async function indexBlogs(): Promise<readonly IndexedItem[]> {
181
182
return items ;
182
183
}
183
184
184
- async function run ( ) : Promise < void > {
185
- config ( { path : join ( process . cwd ( ) , ".env.development.local" ) } ) ;
186
- config ( { path : join ( process . cwd ( ) , ".env.local" ) } ) ;
185
+ interface RequiredEnvVars {
186
+ appId : string ;
187
+ apiKey : string ;
188
+ baseUrl : string ;
189
+ indexName : string ;
190
+ }
187
191
188
- const appId = process . env . NEXT_PUBLIC_ALGOLIA_APP_ID ;
189
- const apiKey = process . env . ALGOLIA_WRITE_API_KEY ;
190
- const indexName = process . env . NEXT_PUBLIC_ALGOLIA_INDEX_NAME ;
191
- if ( ! appId || ! apiKey || ! indexName ) {
192
- throw new Error (
193
- `The .env.local is missing the NEXT_PUBLIC_ALGOLIA_APP_ID and/or ALGOLIA_WRITE_API_KEY and/or NEXT_PUBLIC_ALGOLIA_INDEX_NAME`
194
- ) ;
192
+ async function getEnvVars ( ) : Promise < RequiredEnvVars > {
193
+ const appId = process . env . ALGOLIA_APP_ID ?. trim ( ) ?? "" ;
194
+ const apiKey = process . env . ALGOLIA_WRITE_API_KEY ?. trim ( ) ?? "" ;
195
+ const indexName = process . env . ALGOLIA_INDEX_NAME ?. trim ( ) ?? "" ;
196
+ const baseUrl = process . env . BASE_URL ?. trim ( ) ?? "" ;
197
+ const missing : string [ ] = [ ] ;
198
+ if ( ! appId ) {
199
+ missing . push ( "ALGOLIA_APP_ID" ) ;
200
+ }
201
+ if ( ! apiKey ) {
202
+ missing . push ( "ALGOLIA_WRITE_API_KEY" ) ;
203
+ }
204
+ if ( ! indexName ) {
205
+ missing . push ( "ALGOLIA_INDEX_NAME" ) ;
206
+ }
207
+ if ( ! baseUrl ) {
208
+ missing . push ( "BASE_URL" ) ;
209
+ }
210
+ if ( missing . length ) {
211
+ console . error ( `The following environment variables are missing:` ) ;
212
+ console . error ( missing . map ( ( name ) => `- ${ name } ` ) . join ( "\n" ) ) ;
213
+ console . error ( "Update the `.env.algolia` with the correct values" ) ;
214
+ process . exit ( 1 ) ;
215
+ }
216
+
217
+ if (
218
+ ! ( await confirm ( {
219
+ message : `Are the following variables correct?
220
+
221
+ - appId - ${ appId }
222
+ - apiKey - ${ apiKey }
223
+ - baseUrl - ${ baseUrl }
224
+ - indexName - ${ indexName }
225
+
226
+ ` ,
227
+ } ) )
228
+ ) {
229
+ console . error ( `Update the \`.env.algolia\` and run again.` ) ;
230
+ process . exit ( 1 ) ;
195
231
}
196
232
233
+ return {
234
+ appId,
235
+ apiKey,
236
+ indexName,
237
+ baseUrl,
238
+ } ;
239
+ }
240
+
241
+ async function run ( {
242
+ appId,
243
+ apiKey,
244
+ baseUrl,
245
+ indexName,
246
+ } : RequiredEnvVars ) : Promise < void > {
197
247
const client = searchClient ( appId , apiKey ) ;
198
248
const indexes : IndexedItem [ ] = [ ] ;
199
249
indexes . push (
200
- ...( await log ( indexMdxPages ( ) , "Indexing MDX pages" , "MDX pages indexed" ) ) ,
201
250
...( await log (
202
- indexSassDocPages ( ) ,
251
+ indexMdxPages ( baseUrl ) ,
252
+ "Indexing MDX pages" ,
253
+ "MDX pages indexed"
254
+ ) ) ,
255
+ ...( await log (
256
+ indexSassDocPages ( baseUrl ) ,
203
257
"Indexing SassDoc pages" ,
204
258
"SassDoc pages indexed"
205
259
) ) ,
206
- ...( await log ( indexBlogs ( ) , "Indexing Blog pages" , "Blog pages indexed" ) )
260
+ ...( await log (
261
+ indexBlogs ( baseUrl ) ,
262
+ "Indexing Blog pages" ,
263
+ "Blog pages indexed"
264
+ ) )
207
265
) ;
208
266
indexes . push ( {
209
267
type : "page" ,
210
268
title : "Material Icons and Symbols" ,
211
269
description :
212
270
"This page is used to help find icons available in react-md using Material Symbols or Material Icons svg components. Icons can be filtered by type, group, or name." ,
213
- objectID : `${ BASE_URL } /components/material-icons-and-symbols` ,
214
- url : `${ BASE_URL } /components/material-icons-and-symbols` ,
271
+ objectID : `${ baseUrl } /components/material-icons-and-symbols` ,
272
+ url : `${ baseUrl } /components/material-icons-and-symbols` ,
215
273
pathname : "/components/material-icons-and-symbols" ,
216
274
headings : [ ] ,
217
275
keywords : [ "icon" , "material" , "component" ] ,
@@ -229,8 +287,10 @@ async function run(): Promise<void> {
229
287
} ) ;
230
288
}
231
289
290
+ config ( { path : join ( process . cwd ( ) , ".env.algolia" ) } ) ;
291
+ const envVars = await getEnvVars ( ) ;
232
292
await log (
233
- run ( ) ,
293
+ run ( envVars ) ,
234
294
"Indexing the documentation site" ,
235
295
"Documentation site indexed!"
236
296
) ;
0 commit comments