Skip to content

Commit

Permalink
feat(env): Support MODE environment variable for isDevelopment dete…
Browse files Browse the repository at this point in the history
…ction (#3012)

I've cherry-picked this out of #2992 so it can have a separate changelog entry.

This adds `MODE` as another variable we use to detect the development environment. This value is set by Vite on `import.meta.env` so I think it is valuable to support.
  • Loading branch information
blaine-arcjet authored Jan 30, 2025
1 parent a9af1e4 commit f3a45a7
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 2 deletions.
7 changes: 6 additions & 1 deletion env/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export type Env = {
[key: string]: unknown;
FLY_APP_NAME?: string;
VERCEL?: string;
MODE?: string;
NODE_ENV?: string;
ARCJET_KEY?: string;
ARCJET_ENV?: string;
Expand All @@ -20,7 +21,11 @@ export function platform(env: Env) {
}

export function isDevelopment(env: Env) {
return env.NODE_ENV === "development" || env.ARCJET_ENV === "development";
return (
env.NODE_ENV === "development" ||
env.MODE === "development" ||
env.ARCJET_ENV === "development"
);
}

export function logLevel(env: Env) {
Expand Down
4 changes: 4 additions & 0 deletions env/test/env.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,16 @@ describe("env", () => {
expect(env.platform({})).toBeUndefined();
expect(env.platform({ FLY_APP_NAME: "" })).toBeUndefined();
expect(env.platform({ FLY_APP_NAME: "foobar" })).toEqual("fly-io");
expect(env.platform({ VERCEL: "" })).toBeUndefined();
expect(env.platform({ VERCEL: "1" })).toEqual("vercel");
});

test("isDevelopment", () => {
expect(env.isDevelopment({})).toEqual(false);
expect(env.isDevelopment({ NODE_ENV: "production" })).toEqual(false);
expect(env.isDevelopment({ NODE_ENV: "development" })).toEqual(true);
expect(env.isDevelopment({ MODE: "production" })).toEqual(false);
expect(env.isDevelopment({ MODE: "development" })).toEqual(true);
expect(env.isDevelopment({ ARCJET_ENV: "production" })).toEqual(false);
expect(env.isDevelopment({ ARCJET_ENV: "development" })).toEqual(true);
});
Expand Down
3 changes: 2 additions & 1 deletion turbo.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
"ARCJET_RUNTIME",
"ARCJET_LOG_LEVEL",
"OPENAI_API_KEY",
"FLY_APP_NAME"
"FLY_APP_NAME",
"MODE"
],
"tasks": {
"build": {
Expand Down

0 comments on commit f3a45a7

Please sign in to comment.