Skip to content

Commit

Permalink
Merge branch 'release/release/0.13.11'
Browse files Browse the repository at this point in the history
  • Loading branch information
holtwick committed Sep 15, 2023
2 parents 3d21c31 + 83abbfb commit 97718ed
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 21 deletions.
1 change: 1 addition & 0 deletions demos/logging/.env.test
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DEMO_SECRET = test
4 changes: 1 addition & 3 deletions demos/logging/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ import { Logger, setupEnv } from 'zeed'

// Read .env file

Logger.setDebug()

setupEnv()
setupEnv({ mode: 'test' })

// Some basic logging

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "zeed",
"type": "module",
"version": "0.13.10",
"version": "0.13.11",
"description": "🌱 Simple foundation library",
"author": {
"name": "Dirk Holtwick",
Expand Down
43 changes: 26 additions & 17 deletions src/node/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,19 @@ const RE_INI_KEY_VAL = /^\s*([\w_.-]+)\s*=\s*(.*)?\s*$/
const RE_NEWLINES = /\\n/g
const NEWLINES_MATCH = /\n|\r|\r\n/

interface csvOptions {
interface EnvOptions {
/** @deprecated will probably be replaced by logLevel */
debug?: boolean
path?: string
filename?: string
encoding?: BufferEncoding
prefix?: string
env?: Record<string, string>
mode?: string
}

// Parses src into an Object
function parse(src: string, _options: csvOptions = {}) {
function parse(src: string, _options: EnvOptions = {}) {
const obj: Record<string, string> = {}

// convert Buffers before splitting into lines and processing
Expand Down Expand Up @@ -99,26 +100,34 @@ export function getEnvVariableRelaxed(
}

/** Populates process.env from .env file. */
export function setupEnv(options: csvOptions = {}) {
const dotenvPath: string
= options?.path ?? toPath(options?.filename ?? '.env')
export function setupEnv(options: EnvOptions = {}) {
const dotenvPath: string = options?.path ?? toPath(options?.filename ?? '.env')
const encoding: BufferEncoding = options?.encoding ?? 'utf8'
const debug = options?.debug || false

try {
// specifying an encoding returns a string instead of a buffer
const parsedEnv = fs.existsSync(dotenvPath)
? parse(fs.readFileSync(dotenvPath, { encoding }), { debug })
: {}
const parsedEnvLocal = fs.existsSync(`${dotenvPath}.local`)
? parse(fs.readFileSync(`${dotenvPath}.local`, { encoding }), { debug })
: {}

const parsed: Record<string, string> = Object.assign(
{},
parsedEnv,
parsedEnvLocal,
const parsed: Record<string, string> = {}

function envOf(name: string) {
return fs.existsSync(name)
? parse(fs.readFileSync(name, { encoding }), { debug })
: {}
}

Object.assign(
parsed,
envOf(dotenvPath),
envOf(`${dotenvPath}.local`),
)

if (options.mode) {
Object.assign(
parsed,
envOf(`${dotenvPath}.${options.mode}`),
envOf(`${dotenvPath}.${options.mode}.local`),
)
}

const env = options?.env ?? process.env

Object.entries(parsed).forEach(([key, value]) => {
Expand Down

0 comments on commit 97718ed

Please sign in to comment.