Skip to content

Commit 995d525

Browse files
committed
refactor: stricter type checks
1 parent cb104d3 commit 995d525

File tree

10 files changed

+60
-45
lines changed

10 files changed

+60
-45
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ node_modules
22
*.log*
33
dist
44
coverage
5+
tsconfig.tsbuildinfo

src/_utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
export function toBoolean(val: boolean | string | undefined) {
1+
export function toBoolean(val: boolean | string | undefined): boolean {
22
return val ? val !== "false" : false;
33
}

src/env.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@ export type EnvObject = Record<string, string | undefined>;
44

55
const _getEnv = (useShim?: boolean) =>
66
globalThis.process?.env ||
7+
// @ts-ignore
78
import.meta.env ||
89
globalThis.Deno?.env.toObject() ||
910
globalThis.__env__ ||
1011
(useShim ? _envShim : globalThis);
1112

12-
export const env = new Proxy<EnvObject>(_envShim, {
13+
export const env: EnvObject = new Proxy<EnvObject>(_envShim, {
1314
get(_, prop) {
1415
const env = _getEnv();
1516
return env[prop as any] ?? _envShim[prop];
@@ -37,5 +38,5 @@ export const env = new Proxy<EnvObject>(_envShim, {
3738
},
3839
});
3940

40-
export const nodeENV =
41+
export const nodeENV: string =
4142
(typeof process !== "undefined" && process.env && process.env.NODE_ENV) || "";

src/flags.ts

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,58 @@
1-
import { providerInfo } from "./providers";
2-
import { env, nodeENV } from "./env";
3-
import { toBoolean } from "./_utils";
1+
import { providerInfo } from "./providers.ts";
2+
import { env, nodeENV } from "./env.ts";
3+
import { toBoolean } from "./_utils.ts";
44

55
/** Value of process.platform */
6-
export const platform = globalThis.process?.platform || "";
6+
export const platform: string = globalThis.process?.platform || "";
77

88
/** Detect if `CI` environment variable is set or a provider CI detected */
9-
export const isCI = toBoolean(env.CI) || providerInfo.ci !== false;
9+
export const isCI: boolean = toBoolean(env.CI) || providerInfo.ci !== false;
1010

1111
/** Detect if stdout.TTY is available */
12-
export const hasTTY = toBoolean(
12+
export const hasTTY: boolean = toBoolean(
1313
globalThis.process?.stdout && globalThis.process?.stdout.isTTY,
1414
);
1515

1616
/** Detect if global `window` object is available */
1717
// eslint-disable-next-line unicorn/prefer-global-this
18-
export const hasWindow = typeof window !== "undefined";
18+
export const hasWindow: boolean = typeof window !== "undefined";
1919

2020
/** Detect if `DEBUG` environment variable is set */
21-
export const isDebug = toBoolean(env.DEBUG);
21+
export const isDebug: boolean = toBoolean(env.DEBUG);
2222

2323
/** Detect if `NODE_ENV` environment variable is `test` */
24-
export const isTest = nodeENV === "test" || toBoolean(env.TEST);
24+
export const isTest: boolean = nodeENV === "test" || toBoolean(env.TEST);
2525

2626
/** Detect if `NODE_ENV` environment variable is `production` */
27-
export const isProduction = nodeENV === "production";
27+
export const isProduction: boolean = nodeENV === "production";
2828

2929
/** Detect if `NODE_ENV` environment variable is `dev` or `development` */
30-
export const isDevelopment = nodeENV === "dev" || nodeENV === "development";
30+
export const isDevelopment: boolean =
31+
nodeENV === "dev" || nodeENV === "development";
3132

3233
/** Detect if MINIMAL environment variable is set, running in CI or test or TTY is unavailable */
33-
export const isMinimal = toBoolean(env.MINIMAL) || isCI || isTest || !hasTTY;
34+
export const isMinimal: boolean =
35+
toBoolean(env.MINIMAL) || isCI || isTest || !hasTTY;
3436

3537
/** Detect if process.platform is Windows */
36-
export const isWindows = /^win/i.test(platform);
38+
export const isWindows: boolean = /^win/i.test(platform);
3739

3840
/** Detect if process.platform is Linux */
39-
export const isLinux = /^linux/i.test(platform);
41+
export const isLinux: boolean = /^linux/i.test(platform);
4042

4143
/** Detect if process.platform is macOS (darwin kernel) */
42-
export const isMacOS = /^darwin/i.test(platform);
44+
export const isMacOS: boolean = /^darwin/i.test(platform);
4345

4446
/** Color Support */
45-
export const isColorSupported =
47+
export const isColorSupported: boolean =
4648
!toBoolean(env.NO_COLOR) &&
4749
(toBoolean(env.FORCE_COLOR) ||
4850
((hasTTY || isWindows) && env.TERM !== "dumb") ||
4951
isCI);
5052

5153
/** Node.js versions */
52-
export const nodeVersion =
54+
export const nodeVersion: string | null =
5355
(globalThis.process?.versions?.node || "").replace(/^v/, "") || null;
54-
export const nodeMajorVersion = Number(nodeVersion?.split(".")[0]) || null;
56+
57+
export const nodeMajorVersion: number | null =
58+
Number(nodeVersion?.split(".")[0]) || null;

src/index.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
export * from "./env";
2-
export * from "./flags";
3-
export * from "./process";
4-
export * from "./providers";
5-
export * from "./runtimes";
1+
export * from "./env.ts";
2+
export * from "./flags.ts";
3+
export * from "./process.ts";
4+
export * from "./providers.ts";
5+
export * from "./runtimes.ts";

src/process.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { EnvObject, env } from "./env";
1+
import { type EnvObject, env } from "./env.ts";
22

33
export interface Process
44
extends Partial<Omit<typeof globalThis.process, "versions">> {
@@ -13,7 +13,7 @@ const processShims: Partial<Process> = {
1313
versions: {},
1414
};
1515

16-
export const process = new Proxy<Process>(_process, {
16+
export const process: Process = new Proxy<Process>(_process, {
1717
get(target, prop: keyof Process) {
1818
if (prop === "env") {
1919
return env;

src/providers.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,5 +152,5 @@ function _detectProvider(): ProviderInfo {
152152
}
153153

154154
/** Current provider info */
155-
export const providerInfo = _detectProvider();
155+
export const providerInfo: ProviderInfo = _detectProvider();
156156
export const provider: ProviderName = providerInfo.name;

src/runtimes.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,39 +18,40 @@ export type RuntimeInfo = { name: RuntimeName };
1818
*
1919
* Use `runtime === "node"` if you need strict check for Node.js runtime.
2020
*/
21-
export const isNode = globalThis.process?.release?.name === "node";
21+
export const isNode: boolean = globalThis.process?.release?.name === "node";
2222

2323
/**
2424
* Indicates if running in Bun runtime.
2525
*/
26-
export const isBun = !!globalThis.Bun || !!globalThis.process?.versions?.bun;
26+
export const isBun: boolean =
27+
!!globalThis.Bun || !!globalThis.process?.versions?.bun;
2728

2829
/**
2930
* Indicates if running in Deno runtime.
3031
*/
31-
export const isDeno = !!globalThis.Deno;
32+
export const isDeno: boolean = !!globalThis.Deno;
3233

3334
/**
3435
* Indicates if running in Fastly runtime.
3536
*/
36-
export const isFastly = !!globalThis.fastly;
37+
export const isFastly: boolean = !!globalThis.fastly;
3738

3839
/**
3940
* Indicates if running in Netlify runtime.
4041
*/
41-
export const isNetlify = !!globalThis.Netlify;
42+
export const isNetlify: boolean = !!globalThis.Netlify;
4243

4344
/**
4445
*
4546
* Indicates if running in EdgeLight (Vercel Edge) runtime.
4647
*/
47-
export const isEdgeLight = !!globalThis.EdgeRuntime;
48+
export const isEdgeLight: boolean = !!globalThis.EdgeRuntime;
4849
// https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent
4950

5051
/**
5152
* Indicates if running in Cloudflare Workers runtime.
5253
*/
53-
export const isWorkerd =
54+
export const isWorkerd: boolean =
5455
globalThis.navigator?.userAgent === "Cloudflare-Workers";
5556

5657
const runtimeChecks: [boolean, RuntimeName][] = [
@@ -71,6 +72,6 @@ function _detectRuntime(): RuntimeInfo | undefined {
7172
}
7273
}
7374

74-
export const runtimeInfo = _detectRuntime();
75+
export const runtimeInfo: RuntimeInfo | undefined = _detectRuntime();
7576

7677
export const runtime: RuntimeName = runtimeInfo?.name || "";

test/index.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { expect, it, describe } from "vitest";
2-
import * as stdEnv from "../src";
2+
import * as stdEnv from "../src/index.ts";
33

44
describe("std-env", () => {
55
it("has expected exports", () => {

tsconfig.json

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,22 @@
11
{
22
"compilerOptions": {
3-
"baseUrl": ".",
43
"target": "ESNext",
5-
"module": "ESNext",
6-
"skipLibCheck": true,
4+
"module": "NodeNext",
5+
"moduleResolution": "NodeNext",
6+
"resolveJsonModule": true,
7+
"esModuleInterop": false,
78
"allowSyntheticDefaultImports": true,
8-
"moduleResolution": "Node",
9+
"skipLibCheck": true,
910
"strict": true,
10-
"declaration": true,
11-
"types": ["node", "./src/_types"]
11+
"verbatimModuleSyntax": true,
12+
"isolatedModules": true,
13+
"composite": true,
14+
"allowImportingTsExtensions": true,
15+
"isolatedDeclarations": true,
16+
"forceConsistentCasingInFileNames": true,
17+
"noImplicitOverride": true,
18+
"noEmit": true,
19+
"erasableSyntaxOnly": true
1220
},
13-
"include": ["src", "test", "playground"]
21+
"include": ["src", "test"]
1422
}

0 commit comments

Comments
 (0)