Skip to content

Commit

Permalink
feat(environment): detect the current working environment of a project
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael committed Oct 5, 2022
1 parent 4b1fc0b commit d074a10
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
48 changes: 48 additions & 0 deletions lib/environment/environment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { env } from "./env";
import { isUndefined } from "../utils";

type UrbexContext = "browser" | "node";

export class Environment {
private _context: UrbexContext;

constructor() {
this._context = this.detectContext();
}

private detectContext(): UrbexContext {
if (!isUndefined(window) && !isUndefined(window.document)) {
return "browser";
}

if (
!isUndefined(process) &&
process.versions &&
process.versions.node
) {
return "node";
}

throw new Error("Could not detect environment context");
}

get context(): UrbexContext {
return this._context;
}

get isBrowser(): boolean {
return this.context === "browser";
}

get isNode(): boolean {
return this.context === "node";
}

get isDevelopment(): boolean {
return env.get("NODE_ENV") === "development";
}

get isProduction(): boolean {
return env.get("NODE_ENV") === "production";
}
}
2 changes: 2 additions & 0 deletions lib/environment/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from "./environment";
export * from "./env";

0 comments on commit d074a10

Please sign in to comment.