1
0
Fork 0

fix(dev): Clear algolia index prior to indexing

This commit is contained in:
Joost De Cock 2022-08-10 14:03:18 +02:00
parent e2ae2376ad
commit 263b3216ef

View file

@ -4,11 +4,9 @@
* content
*
* It expects the following environment vars to be set in a
* .env file in the 'packages/freesewing.dev' folder:
* .env file in the 'sites/dev' folder:
*
* ALGOLIA_APP_ID -> probably MA0Y5A2PF0
* ALGOLIA_API_KEY -> Needs permission to index/create/delete
* ALGOLIA_INDEX -> Name of the index to index to
* ALGOLIA_API_WRITE_KEY -> Needs permission to index/create/delete
*
*/
import dotenv from 'dotenv'
@ -24,9 +22,9 @@ import remarkRehype from 'remark-rehype'
import rehypeSanitize from 'rehype-sanitize'
import rehypeStringify from 'rehype-stringify'
import yaml from 'yaml'
import { getPosts } from '../../freesewing.shared/prebuild/strapi.mjs'
import { getMdxFileList } from '../../freesewing.shared/prebuild/mdx.mjs'
import config from '../algolia.mjs'
import { getPosts } from '../../shared/prebuild/strapi.mjs'
import { getMdxFileList } from '../../shared/prebuild/mdx.mjs'
import config from '../algolia.config.mjs'
dotenv.config()
/*
@ -70,8 +68,7 @@ const transformAuthor = author => ({
const indexStrapiContent = async () => {
// Say hi
console.log()
console.log(`Indexing Strapi content to Algolia`)
console.log(`🗂️ Indexing Strapi content to Algolia`)
const authors = {}
const rawPosts = await getPosts('blog', 'dev', 'en')
@ -83,12 +80,12 @@ const indexStrapiContent = async () => {
// Index posts to Algolia
index
.saveObjects(Object.values(rawPosts).map(post => transformBlogpost(post)))
.then(({ objectIDs }) => console.log(objectIDs))
.then(({ objectIDs }) => null)
.catch(err => console.log(err))
// Index authors to Algolia
index
.saveObjects(Object.values(authors))
.then(({ objectIDs }) => console.log(objectIDs))
.then(({ objectIDs }) => null)
.catch(err => console.log(err))
}
@ -119,6 +116,13 @@ const markdownLoader = async file => {
}
}
/*
* Clear the index to scrub old pages
*/
const clearIndex = async () => {
console.log(`🗑️ Clearing index`)
await index.clearObjects()
}
/*
@ -127,8 +131,7 @@ const markdownLoader = async file => {
const indexMarkdownContent = async () => {
// Say hi
console.log()
console.log(`Indexing Markdown content to Algolia`)
console.log(`🗂️ Indexing Markdown content to Algolia`)
// Setup MDX root path
const mdxRoot = path.resolve('..', '..', 'markdown', 'dev')
@ -136,11 +139,13 @@ const indexMarkdownContent = async () => {
// Get list of filenames
const list = await getMdxFileList(mdxRoot, 'en')
const pages = []
for (const file of list) pages.push(await markdownLoader(file))
// Index markdown to Algolia
index
await index.clearObjects()
await index
.saveObjects(pages)
.then(({ objectIDs }) => console.log(objectIDs))
.then(({ objectIDs }) => null)
.catch(err => console.log(err))
}
@ -149,6 +154,8 @@ const run = async () => {
process.env.VERCEL_ENV === 'production' ||
process.env.FORCE_ALGOLIA
) {
console.log()
await clearIndex()
await indexMarkdownContent()
await indexStrapiContent()
console.log()