feat: ✨ add namespace prefix filtering to createEnvParser - #213
Merged
Conversation
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>
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 Report✅ All modified and coverable lines are covered by tests. @@ 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
🚀 New features to boost your workflow:
|
Signed-off-by: Newton <5769156+iamnewton@users.noreply.github.com>
Signed-off-by: Newton <5769156+iamnewton@users.noreply.github.com>
Contributor
Author
|
🎉 This PR is included in version 1.2.0 🎉 The release is available on:
Your semantic-release bot 📦🚀 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
namespaces?: string[]toEnvParserOptions_) are visible to the parser — the prefix is stripped before key normalisationfilterByNamespaceandmergeNamespacesare exported from the package root for direct useMotivation
CLIs built from the
cli-templateeach have their own namespace (e.g.CLI_TEMPLATE_). But you may also want a shared org-wide namespace (e.g.HOLOCRON_) set once in~/.bashrcthat any tool can fall back to without copying vars into every project's.env.Test plan
pnpm --filter @theholocron/env-utils build— clean buildpnpm --filter @theholocron/env-utils test— 138/138 tests pass (10 new in filter-by-namespace, 6 new in create-env-parser)