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

Conversation

kachkaev
Copy link
Contributor

@kachkaev kachkaev commented Feb 3, 2022

We were having issues with Algolia indexing recently and it was unclear whether it was because of missing or incorrect env vars. This PR introduces envalid, which improves script feedback. We can add this library to other scripts as needed.

Before 🤔

Screenshot 2022-02-03 at 19 33 49

After 💡

Screenshot 2022-02-03 at 19 33 02

Feature summary

const env = envalid.cleanEnv({
  EMAIL: envalid.email({ default: "hello@example.com" }),
  EMAIL_TRANSPORT: envalid.str({ choices: ["aws", "none"], default: "aws", devDefault: "none" }),
  PORT: envalid.port({ default: 3000 }),
});
console.log(env.PORT === 3000); // true

// with PORT=-42 → 💥

console.log(env.EMAIL); // always a valid email

console.log(env.EMAIL_TRANSPORT); // "none"
console.log(env.isProduction); // false

// with NODE_ENV=production
console.log(env.EMAIL_TRANSPORT); // "aws"
console.log(env.isProduction); // true

// with EMAIL_TRANSPORT=oops → 💥

Caveats

  • By default, envalid calls process.exit instead of throwing. This makes script output cleaner because it does not contain the call stack. We can’t use try / catch though. If we do want to use try / catch, we can implement a custom reporter, which would throw. This may be useful in server-side code, but I doubt about scripts.

  • We need to use import * as envalid from "envalid". Writing import envalid from "envalid" does not produce a TS error but crashes. This may change if we switch to native ESM modules.

  • Specifying choices does not narrow the return type (i.e. env.EMAIL_TRANSPORT is string, not "aws" | "none"). This might be fixed via Ensure default values don't go through validation, fix type narrowing from defaults af/envalid#176


Asana task (internal link)

@kachkaev kachkaev marked this pull request as ready for review February 3, 2022 20:13
@CiaranMn CiaranMn added the area: infra Relates to version control, CI, CD or IaC (area) label Feb 4, 2022
CiaranMn
CiaranMn previously approved these changes Feb 4, 2022
Comment on lines +97 to +106
const specFiles = getFileInfos(
path.resolve(monorepoDirPath, "site/src/_pages/spec"),
[],
"spec",
);
const docsFiles = getFileInfos(
path.resolve(monorepoDirPath, "site/src/_pages/docs"),
[],
"docs",
);
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.

@kachkaev kachkaev enabled auto-merge (squash) February 4, 2022 09:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: infra Relates to version control, CI, CD or IaC (area)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants