Skip to content

Commit a8e67a1

Browse files
committedSep 1, 2022
integrate GH action to publish to algolia
1 parent 0061007 commit a8e67a1

File tree

8 files changed

+76
-5
lines changed

8 files changed

+76
-5
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
name: Build And Publish Search Index
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
8+
jobs:
9+
build-publish-search-index:
10+
runs-on: ubuntu-latest
11+
timeout-minutes: 10
12+
steps:
13+
- uses: google/wireit@setup-github-actions-caching/v1
14+
- uses: actions/checkout@v2
15+
- uses: actions/setup-node@v2
16+
with:
17+
node-version: 18
18+
cache: npm
19+
cache-dependency-path: '**/package-lock.json'
20+
21+
- run: npm ci
22+
- id: build
23+
run: npm run build
24+
- name: Publish algolia search index
25+
env:
26+
ALGOLIA_WRITE_KEY: ${{ secrets.LIT_DEV_ALGOLIA_WRITE_KEY }}
27+
run: ALGOLIA_WRITE_KEY=$ALGOLIA_WRITE_KEY node packages/lit-dev-tools-esm/lib/upload-algolia-index.js

‎.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ packages/lit-dev-content/rollupout
1010
packages/lit-dev-content/site/fonts/manrope
1111
packages/lit-dev-content/temp
1212
packages/lit-dev-content/samples/js
13+
packages/lit-dev-content/src/public-vars.ts
1314

1415
packages/lit-dev-api/api-data/*/repo/
1516
packages/lit-dev-api/api-data/*/INSTALLED

‎packages/lit-dev-content/package.json

+13
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
"dev:build:eleventy": "wireit",
2323
"dev:build:eleventy:watch": "wireit",
2424
"dev:serve": "wireit",
25+
"copy-keys": "wireit",
2526
"prod:build": "wireit",
2627
"prod:build:assets": "wireit"
2728
},
@@ -135,6 +136,9 @@
135136
"output": [
136137
"lib"
137138
],
139+
"dependencies": [
140+
"copy-keys"
141+
],
138142
"command": "../../node_modules/.bin/tsc --build --pretty",
139143
"clean": "if-file-deleted"
140144
},
@@ -162,6 +166,15 @@
162166
"samples/js/**"
163167
],
164168
"command": "node ../lit-dev-tools-esm/lib/generate-js-samples.js"
169+
},
170+
"copy-keys": {
171+
"files": [
172+
"../../public-vars.json"
173+
],
174+
"output": [
175+
"src/public-vars.ts"
176+
],
177+
"command": "cp ../../public-vars.json public-vars.tsorig && echo 'export default ' > public-vars.tstemp && cat public-vars.tsorig >> public-vars.tstemp && cp public-vars.tstemp src/public-vars.ts && rm public-vars.tstemp && rm public-vars.tsorig"
165178
}
166179
},
167180
"devDependencies": {

‎packages/lit-dev-content/src/components/litdev-search.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import algoliasearch, {
2929
SearchIndex,
3030
} from 'algoliasearch/dist/algoliasearch-lite.esm.browser.js';
3131
import {Task, TaskStatus} from '@lit-labs/task';
32+
import vars from '../public-vars.js';
3233

3334
/**
3435
* Representation of each document indexed by Minisearch.
@@ -191,12 +192,12 @@ export class LitDevSearch extends LitElement {
191192
private _searchText: string = '';
192193

193194
private static _algoliaSearchClient: SearchClient = algoliasearch(
194-
'OC866NN61X',
195-
'33401c252374747a39ef3b42c9f701ac'
195+
vars.algolia.appId,
196+
vars.algolia.searchOnlyKey
196197
);
197198

198199
private static _algoliaSearch: SearchIndex =
199-
LitDevSearch._algoliaSearchClient.initIndex('lit_dev_test');
200+
LitDevSearch._algoliaSearchClient.initIndex(vars.algolia.index);
200201

201202
private _searchTask: Task<string[], Suggestion[]> | null = null;
202203

‎packages/lit-dev-content/tsconfig.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66
"incremental": true,
77
"composite": true,
88
"tsBuildInfoFile": "lib/.tsbuildinfo",
9-
"skipLibCheck": true
9+
"skipLibCheck": true,
10+
"resolveJsonModule": true
1011
},
11-
"include": ["src/**/*.ts"],
12+
"include": ["src/**/*.ts", "src/**/*.json"],
1213
"exclude": []
1314
}

‎packages/lit-dev-tools-cjs/src/search/plugin.ts

+2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ type UrlToFile = Map<string, string>;
1919
*/
2020
interface UserFacingPageData {
2121
id: number;
22+
objectID: number;
2223
relativeUrl: string;
2324
title: string;
2425
heading: string;
@@ -105,6 +106,7 @@ export async function createSearchIndex(outputDir: '_dev' | '_site') {
105106
// facing data to display in the suggested results.
106107
searchIndex.push({
107108
id: id++,
109+
objectID: id,
108110
title: title.replace(/ Lit$/, ''),
109111
heading,
110112
relativeUrl: relUrl.replace(/index.html$/, '') + (fragment ?? ''),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import algolia from 'algoliasearch';
2+
import fs from 'fs';
3+
4+
const varsRaw = fs.readFileSync('../../public-vars.json', 'utf8');
5+
const vars = JSON.parse(varsRaw);
6+
const searchIndexRaw = fs.readFileSync(
7+
'../lit-dev-content/_site/searchIndex.json',
8+
'utf8'
9+
);
10+
const searchIndex = JSON.parse(searchIndexRaw);
11+
const ALGOLIA_WRITE_KEY = process.env.ALGOLIA_WRITE_KEY;
12+
13+
if (!ALGOLIA_WRITE_KEY) {
14+
throw new Error('ALGOLIA_WRITE_KEY environment variable is not set');
15+
}
16+
17+
const client = algolia(vars.algolia.appId, ALGOLIA_WRITE_KEY);
18+
const index = client.initIndex(vars.algolia.indexName);
19+
await index.replaceAllObjects(searchIndex);

‎public-vars.json

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"algolia": {
3+
"appId": "OC866NN61X",
4+
"searchOnlyKey": "33401c252374747a39ef3b42c9f701ac",
5+
"index": "lit.dev"
6+
}
7+
}

0 commit comments

Comments
 (0)
Please sign in to comment.