{"version":3,"sources":["../../source/environment/environment.ts","../../source/variable/variable.ts","../../source/scope/scopes/continuous-integration.ts","../../source/scope/scopes/storybook.ts","../../source/scope/scopes/test.ts","../../source/scope/name.ts","../../source/scope/scopes/node.ts"],"names":["z","RUNTIME_ENVIRONMENTS","RUNTIME_ENVIRONMENT_SCHEMA","IN_BUN","IN_DENO","IN_DOM","IN_EDGE","IN_JSDOM","IN_HAPPY_DOM","IN_NODE","IN_BROWSER","IN_WEB_WORKER","getRuntimeEnvironmentName","conditions","name","isTrue","SUPPORTED_RUNTIMES","isSupportedRuntime","getSupportedRuntime","runtimeName","getEnvironmentVariables","toMap","v","getEnvironmentVariable","variable","options","strict","value","parseEnvironmentVariableValue","asNumber","CI_CD_ENVIRONMENT_VARIABLES","CI_CD_ENVIRONMENT_VARIABLE_SCHEMA","STORYBOOK_ENVIRONMENT_VARIABLES","STORYBOOK_ENVIRONMENT_VARIABLE_SCHEMA","TEST_ENVIRONMENT_VARIABLES","TEST_ENVIRONMENT_VARIABLE_SCHEMA","SCOPE_NAMES","SCOPE_VARIABLES","isIn","scopeName","handleBrowserRuntime","isAnyVariableDefined","names","inDevelopment","isNodeEnvironment","inProduction","handleNonNodeRuntime"],"mappings":"AACA,OAAS,KAAAA,MAAS,MAGX,IAAMC,EAAuB,CACnC,UACA,MACA,OACA,MACA,OACA,YACA,QACA,OACA,YACD,EAEaC,EAA6BF,EAAE,KAAKC,CAAoB,EAKxDE,EAAS,WAAW,MAAQ,QAAa,IAAI,UAAY,OAKzDC,EAAU,WAAW,OAAS,QAAa,KAAK,UAAY,QAAa,KAAK,QAAQ,OAAS,OAK/FC,EACZ,WAAW,SAAW,QACtB,CAAC,WAAW,YAAc,QAE1B,OAAO,WAAa,OAMRC,EAAU,WAAW,cAAgB,OAKrCC,EAAWF,GAAU,UAAU,UAAU,SAAS,OAAO,EAMzDG,EAAeH,GAAU,OAAO,WAAa,OAK7CI,EAAU,WAAW,UAAY,QAAa,QAAQ,UAAY,OAKlEC,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AEA,SAASoB,EAAqBpB,EAAiB,CAC9C,GAAIA,EAAQ,MAAM,IAAI,MAAM,sEAAsE,EAC7F,MAAO,EACb","sourcesContent":["/* import { RuntimeError } from \"@terminal-nerds/snippets-error/custom\"; */\nimport { z } from \"zod\";\n\n/* prettier-ignore */\nexport const RUNTIME_ENVIRONMENTS = [\n\t\"browser\",\n\t\"bun\",\n\t\"deno\",\n\t\"dom\",\n\t\"edge\",\n\t\"happy-dom\",\n\t\"jsdom\",\n\t\"node\",\n\t\"web-worker\",\n] as const;\nexport type RuntimeEnvironment = (typeof RUNTIME_ENVIRONMENTS)[number];\nexport const RUNTIME_ENVIRONMENT_SCHEMA = z.enum(RUNTIME_ENVIRONMENTS);\n\n/**\n * @see {@link https://bun.sh/docs/api/globals}\n */\nexport const IN_BUN = globalThis.Bun !== undefined && Bun.version !== undefined;\n\n/**\n * @see {@link https://deno.land/manual@v1.31.1/runtime#deno-global}\n */\nexport const IN_DENO = globalThis.Deno !== undefined && Deno.version !== undefined && Deno.version.deno !== undefined;\n\n/**\n * Detects if the DOM is available.\n */\nexport const IN_DOM =\n\tglobalThis.window !== undefined &&\n\t!globalThis.navigator !== undefined &&\n\t// @ts-ignore FIXME: Find a way to make it typed?\n\twindow.document !== undefined;\n\n/**\n * @see {@link https://edge-runtime.vercel.app/features/available-apis#addressing-the-runtime}\n */\n// @ts-ignore FIXME: Find a way to make it typed?\nexport const IN_EDGE = globalThis.EdgeRuntime !== undefined;\n\n/**\n * @see {@link https://github.com/jsdom/jsdom}\n */\nexport const IN_JSDOM = IN_DOM && navigator.userAgent.includes(\"jsdom\");\n\n/**\n * @see {@link https://github.com/capricorn86/happy-dom/discussions/481}\n */\n// @ts-ignore FIXME: Find a way to make it typed?\nexport const IN_HAPPY_DOM = IN_DOM && window.happyDOM !== undefined;\n\n/**\n * @see {@link https://nodejs.org/dist/latest-v18.x/docs/api/process.html#process}\n */\nexport const IN_NODE = globalThis.process !== undefined && process.version !== undefined;\n\n/**\n * Detect if is running in browser, and is not in a browser emulator (e.g. jsdom or Happy DOM)\n */\nexport const IN_BROWSER = IN_DOM && !IN_HAPPY_DOM && !IN_JSDOM;\n\n/**\n * @see {@link https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Using_web_workers}\n */\nexport const IN_WEB_WORKER =\n\ttypeof self === \"object\" && self.constructor && self.constructor.name === \"DedicatedWorkerGlobalScope\";\n\n/**\n * Receive the name _(in lowercase)_ of the currently running JavaScript environment.\n */\nexport function getRuntimeEnvironmentName(): RuntimeEnvironment {\n\t/**\n\t * NOTE: Order matters!\n\t */\n\tconst conditions = {\n\t\t\"edge\": IN_EDGE,\n\t\t\"bun\": IN_BUN,\n\t\t\"happy-dom\": IN_HAPPY_DOM,\n\t\t\"jsdom\": IN_JSDOM,\n\t\t\"deno\": IN_DENO,\n\t\t\"browser\": IN_BROWSER,\n\t\t\"node\": IN_NODE,\n\t\t\"web-worker\": IN_WEB_WORKER,\n\t\t\"dom\": IN_DOM,\n\t} as const;\n\n\tfor (const [name, isTrue] of Object.entries(conditions)) {\n\t\tif (isTrue) return name as RuntimeEnvironment;\n\t}\n\n\tthrow new Error(`Unrecognized JavaScript runtime environment!`);\n}\n\nexport function validateRuntimeEnvironmentName(name: string): asserts name is RuntimeEnvironment {\n\tRUNTIME_ENVIRONMENT_SCHEMA.parse(name);\n}\n\nexport function isValidRuntimeEnvironmentName(name: string): name is RuntimeEnvironment {\n\treturn RUNTIME_ENVIRONMENT_SCHEMA.safeParse(name).success;\n}\n","/* import { RuntimeError } from \"@terminal-nerds/snippets-error/custom\"; */\n\nimport { getRuntimeEnvironmentName, type RuntimeEnvironment } from \"../environment/environment.ts\";\n\nconst SUPPORTED_RUNTIMES = [\"bun\", \"deno\", \"node\"] as const;\ntype SupportedRuntime = Extract<RuntimeEnvironment, (typeof SUPPORTED_RUNTIMES)[number]>;\n\nfunction isSupportedRuntime(name: RuntimeEnvironment): name is SupportedRuntime {\n\treturn new Set(SUPPORTED_RUNTIMES).has(name);\n}\n\nfunction getSupportedRuntime(): SupportedRuntime {\n\tconst runtimeName = getRuntimeEnvironmentName();\n\n\tif (isSupportedRuntime(runtimeName)) {\n\t\treturn runtimeName;\n\t} else {\n\t\tthrow new Error(`This snippet cannot be run in a runtime: ${runtimeName}!`);\n\t}\n}\n\nexport function getEnvironmentVariables() {\n\t/* prettier-ignore */\n\tswitch (getSupportedRuntime()) {\n\t\t/* eslint-disable unicorn/switch-case-braces */\n\t\tcase \"bun\": return Bun.env;\n\t\tcase \"deno\": return Deno.env.toObject();\n\t\tcase \"node\": return process.env;\n\t\t/* eslint-enable unicorn/switch-case-braces */\n\t}\n}\n\nconst toMap = (v: Record<string, string | undefined>) => new Map<string, string | undefined>(Object.entries(v));\n\nexport interface EnvironmentVariableOptions {\n\t/**\n\t * Strict checking. Will throw error if the variable name is non-existent.\n\t *\n\t * @defaultValue `false`\n\t */\n\tstrict?: boolean;\n}\n\n/**\n * Possible value types of the environment variable.\n */\nexport type EnvironmentVariableValue = boolean | number | string;\n\nexport function hasEnvironmentVariable(variable: string): boolean {\n\treturn toMap(getEnvironmentVariables()).has(variable);\n}\n\nexport function getEnvironmentVariable<ValueType extends EnvironmentVariableValue>(\n\t_variable: string,\n\t_options?: { strict?: false },\n): ValueType | undefined;\nexport function getEnvironmentVariable<ValueType extends EnvironmentVariableValue>(\n\t_variable: string,\n\t_options: { strict: true },\n): ValueType;\nexport function getEnvironmentVariable<ValueType extends EnvironmentVariableValue>(\n\tvariable: string,\n\toptions: EnvironmentVariableOptions = {},\n): ValueType | undefined {\n\tconst { strict = false } = options;\n\n\tconst value = toMap(getEnvironmentVariables()).get(variable);\n\n\tif (value) {\n\t\treturn parseEnvironmentVariableValue(value) as ValueType;\n\t} else if (strict) {\n\t\tthrow new Error(`The environment variable - \"${variable}\" - is not set. Getter aborted.`);\n\t}\n}\n\ntype ParsedEnvVariableValue<Value extends string> = Value extends \"true\"\n\t? true\n\t: Value extends \"false\"\n\t? false\n\t: Value extends `${number}`\n\t? number\n\t: string;\n\nfunction parseEnvironmentVariableValue<Value extends string>(value: Value): ParsedEnvVariableValue<Value> {\n\tif (value === \"true\") return true as ParsedEnvVariableValue<Value>;\n\telse if (value === \"false\") return false as ParsedEnvVariableValue<Value>;\n\telse {\n\t\tconst asNumber = Number(value);\n\n\t\treturn (Number.isNaN(asNumber) ? value : asNumber) as ParsedEnvVariableValue<Value>;\n\t}\n}\n\nexport function setEnvironmentVariable(name: string, value: EnvironmentVariableValue): void {\n\tswitch (getSupportedRuntime()) {\n\t\tcase \"bun\": {\n\t\t\tBun.env[name] = String(value);\n\t\t\tbreak;\n\t\t}\n\t\tcase \"deno\": {\n\t\t\tDeno.env.set(name, String(value));\n\t\t\tbreak;\n\t\t}\n\t\tcase \"node\": {\n\t\t\tglobalThis.process.env[name] = String(value);\n\t\t\tbreak;\n\t\t}\n\t}\n}\n\nexport function deleteEnvironmentVariable(name: string, options: EnvironmentVariableOptions = {}): void {\n\tconst { strict = false } = options;\n\n\tif (hasEnvironmentVariable(name)) {\n\t\tswitch (getSupportedRuntime()) {\n\t\t\tcase \"bun\": {\n\t\t\t\tdelete Bun.env[name];\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\tcase \"deno\": {\n\t\t\t\tDeno.env.delete(name);\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\tcase \"node\": {\n\t\t\t\tdelete globalThis.process.env[name];\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t} else if (strict) {\n\t\tthrow new Error(`The environment variable name: \"${name}\" does not exist. Deletion aborted.`);\n\t}\n}\n","import { z } from \"zod\";\n\nexport const CI_CD_ENVIRONMENT_VARIABLES = [\"CI\", \"CONTINUOUS_INTEGRATION\"] as const;\nexport type CICDEnvironmentVariable = (typeof CI_CD_ENVIRONMENT_VARIABLES)[number];\nexport const CI_CD_ENVIRONMENT_VARIABLE_SCHEMA = z.enum(CI_CD_ENVIRONMENT_VARIABLES);\n","import { z } from \"zod\";\n\nexport const STORYBOOK_ENVIRONMENT_VARIABLES = [\"STORYBOOK\"] as const;\nexport type StorybookEnvironmentVariable = (typeof STORYBOOK_ENVIRONMENT_VARIABLES)[number];\nexport const STORYBOOK_ENVIRONMENT_VARIABLE_SCHEMA = z.enum(STORYBOOK_ENVIRONMENT_VARIABLES);\n","import { z } from \"zod\";\n\nexport const TEST_ENVIRONMENT_VARIABLES = [\"JEST\", \"VITEST\"] as const;\nexport type TestEnvironmentVariable = (typeof TEST_ENVIRONMENT_VARIABLES)[number];\nexport const TEST_ENVIRONMENT_VARIABLE_SCHEMA = z.enum(TEST_ENVIRONMENT_VARIABLES);\n","/* import { RuntimeError } from \"@terminal-nerds/snippets-error/custom\"; */\n\nimport { IN_BROWSER } from \"../environment/environment.ts\";\nimport { getEnvironmentVariable } from \"../variable/variable.ts\";\nimport { CI_CD_ENVIRONMENT_VARIABLES } from \"./scopes/continuous-integration.ts\";\nimport { STORYBOOK_ENVIRONMENT_VARIABLES } from \"./scopes/storybook.ts\";\nimport { TEST_ENVIRONMENT_VARIABLES } from \"./scopes/test.ts\";\nimport type { ScopeOptions } from \"./shared.ts\";\n\nexport const SCOPE_NAMES = [\"continuousIntegration\", \"storybook\", \"test\"] as const;\nexport type ScopeName = (typeof SCOPE_NAMES)[number];\nexport const SCOPE_VARIABLES = {\n\tcontinuousIntegration: CI_CD_ENVIRONMENT_VARIABLES,\n\tstorybook: STORYBOOK_ENVIRONMENT_VARIABLES,\n\ttest: TEST_ENVIRONMENT_VARIABLES,\n} as const;\n\nexport function isIn(scopeName: ScopeName, options: ScopeOptions = {}): boolean {\n\tconst { strict = false } = options;\n\n\treturn IN_BROWSER ? handleBrowserRuntime(strict) : isAnyVariableDefined(SCOPE_VARIABLES[scopeName]);\n}\n\nfunction handleBrowserRuntime(strict: boolean): boolean {\n\tif (strict) throw new Error(`You cannot use this snippet in the browser.`);\n\telse return false;\n}\n\nfunction isAnyVariableDefined(names: readonly string[]): boolean {\n\tfor (const variable of names) {\n\t\tif (getEnvironmentVariable(variable)) return true;\n\t}\n\n\treturn false;\n}\n","/* import { RuntimeError } from \"@terminal-nerds/snippets-error/custom\"; */\n\nimport { IN_NODE } from \"../../environment/environment.ts\";\nimport { getEnvironmentVariable } from \"../../variable/variable.ts\";\nimport type { ScopeOptions } from \"../shared.ts\";\n\nexport function inDevelopment(options: ScopeOptions = {}): boolean {\n\treturn isNodeEnvironment(\"development\", options);\n}\n\nexport function inProduction(options: ScopeOptions = {}): boolean {\n\treturn isNodeEnvironment(\"production\", options);\n}\n\nexport type NodeEnvironment = \"development\" | \"production\";\n\nexport function isNodeEnvironment(value: NodeEnvironment, options: ScopeOptions = {}): boolean {\n\tconst { strict = false } = options;\n\n\t/* prettier-ignore */\n\treturn IN_NODE\n        ? getEnvironmentVariable(\"NODE_ENV\", { strict: true }) === value\n        : handleNonNodeRuntime(strict);\n}\n\nfunction handleNonNodeRuntime(strict: boolean) {\n\tif (strict) throw new Error(`Currently you can check only inside the Node.js runtime environment.`);\n\telse return false;\n}\n"]}