Skip to content

feat: ✨ add namespace prefix filtering to createEnvParser - #213

Merged
iamnewton merged 5 commits into
mainfrom
feat/env-utils-namespaces
Aug 7, 2026
Merged

feat: ✨ add namespace prefix filtering to createEnvParser#213
iamnewton merged 5 commits into
mainfrom
feat/env-utils-namespaces

Conversation

@iamnewton

Copy link
Copy Markdown
Contributor

Summary

  • Adds namespaces?: string[] to EnvParserOptions
  • When set, only env vars whose names start with one of those prefixes (case-insensitive, followed by _) are visible to the parser — the prefix is stripped before key normalisation
  • Priority order: first namespace in the array wins when the same key appears under multiple prefixes
  • New utilities filterByNamespace and mergeNamespaces are exported from the package root for direct use

Motivation

CLIs built from the cli-template each have their own namespace (e.g. CLI_TEMPLATE_). But you may also want a shared org-wide namespace (e.g. HOLOCRON_) set once in ~/.bashrc that any tool can fall back to without copying vars into every project's .env.

// CLI_TEMPLATE_DEBUG overrides HOLOCRON_DEBUG; HOLOCRON_VERBOSE is still visible
const parser = createEnvParser({
  appName: "my-cli",
  namespaces: ["CLI_TEMPLATE", "HOLOCRON"],
});
const debug = parser.get("debug");   // CLI_TEMPLATE_DEBUG ?? HOLOCRON_DEBUG
const verbose = parser.get("verbose"); // HOLOCRON_VERBOSE

Test plan

  • pnpm --filter @theholocron/env-utils build — clean build
  • pnpm --filter @theholocron/env-utils test — 138/138 tests pass (10 new in filter-by-namespace, 6 new in create-env-parser)
  • CI lint, typecheck, test pass

Adds a `namespaces` option to `EnvParserOptions` that filters and
strips namespace prefixes from raw env var keys before parsing.

When `namespaces: ["CLI_TEMPLATE", "HOLOCRON"]` is set:
- only vars starting with one of those prefixes are visible
- the prefix is stripped so CLI_TEMPLATE_DEBUG becomes get("debug")
- first namespace wins: CLI_TEMPLATE_DEBUG overrides HOLOCRON_DEBUG
- HOLOCRON_* acts as a global fallback (e.g. set in ~/.bashrc) without
  needing to copy vars into each project's .env

New utilities:
- filterByNamespace(raw, namespace) — filter + strip a single prefix
- mergeNamespaces(raw, namespaces) — merge in priority order
Both are exported from the package root for direct use.

Signed-off-by: Newton <vj2kp2hfh4@privaterelay.appleid.com>
Signed-off-by: Newton <5769156+iamnewton@users.noreply.github.com>
@iamnewton
iamnewton requested a review from a team as a code owner August 6, 2026 17:43
Changes the priority model from "first wins" to "last wins" to match
the CSS/Object.assign cascade convention: put broad/global namespaces
first, project-specific overrides last.

  namespaces: ["HOLOCRON", "CLI_TEMPLATE"]
  //            ^ org base   ^ local override (last = wins)

Updates the JSDoc, examples, and all tests accordingly.

Signed-off-by: Newton <vj2kp2hfh4@privaterelay.appleid.com>
Signed-off-by: Newton <5769156+iamnewton@users.noreply.github.com>
Signed-off-by: Newton <5769156+iamnewton@users.noreply.github.com>
@codecov

codecov Bot commented Aug 6, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #213      +/-   ##
==========================================
+ Coverage   97.30%   97.43%   +0.12%     
==========================================
  Files          21       22       +1     
  Lines         223      234      +11     
  Branches       55       57       +2     
==========================================
+ Hits          217      228      +11     
  Partials        6        6              
Files with missing lines Coverage Δ
packages/env-utils/src/create-env-parser.ts 100.00% <100.00%> (ø)
...ackages/env-utils/src/utils/filter-by-namespace.ts 100.00% <100.00%> (ø)
🚀 New features to boost your workflow:
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Signed-off-by: Newton <5769156+iamnewton@users.noreply.github.com>
Signed-off-by: Newton <5769156+iamnewton@users.noreply.github.com>
@iamnewton
iamnewton merged commit 8a43f49 into main Aug 7, 2026
31 checks passed
@iamnewton
iamnewton deleted the feat/env-utils-namespaces branch August 7, 2026 02:36
@iamnewton

Copy link
Copy Markdown
Contributor Author

🎉 This PR is included in version 1.2.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

@iamnewton iamnewton added the released Included in a release label Aug 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

released Included in a release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant