-
-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: update algolia indexes with a local script (#59)
- Loading branch information
Showing
6 changed files
with
237 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
name: Update Algolia Index | ||
|
||
on: [workflow_dispatch] | ||
|
||
jobs: | ||
build: | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- name: checkout | ||
uses: actions/checkout@v3 | ||
|
||
- name: Enable PNPM | ||
uses: pnpm/action-setup@v2 | ||
|
||
- name: Set node version to 20 | ||
uses: actions/setup-node@v3 | ||
with: | ||
node-version: "20" | ||
|
||
- name: Install | ||
run: pnpm install --frozen-lockfile | ||
|
||
- name: Update Algolia Index | ||
run: pnpm run algolia | ||
env: | ||
ALGOLIA_API_KEY: ${{ secrets.ALGOLIA_ADMIN_KEY }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ | |
"packageManager": "[email protected]", | ||
"private": true, | ||
"scripts": { | ||
"algolia": "tsx scripts/algolia.ts", | ||
"check-duplicates": "tsx scripts/check-duplicates.ts", | ||
"fontlist": "tsx scripts/fontlist.ts", | ||
"gfm-metadata": "tsx scripts/gfm-metadata.ts", | ||
|
@@ -18,6 +19,7 @@ | |
"@fontsource-utils/cli": "0.4.1", | ||
"@fontsource-utils/publish": "^0.2.8", | ||
"@types/node": "^20.5.7", | ||
"algoliasearch": "^4.22.1", | ||
"consola": "^3.2.3", | ||
"google-font-metadata": "^5.2.1", | ||
"json-stringify-pretty-compact": "^4.0.0", | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import algoliasearch from 'algoliasearch'; | ||
import metadataImport from '../metadata/fontsource.json'; | ||
|
||
interface AlgoliaMetadata { | ||
objectID: string; | ||
family: string; | ||
subsets: string[]; | ||
weights: number[]; | ||
styles: string[]; | ||
defSubset: string; | ||
category: string; | ||
variable: boolean; | ||
lastModified: number; | ||
downloadMonth: number; | ||
randomIndex: number; | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
const client = algoliasearch('WNATE69PVR', process.env.ALGOLIA_ADMIN_KEY!); | ||
|
||
const shuffleArray = (size: number) => { | ||
// Generate array of numbers from 0 to size | ||
const arr: number[] = [...Array.from({ length: size }).keys()]; | ||
|
||
// Durstenfeld shuffle to randomly sort array | ||
for (let i = arr.length - 1; i > 0; i--) { | ||
const j = Math.floor(Math.random() * (i + 1)); | ||
[arr[i], arr[j]] = [arr[j], arr[i]]; | ||
} | ||
return arr; | ||
}; | ||
|
||
const updateAlgoliaIndex = async (force?: boolean) => { | ||
try { | ||
// Get font list | ||
const list = Object.keys(metadataImport); | ||
const indexArray: AlgoliaMetadata[] = []; | ||
|
||
// For the random shuffle, we need a presorted index | ||
// as Algolia does not support random sorting natively | ||
const randomIndexArr = shuffleArray(list.length); | ||
|
||
let index = 0; | ||
for (const id of list) { | ||
const metadata = metadataImport[id]; | ||
if (!metadata) | ||
console.warn(`No metadata found for ${id} when updating Algolia index`); | ||
|
||
const stats = ( | ||
await fetch(`https://api.fontsource.org/v1/stats/${id}`) | ||
).json() as any; | ||
const downloadCountMonthly = stats?.total?.npmDownloadMonthly; | ||
|
||
const obj = { | ||
objectID: id, | ||
family: metadata.family, | ||
subsets: metadata.subsets, | ||
weights: metadata.weights, | ||
styles: metadata.styles, | ||
category: metadata.category, | ||
defSubset: metadata.defSubset, | ||
variable: metadata.variable, | ||
// Algolia sorts date using a unix timestamp instead | ||
lastModified: Math.floor( | ||
new Date(metadata.lastModified).getTime() / 1000 | ||
), | ||
downloadMonth: downloadCountMonthly ?? 0, | ||
randomIndex: randomIndexArr[index], | ||
}; | ||
|
||
indexArray.push(obj); | ||
index++; | ||
} | ||
|
||
const searchIndex = client.initIndex('prod_NAME'); | ||
if (force) { | ||
await searchIndex.replaceAllObjects(indexArray); | ||
console.log('Replaced Algolia index'); | ||
} else { | ||
await searchIndex.saveObjects(indexArray); | ||
console.log('Updated Algolia index'); | ||
} | ||
} catch (error) { | ||
console.error(error); | ||
} | ||
}; | ||
|
||
updateAlgoliaIndex(); |