-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add prismic env commands and CLI-owned active environment
#211
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
angeloashmore
wants to merge
5
commits into
main
Choose a base branch
from
feat/env-commands
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
037e32e
feat: add `prismic env` commands and CLI-owned active environment
angeloashmore 801961e
chore: blank line after the register import
angeloashmore abefdfb
refactor: address review feedback
angeloashmore f75bf93
refactor: rename getEnvironments to getUserEnvironments
angeloashmore ef8b75b
refactor: clearer names for env register import and active env
angeloashmore File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| import { mkdirSync, readFileSync, realpathSync, writeFileSync } from "node:fs"; | ||
| import { fileURLToPath } from "node:url"; | ||
|
|
||
| import { getConfigDir } from "./lib/config-dir"; | ||
| import { findUpwardSync } from "./lib/file"; | ||
| import { stringify } from "./lib/json"; | ||
|
|
||
| // The active environment lives in the CLI, keyed by project so nothing lands in | ||
| // the repo. Reads are synchronous so a framework config can set the environment | ||
| // variable before the framework reads it at build time. This module is published | ||
| // as `prismic/env`, so it stays dependency-light. | ||
|
|
||
| const CONFIG_FILENAME = "prismic.config.json"; | ||
|
|
||
| const ENVIRONMENTS_PATH = new URL( | ||
| "environments.json", | ||
| getConfigDir("prismic", process.env.PRISMIC_CONFIG_DIR), | ||
| ); | ||
|
|
||
| const FRAMEWORK_ENV_VARS: Record<string, string> = { | ||
| next: "NEXT_PUBLIC_PRISMIC_ENVIRONMENT", | ||
| nuxt: "NUXT_PUBLIC_PRISMIC_ENVIRONMENT", | ||
| "@sveltejs/kit": "VITE_PRISMIC_ENVIRONMENT", | ||
| }; | ||
|
|
||
| export function getActiveEnvironment(): string | undefined { | ||
| const project = findProjectRoot(); | ||
| if (!project) return undefined; | ||
| return readState()[project]; | ||
| } | ||
|
|
||
| export function setActiveEnvironment(domain: string): void { | ||
| const project = findProjectRoot(); | ||
| if (!project) return; | ||
| const state = readState(); | ||
| state[project] = domain; | ||
| writeState(state); | ||
| } | ||
|
|
||
| export function unsetActiveEnvironment(): void { | ||
| const project = findProjectRoot(); | ||
| if (!project) return; | ||
| const state = readState(); | ||
| if (!(project in state)) return; | ||
| delete state[project]; | ||
| writeState(state); | ||
| } | ||
|
|
||
| export function getFrameworkEnvVar(): string | undefined { | ||
| const packageJson = readPackageJson(); | ||
| if (!packageJson) return undefined; | ||
| const dependencies = { | ||
| ...packageJson.dependencies, | ||
| ...packageJson.devDependencies, | ||
| ...packageJson.peerDependencies, | ||
| }; | ||
| for (const dependency in FRAMEWORK_ENV_VARS) { | ||
| if (dependency in dependencies) return FRAMEWORK_ENV_VARS[dependency]; | ||
| } | ||
| return undefined; | ||
| } | ||
|
|
||
| function readState(): Record<string, string> { | ||
| try { | ||
| return JSON.parse(readFileSync(ENVIRONMENTS_PATH, "utf8")); | ||
| } catch { | ||
| return {}; | ||
| } | ||
| } | ||
|
|
||
| function writeState(state: Record<string, string>): void { | ||
| mkdirSync(new URL(".", ENVIRONMENTS_PATH), { recursive: true }); | ||
| writeFileSync(ENVIRONMENTS_PATH, stringify(state)); | ||
| } | ||
|
|
||
| function findProjectRoot(): string | undefined { | ||
| const configPath = findUpwardSync(CONFIG_FILENAME); | ||
| if (!configPath) return undefined; | ||
| return realpathSync(fileURLToPath(new URL(".", configPath))); | ||
| } | ||
|
|
||
| type PackageJson = { | ||
| dependencies?: Record<string, string>; | ||
| devDependencies?: Record<string, string>; | ||
| peerDependencies?: Record<string, string>; | ||
| }; | ||
|
|
||
| function readPackageJson(): PackageJson | undefined { | ||
| const packageJsonPath = findUpwardSync("package.json"); | ||
| if (!packageJsonPath) return undefined; | ||
| try { | ||
| return JSON.parse(readFileSync(packageJsonPath, "utf8")); | ||
| } catch { | ||
| return undefined; | ||
| } | ||
| } | ||
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
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| import { createCommand, type CommandConfig } from "../lib/command"; | ||
| import { getRepositoryName } from "../project"; | ||
|
|
||
| const config = { | ||
| name: "prismic env active", | ||
| description: ` | ||
| Print the active environment. | ||
|
|
||
| Prints the production environment when no environment is active. | ||
| `, | ||
| } satisfies CommandConfig; | ||
|
|
||
| export default createCommand(config, async () => { | ||
| console.info(await getRepositoryName()); | ||
| }); |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| import { getActiveEnvironment } from "../active-environment"; | ||
| import { getHost, getToken } from "../auth"; | ||
| import { getUserEnvironments } from "../environments"; | ||
| import { createCommand, type CommandConfig } from "../lib/command"; | ||
| import { stringify } from "../lib/json"; | ||
| import { formatTable } from "../lib/string"; | ||
| import { readConfig } from "../project"; | ||
|
|
||
| const config = { | ||
| name: "prismic env list", | ||
| description: ` | ||
| List the environments available for the project, marking the active one. | ||
| `, | ||
| options: { | ||
| json: { type: "boolean", description: "Output as JSON" }, | ||
| }, | ||
| } satisfies CommandConfig; | ||
|
|
||
| export default createCommand(config, async ({ values }) => { | ||
| const { json } = values; | ||
|
|
||
| const { repositoryName } = await readConfig(); | ||
| const token = await getToken(); | ||
| const host = await getHost(); | ||
|
|
||
| const environments = await getUserEnvironments({ repo: repositoryName, token, host }); | ||
| const activeEnvironment = getActiveEnvironment() ?? repositoryName; | ||
|
|
||
| if (json) { | ||
| const results = environments.map((environment) => ({ | ||
| kind: environment.kind, | ||
| name: environment.name, | ||
| domain: environment.domain, | ||
| active: environment.domain === activeEnvironment, | ||
| })); | ||
| console.info(stringify(results)); | ||
| return; | ||
| } | ||
|
|
||
| if (environments.length === 0) { | ||
| console.info("No environments found."); | ||
| return; | ||
| } | ||
|
|
||
| const rows = environments.map((environment) => { | ||
| const activeLabel = environment.domain === activeEnvironment ? " (active)" : ""; | ||
| return [environment.domain, `${environment.name}${activeLabel}`]; | ||
| }); | ||
| console.info(formatTable(rows, { headers: ["DOMAIN", "NAME"] })); | ||
| }); |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| import { setActiveEnvironment, unsetActiveEnvironment } from "../active-environment"; | ||
| import { getHost, getToken } from "../auth"; | ||
| import { resolveEnvironment } from "../environments"; | ||
| import { createCommand, type CommandConfig } from "../lib/command"; | ||
| import { readConfig } from "../project"; | ||
|
|
||
| const config = { | ||
| name: "prismic env set", | ||
| description: ` | ||
| Set the active environment for the project. | ||
|
|
||
| The active environment is stored by the CLI and used by every command and by | ||
| the project at build time. Setting the production environment is the same as | ||
| \`prismic env unset\`. | ||
| `, | ||
| positionals: { | ||
| name: { description: "Environment domain", required: true }, | ||
| }, | ||
| } satisfies CommandConfig; | ||
|
|
||
| export default createCommand(config, async ({ positionals }) => { | ||
| const [name] = positionals; | ||
|
|
||
| const { repositoryName } = await readConfig(); | ||
| const token = await getToken(); | ||
| const host = await getHost(); | ||
|
|
||
| const domain = await resolveEnvironment(name, { repo: repositoryName, token, host }); | ||
|
angeloashmore marked this conversation as resolved.
|
||
|
|
||
| if (domain === repositoryName) { | ||
| unsetActiveEnvironment(); | ||
| console.info(`Reset to the production environment "${repositoryName}".`); | ||
| return; | ||
| } | ||
|
|
||
| setActiveEnvironment(domain); | ||
| console.info(`Set the active environment to "${domain}".`); | ||
| }); | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| import { unsetActiveEnvironment } from "../active-environment"; | ||
| import { createCommand, type CommandConfig } from "../lib/command"; | ||
| import { readConfig } from "../project"; | ||
|
|
||
| const config = { | ||
| name: "prismic env unset", | ||
| description: ` | ||
| Reset the active environment to the production environment. | ||
| `, | ||
| } satisfies CommandConfig; | ||
|
|
||
| export default createCommand(config, async () => { | ||
| const { repositoryName } = await readConfig(); | ||
|
|
||
| unsetActiveEnvironment(); | ||
|
|
||
| console.info(`Reset to the production environment "${repositoryName}".`); | ||
| }); |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| import { createCommandRouter } from "../lib/command"; | ||
| import envActive from "./env-active"; | ||
| import envList from "./env-list"; | ||
| import envSet from "./env-set"; | ||
| import envUnset from "./env-unset"; | ||
|
|
||
| export default createCommandRouter({ | ||
| name: "prismic env", | ||
| description: "Manage the active environment for a Prismic project.", | ||
| commands: { | ||
| set: { | ||
| handler: envSet, | ||
| description: "Set the active environment", | ||
| }, | ||
| unset: { | ||
| handler: envUnset, | ||
| description: "Reset to the production environment", | ||
| }, | ||
| active: { | ||
| handler: envActive, | ||
| description: "Print the active environment", | ||
| }, | ||
| list: { | ||
| handler: envList, | ||
| description: "List environments", | ||
| }, | ||
| }, | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.