Skip to content

feat: persist the active environment in .env.local#210

Open
angeloashmore wants to merge 1 commit into
mainfrom
feat/persist-active-environment
Open

feat: persist the active environment in .env.local#210
angeloashmore wants to merge 1 commit into
mainfrom
feat/persist-active-environment

Conversation

@angeloashmore

@angeloashmore angeloashmore commented Jul 2, 2026

Copy link
Copy Markdown
Member

Resolves: #209

Description

Environments only had low-level controls: --env worked per command but nothing persisted it, so a dev had to remember which environment they were in and repeat the flag. This adds a persisted, shared active environment.

  • prismic env list — list environments, including production, marking the active one
  • prismic env set <name> — set the active environment (writes it to .env.local)
  • prismic env unset — revert to production (removes it from .env.local)
  • prismic env active — print the active environment

The active environment is stored as one framework-specific variable in .env.local, resolved by the adapter (NEXT_PUBLIC_PRISMIC_ENVIRONMENT, NUXT_PUBLIC_PRISMIC_ENVIRONMENT, VITE_PRISMIC_ENVIRONMENT). The CLI reads it as the default repository (after --env and --repo, before the prismic.config.json default). Generated Next.js and SvelteKit projects read the same variable natively; Nuxt reads it through @nuxtjs/prismic. The names match Slice Machine, so existing .env.local files stay compatible.

Checklist

  • A comprehensive Linear ticket, providing sufficient context and details to facilitate the review of the PR, is linked to the PR.
  • If my changes require tests, I added them.
  • If my changes affect backward compatibility, it has been discussed.
  • If my changes require an update to the CONTRIBUTING.md guide, I updated it.

Preview

How to QA 1

In a Next.js or SvelteKit project with environments:

  1. prismic env list — production is marked active.
  2. prismic env set <environment> — writes the variable to .env.local; prismic env active reflects it, and content commands target it.
  3. Run the dev server — the site reads the active environment.
  4. prismic env unset — reverts to production.

Note

Medium Risk
Default repository resolution now affects many API commands (push/pull/sync, tokens, webhooks), so a stale .env.local could target the wrong environment; changes are local-dev focused and --repo/--env still override.

Overview
Adds a prismic env command group so local dev can keep a shared active environment instead of passing --env on every command.

env list, env set, env unset, and env active read and write a framework-specific variable in .env.local (NEXT_PUBLIC_PRISMIC_ENVIRONMENT, NUXT_PUBLIC_PRISMIC_ENVIRONMENT, or VITE_PRISMIC_ENVIRONMENT). Setting production removes the variable. Adapters expose repositoryEnvVar; new lib/env-file helpers update the file.

resolveRepositoryName() (active env from process.env / .env.local, else prismic.config.json) replaces getRepositoryName() as the default target for push/pull/sync/status and locale, preview, token, and webhook commands. --env / --repo still override per invocation.

Scaffolded Next.js and SvelteKit prismicio templates prefer the same env var for repositoryName, aligning the dev server with CLI and Slice Machine naming.

Reviewed by Cursor Bugbot for commit 4a511c6. Bugbot is set up for automated code reviews on this repo. Configure here.

Footnotes

  1. Please use these labels when submitting a review:
    ❓ #ask: Ask a question.
    💡 #idea: Suggest an idea.
    ⚠️ #issue: Strongly suggest a change.
    🎉 #nice: Share a compliment.

Comment thread src/commands/push.ts

export default createCommand(config, async ({ values }) => {
const { force = false, repo: parentRepo = await getRepositoryName(), env } = values;
const { force = false, repo: parentRepo = await resolveRepositoryName(), env } = values;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Active env breaks --env resolution

Medium Severity

The parentRepo variable, now resolved by resolveRepositoryName(), can incorrectly default to an active environment's domain from .env.local instead of the production repository. This happens when --repo is not provided, causing resolveEnvironment to use the wrong base repository and potentially fail or target incorrect environments for prismic push and prismic pull.

Additional Locations (1)
Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit 8820fb6. Configure here.

Comment thread src/environments.ts

/** The repository name to target by default, honoring the active environment. */
export async function resolveRepositoryName(): Promise<string> {
return (await getActiveRepositoryName()) ?? (await getRepositoryName());

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Empty active env breaks repo

Medium Severity

resolveRepositoryName uses nullish coalescing, so an empty NEXT_PUBLIC_PRISMIC_ENVIRONMENT (or adapter equivalent) in .env.local is treated as the repository name instead of falling back to prismic.config.json, leading to invalid API hosts and broken commands.

Additional Locations (1)
Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit 8820fb6. Configure here.

@angeloashmore angeloashmore force-pushed the feat/persist-active-environment branch from 8820fb6 to b7a5949 Compare July 2, 2026 21:46

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes using high effort and found 1 potential issue.

There are 3 total unresolved issues (including 2 from previous reviews).

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit b7a5949. Configure here.

Comment thread src/commands/pull.ts

export default createCommand(config, async ({ values }) => {
const { force = false, repo: parentRepo = await getRepositoryName(), env } = values;
const { force = false, repo: parentRepo = await resolveRepositoryName(), env } = values;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

--env uses active staging parent

Medium Severity

When --env is used without --repo and a staging environment is active, the parentRepo incorrectly resolves to the staging domain. This causes environment resolution to query the staging repository's API instead of the production one, leading to incorrect or failed lookups.

Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit b7a5949. Configure here.

Add `prismic env list/set/unset/active` to manage the active environment,
persisted as a framework-specific variable in `.env.local`. The CLI reads it
as the default repository (after `--env` and `--repo`, before the config
default), and generated Next.js and SvelteKit projects read it natively. Nuxt
reads it through `@nuxtjs/prismic`.

Resolves: #209

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat: better environment support

1 participant