Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add envalid to improve script feedback when env vars are missing #192

Merged
merged 5 commits into from
Feb 4, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"check-dependency-version-consistency": "^1.6.0",
"concurrently": "^6.5.1",
"dotenv-flow": "3.2.0",
"envalid": "^7.2.2",
"eslint": "^7.32.0",
"eslint-config-airbnb": "^18.2.1",
"eslint-config-prettier": "8.3.0",
Expand Down
50 changes: 30 additions & 20 deletions scripts/sync-algolia-index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ import fs from "fs";
import path from "path";
import matter from "gray-matter";
import algoliasearch from "algoliasearch";
import * as envalid from "envalid";

import siteMap from "../site/site-map.json";

const monorepoDirPath = path.resolve(__dirname, "..");

type DocsFrontMatter = {
content: string;
data: Record<string, string>;
Expand Down Expand Up @@ -91,8 +94,16 @@ const generateAlgoliaRecords: () => AlgoliaRecord[] = () => {
return appendData;
};

const specFiles = getFileInfos("../../../site/src/_pages/spec", [], "spec");
const docsFiles = getFileInfos("../../../site/src/_pages/docs", [], "docs");
const specFiles = getFileInfos(
path.resolve(monorepoDirPath, "site/src/_pages/spec"),
[],
"spec",
);
const docsFiles = getFileInfos(
path.resolve(monorepoDirPath, "site/src/_pages/docs"),
[],
"docs",
);
Comment on lines +97 to +106
Copy link
Contributor Author

@kachkaev kachkaev Feb 4, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


const specData = specFiles.map((filePath, fileIndex) => {
const file = fs.readFileSync(filePath.inputPath, "utf8");
Expand All @@ -113,11 +124,22 @@ const generateAlgoliaRecords: () => AlgoliaRecord[] = () => {
return [...specData, ...docsData];
};

const syncAlgoliaIndex = async () => {
const client = algoliasearch(
process.env.ALGOLIA_PROJECT ?? "",
process.env.ALGOLIA_WRITE_KEY ?? "",
);
const script = async () => {
console.log("Syncing Algolia index");

const env = envalid.cleanEnv(process.env, {
ALGOLIA_PROJECT: envalid.str({
desc: "Algolia app id",
example: "A1B2C3D4C5D6",
docs: "https://www.algolia.com/doc/api-client/getting-started/instantiate-client-index/javascript/?client=javascript",
}),
ALGOLIA_WRITE_KEY: envalid.str({
desc: "Algolia app API key with write permissions (32-char HEX)",
docs: "https://www.algolia.com/doc/api-client/getting-started/instantiate-client-index/javascript/?client=javascript",
}),
});

const client = algoliasearch(env.ALGOLIA_PROJECT, env.ALGOLIA_WRITE_KEY);

const index = client.initIndex("blockprotocol");

Expand Down Expand Up @@ -151,20 +173,8 @@ const syncAlgoliaIndex = async () => {
await index.deleteObjects(objectIDsToDelete);

await index.saveObjects(indexObjects);
};

const script = async () => {
try {
await syncAlgoliaIndex();
console.log("Algolia Indexes Updated.");
} catch (error) {
throw new Error(
`Error while running Algolia Update: ${
// @ts-expect-error -- TS is a bit too strict here. We can‘t use instanceof Error because Algolia errors are plain objects
error?.message ?? error
}`,
);
}
console.log("Algolia index updated.");
};

void script();
17 changes: 12 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5075,6 +5075,13 @@ env-paths@^2.2.0:
resolved "https://registry.yarnpkg.com/env-paths/-/env-paths-2.2.1.tgz#420399d416ce1fbe9bc0a07c62fa68d67fd0f8f2"
integrity sha512-+h1lkLKhZMTYjog1VEpJNG7NZJWcuc2DDk/qsqSTRRCOXiLjeQ1d1/udrUGhqMxUgAlwKNZ0cf2uqan5GLuS2A==

envalid@^7.2.2:
version "7.2.2"
resolved "https://registry.yarnpkg.com/envalid/-/envalid-7.2.2.tgz#f3219f85e692002dca0f28076740227d30c817e3"
integrity sha512-bl/3VF5PhoF26HlDWiE0NRRHUbKT/+UDP/+0JtOFmhUwK3cUPS7JgWYGbE8ArvA61T+SyNquxscLCS6y4Wnpdw==
dependencies:
tslib "2.3.1"

err-code@^2.0.2:
version "2.0.3"
resolved "https://registry.yarnpkg.com/err-code/-/err-code-2.0.3.tgz#23c2f3b756ffdfc608d30e27c9a941024807e7f9"
Expand Down Expand Up @@ -11048,16 +11055,16 @@ tsconfig-paths@^3.11.0:
minimist "^1.2.0"
strip-bom "^3.0.0"

tslib@2.3.1, tslib@^2.0.3, tslib@^2.1.0, tslib@^2.3.0:
version "2.3.1"
resolved "https://registry.yarnpkg.com/tslib/-/tslib-2.3.1.tgz#e8a335add5ceae51aa261d32a490158ef042ef01"
integrity sha512-77EbyPPpMz+FRFRuAFlWMtmgUWGe9UOG2Z25NqCwiIjRhOf5iKGuzSe5P2w1laq+FkRy4p+PCuVkJSGkzTEKVw==

tslib@^1.11.1, tslib@^1.8.1, tslib@^1.9.0:
version "1.14.1"
resolved "https://registry.yarnpkg.com/tslib/-/tslib-1.14.1.tgz#cf2d38bdc34a134bcaf1091c41f6619e2f672d00"
integrity sha512-Xni35NKzjgMrwevysHTCArtLDpPvye8zV/0E4EyYn43P7/7qvQwPh9BGkHewbMulVntbigmcT7rdX3BNo9wRJg==

tslib@^2.0.3, tslib@^2.1.0, tslib@^2.3.0:
version "2.3.1"
resolved "https://registry.yarnpkg.com/tslib/-/tslib-2.3.1.tgz#e8a335add5ceae51aa261d32a490158ef042ef01"
integrity sha512-77EbyPPpMz+FRFRuAFlWMtmgUWGe9UOG2Z25NqCwiIjRhOf5iKGuzSe5P2w1laq+FkRy4p+PCuVkJSGkzTEKVw==

tsutils@^3.21.0:
version "3.21.0"
resolved "https://registry.yarnpkg.com/tsutils/-/tsutils-3.21.0.tgz#b48717d394cea6c1e096983eed58e9d61715b623"
Expand Down