Skip to content
This repository was archived by the owner on Dec 30, 2023. It is now read-only.

Commit d86ad4a

Browse files
authored
refactor(runtime): ♻️ Organize & remove more occurencies of RuntimeError (#93)
1 parent 9245a58 commit d86ad4a

File tree

13 files changed

+66
-66
lines changed

13 files changed

+66
-66
lines changed

.changeset/shaggy-bugs-unite.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@terminal-nerds/snippets-runtime": patch
3+
---
4+
5+
♻️ Remove more occurences of `RuntimeError` to fix CJS issues

packages/runtime/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141
"dist/"
4242
],
4343
"dependencies": {
44-
"@terminal-nerds/snippets-error": "workspace:*",
4544
"local-pkg": "0.4.3",
4645
"zod": "3.21.4"
4746
},

packages/runtime/source/environment/environment.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/* import { RuntimeError } from "@terminal-nerds/snippets-error/custom"; */
22
import { z } from "zod";
3+
34
/* prettier-ignore */
45
export const RUNTIME_ENVIRONMENTS = [
56
"browser",

packages/runtime/source/scope/scope.test.ts renamed to packages/runtime/source/scope/name.test.ts

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,11 @@ import { returns } from "@terminal-nerds/snippets-test/unit";
22
import { describe, expect, it } from "vitest";
33

44
import { deleteEnvironmentVariable, getEnvironmentVariable, setEnvironmentVariable } from "../variable/variable.ts";
5-
import {
6-
CI_CD_ENVIRONMENT_VARIABLES,
7-
inDevelopment,
8-
inProduction,
9-
isIn,
10-
type ScopeName,
11-
STORYBOOK_ENVIRONMENT_VARIABLES,
12-
TEST_ENVIRONMENT_VARIABLES,
13-
} from "./scope.ts";
5+
import { isIn, type ScopeName } from "./name.ts";
6+
import { CI_CD_ENVIRONMENT_VARIABLES } from "./scopes/continuous-integration.ts";
7+
import { inDevelopment, inProduction } from "./scopes/node.ts";
8+
import { STORYBOOK_ENVIRONMENT_VARIABLES } from "./scopes/storybook.ts";
9+
import { TEST_ENVIRONMENT_VARIABLES } from "./scopes/test.ts";
1410

1511
describe(`isIn("continuousIntegration", options?)`, () => {
1612
const scopeName: ScopeName = "continuousIntegration";

packages/runtime/source/scope/name.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/* import { RuntimeError } from "@terminal-nerds/snippets-error/custom"; */
2+
3+
import { IN_BROWSER } from "../environment/environment.ts";
4+
import { getEnvironmentVariable } from "../variable/variable.ts";
5+
import { CI_CD_ENVIRONMENT_VARIABLES } from "./scopes/continuous-integration.ts";
6+
import { STORYBOOK_ENVIRONMENT_VARIABLES } from "./scopes/storybook.ts";
7+
import { TEST_ENVIRONMENT_VARIABLES } from "./scopes/test.ts";
8+
import type { ScopeOptions } from "./shared.ts";
9+
10+
export const SCOPE_NAMES = ["continuousIntegration", "storybook", "test"] as const;
11+
export type ScopeName = (typeof SCOPE_NAMES)[number];
12+
export const SCOPE_VARIABLES = {
13+
continuousIntegration: CI_CD_ENVIRONMENT_VARIABLES,
14+
storybook: STORYBOOK_ENVIRONMENT_VARIABLES,
15+
test: TEST_ENVIRONMENT_VARIABLES,
16+
} as const;
17+
18+
export function isIn(scopeName: ScopeName, options: ScopeOptions = {}): boolean {
19+
const { strict = false } = options;
20+
21+
return IN_BROWSER ? handleBrowserRuntime(strict) : isAnyVariableDefined(SCOPE_VARIABLES[scopeName]);
22+
}
23+
24+
function handleBrowserRuntime(strict: boolean): boolean {
25+
if (strict) throw new Error(`You cannot use this snippet in the browser.`);
26+
else return false;
27+
}
28+
29+
function isAnyVariableDefined(names: readonly string[]): boolean {
30+
for (const variable of names) {
31+
if (getEnvironmentVariable(variable)) return true;
32+
}
33+
34+
return false;
35+
}
Lines changed: 5 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,5 @@
1-
import { RuntimeError } from "@terminal-nerds/snippets-error/custom";
2-
3-
import { IN_BROWSER } from "../environment/environment.ts";
4-
import { getEnvironmentVariable } from "../variable/variable.ts";
5-
import { CI_CD_ENVIRONMENT_VARIABLES } from "./groups/continuous-integration.ts";
6-
import { STORYBOOK_ENVIRONMENT_VARIABLES } from "./groups/storybook.ts";
7-
import { TEST_ENVIRONMENT_VARIABLES } from "./groups/test.ts";
8-
import type { ScopeOptions } from "./shared.ts";
9-
10-
export * from "./groups/continuous-integration.ts";
11-
export * from "./groups/node.ts";
12-
export * from "./groups/storybook.ts";
13-
export * from "./groups/test.ts";
14-
15-
export const SCOPE_NAMES = ["continuousIntegration", "storybook", "test"] as const;
16-
export type ScopeName = (typeof SCOPE_NAMES)[number];
17-
export const SCOPE_VARIABLES = {
18-
continuousIntegration: CI_CD_ENVIRONMENT_VARIABLES,
19-
storybook: STORYBOOK_ENVIRONMENT_VARIABLES,
20-
test: TEST_ENVIRONMENT_VARIABLES,
21-
} as const;
22-
23-
export function isIn(scopeName: ScopeName, options: ScopeOptions = {}): boolean {
24-
const { strict = false } = options;
25-
26-
return IN_BROWSER ? handleBrowserRuntime(strict) : isAnyVariableDefined(SCOPE_VARIABLES[scopeName]);
27-
}
28-
29-
function handleBrowserRuntime(strict: boolean): boolean {
30-
if (strict) throw new RuntimeError(`You cannot use this snippet in the browser.`);
31-
else return false;
32-
}
33-
34-
function isAnyVariableDefined(names: readonly string[]): boolean {
35-
for (const variable of names) {
36-
if (getEnvironmentVariable(variable)) return true;
37-
}
38-
39-
return false;
40-
}
1+
export * from "./name.ts";
2+
export * from "./scopes/continuous-integration.ts";
3+
export * from "./scopes/node.ts";
4+
export * from "./scopes/storybook.ts";
5+
export * from "./scopes/test.ts";

packages/runtime/source/scope/groups/node.ts renamed to packages/runtime/source/scope/scopes/node.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { RuntimeError } from "@terminal-nerds/snippets-error/custom";
1+
/* import { RuntimeError } from "@terminal-nerds/snippets-error/custom"; */
22

33
import { IN_NODE } from "../../environment/environment.ts";
44
import { getEnvironmentVariable } from "../../variable/variable.ts";
@@ -24,6 +24,6 @@ export function isNodeEnvironment(value: NodeEnvironment, options: ScopeOptions
2424
}
2525

2626
function handleNonNodeRuntime(strict: boolean) {
27-
if (strict) throw new RuntimeError(`Currently you can check only inside the Node.js runtime environment.`);
27+
if (strict) throw new Error(`Currently you can check only inside the Node.js runtime environment.`);
2828
else return false;
2929
}

0 commit comments

Comments
 (0)