Skip to content

Commit

Permalink
feat(errors): add snowpack env var support for assert
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Sep 21, 2021
1 parent 8605f1e commit 52822b1
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions packages/errors/src/assert.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { defError } from "./deferror";

// FIXME https://github.com/snowpackjs/snowpack/issues/3621#issuecomment-907731004
import.meta.hot;

declare const __SNOWPACK_ENV__: any;

export const AssertionError = defError<any>(() => "Assertion failed");

/**
Expand All @@ -10,15 +15,15 @@ export const AssertionError = defError<any>(() => "Assertion failed");
* The function is only enabled if `process.env.NODE_ENV != "production"`
* or if the `UMBRELLA_ASSERTS` env var is set to 1.
*/
export const assert = (() => {
try {
return (
process.env.NODE_ENV !== "production" ||
process.env.UMBRELLA_ASSERTS === "1"
);
} catch (e) {}
return false;
})()
export const assert = (() =>
typeof process !== "undefined" && typeof process.env !== "undefined"
? process.env.NODE_ENV !== "production" ||
!!process.env.UMBRELLA_ASSERTS
: typeof __SNOWPACK_ENV__ !== "undefined"
? __SNOWPACK_ENV__.MODE !== "production" ||
!!__SNOWPACK_ENV__.UMBRELLA_ASSERTS ||
!!__SNOWPACK_ENV__.SNOWPACK_PUBLIC_UMBRELLA_ASSERTS
: true)()
? (test: boolean | (() => boolean), msg?: string | (() => string)) => {
if ((typeof test === "function" && !test()) || !test) {
throw new AssertionError(typeof msg === "function" ? msg() : msg);
Expand Down

0 comments on commit 52822b1

Please sign in to comment.