env_files.ts

view source

Declarations
#

5 declarations

load_env
#

env_files.ts view source

(fs_paths: FsPaths, { dev, env_dir, env_files, ambient_env }: LoadEnvOptions): Promise<Record<string, string>> import {load_env} from '@fuzdev/gro/env_files.js';

Loads every env var from the env files plus the ambient env, later sources winning, so the ambient env overrides the files. A missing env file is skipped; one that can't be read throws.

fs_paths

reads the env files, a relative env_dir or file resolved against its project

type FsPaths

__1

returns

Promise<Record<string, string>>

all loaded vars, unfiltered - resolve_env_vars picks out the declared ones

throws

  • FsError - when reading an env file fails other than by its absence

load_env_file
#

env_files.ts view source

(fs_paths: FsPaths, path: string): Promise<Record<string, string> | null> import {load_env_file} from '@fuzdev/gro/env_files.js';

Loads one env file's vars, parsed as dotenv, without merging them into anything.

fs_paths

reads the file, a relative path resolved against its project

type FsPaths

path

absolute, or relative to the project

type string

returns

Promise<Record<string, string> | null>

the file's vars, or null when the file doesn't exist

throws

  • FsError - when reading the file fails other than by its absence

load_from_env
#

env_files.ts view source

(fs_paths: FsPaths, ambient_env: Readonly<Record<string, string | undefined>>, key: string, paths?: string[]): Promise<string | undefined> import {load_from_env} from '@fuzdev/gro/env_files.js';

Loads a single env value without merging it into the ambient env. Searches ambient_env, then each of paths that exists, by default only the project's .env - never a parent directory's, so one directory's .env isn't a secret channel to every repo under it. Empty strings are semantically the same as undefined for more ergonomic fallbacks.

fs_paths

reads the env files, a relative path resolved against its project

type FsPaths

ambient_env

a plain record a root builds, needing only key

type Readonly<Record<string, string | undefined>>

key

type string

paths?

absolute, or relative to the project

type string[]
optional default ['.env']

returns

Promise<string | undefined>

throws

  • FsError - when reading one of `paths` fails other than by its absence

resolve_env_vars
#

env_files.ts view source

(variables: EnvVarDeclarations, visibility: EnvVisibility, env: Record<string, string | undefined>): Record<string, unknown> import {resolve_env_vars} from '@fuzdev/gro/env_files.js';

Resolves the declared env vars of one visibility from env, validating each the way SvelteKit does: a var with no schema must be present, and one with a schema gets its Standard Schema result, so validators can transform values and make them optional.

variables

the declarations from the project's src/env.ts

visibility

which of the declared vars to resolve

env

the values to resolve them from, usually from load_env

type Record<string, string | undefined>

returns

Record<string, unknown>

the resolved values keyed by var name

throws

  • listing - every invalid var, so a misconfigured env fails all at once

to_default_env_files
#

Depends on
#

Imported by
#