-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add env commands
#227
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
c9d5748
feat: add prismic env commands backed by .env.local
angeloashmore a222150
feat: wire env command and read active environment in clients
angeloashmore c74abb3
fix: address env review feedback
angeloashmore 2e3217e
feat: hide env commands from help and mark them experimental
angeloashmore 81875ef
refactor: remove env file format checks that can never throw
angeloashmore 8583370
Merge remote-tracking branch 'origin/main' into aa/env-dot-env
angeloashmore ea91d5b
feat: use active environment across commands (#228)
angeloashmore fdb2078
fix: fall back to config repository name when environment variable is…
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
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,19 @@ | ||
| import { getAdapter } from "../adapters"; | ||
| import { createCommand, type CommandConfig } from "../lib/command"; | ||
| import { getRepositoryName } from "../project"; | ||
|
|
||
| const config = { | ||
| name: "prismic env active", | ||
| description: ` | ||
| Print the active environment. | ||
|
|
||
| @experimental - This command may change or be removed in any release and does not follow semver. | ||
| `, | ||
| } satisfies CommandConfig; | ||
|
|
||
| export default createCommand(config, async () => { | ||
| const repo = await getRepositoryName(); | ||
| const adapter = await getAdapter(); | ||
| const environment = await adapter.getEnvironment(); | ||
| console.info(environment ?? repo); | ||
| }); |
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,53 @@ | ||
| import { getAdapter } from "../adapters"; | ||
| import { getCredentials } from "../auth"; | ||
| import { createCommand, type CommandConfig } from "../lib/command"; | ||
| import { stringify } from "../lib/json"; | ||
| import { getUserEnvironments } from "../lib/prismic/environments"; | ||
| import { formatTable } from "../lib/string"; | ||
| import { getRepositoryName } from "../project"; | ||
|
|
||
| const config = { | ||
| name: "prismic env list", | ||
| description: ` | ||
| List the environments available for the project. | ||
|
|
||
| @experimental - This command may change or be removed in any release and does not follow semver. | ||
| `, | ||
| options: { | ||
| json: { type: "boolean", description: "Output as JSON" }, | ||
| repo: { type: "string", short: "r", description: "Repository domain" }, | ||
| }, | ||
| } satisfies CommandConfig; | ||
|
|
||
| export default createCommand(config, async ({ values }) => { | ||
| const projectRepo = await getRepositoryName(); | ||
| const { json, repo = projectRepo } = values; | ||
|
|
||
| const { token, host } = await getCredentials(); | ||
| const environments = await getUserEnvironments({ repo, token, host }); | ||
|
|
||
| let activeEnvironment: string | undefined; | ||
| if (repo === projectRepo) { | ||
| const adapter = await getAdapter(); | ||
| activeEnvironment = (await adapter.getEnvironment()) ?? projectRepo; | ||
| } | ||
|
|
||
| if (json) { | ||
| const results = environments.map((environment) => ({ | ||
| domain: environment.domain, | ||
| kind: environment.kind, | ||
| active: environment.domain === activeEnvironment, | ||
| })); | ||
| console.info(stringify(results)); | ||
| return; | ||
| } | ||
|
|
||
| const rows = environments.map((environment) => { | ||
| return [ | ||
| environment.domain, | ||
| environment.kind, | ||
| environment.domain === activeEnvironment ? "*" : "", | ||
| ]; | ||
| }); | ||
| console.info(formatTable(rows, { headers: ["DOMAIN", "KIND", "ACTIVE"] })); | ||
| }); |
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,39 @@ | ||
| import { getAdapter } from "../adapters"; | ||
| import { getCredentials } from "../auth"; | ||
| import { createCommand, type CommandConfig } from "../lib/command"; | ||
| import { getUserEnvironments, InvalidEnvironmentError } from "../lib/prismic/environments"; | ||
| import { getRepositoryName } from "../project"; | ||
|
|
||
| const config = { | ||
| name: "prismic env set", | ||
| description: ` | ||
| Set the active environment. | ||
|
|
||
| @experimental - This command may change or be removed in any release and does not follow semver. | ||
| `, | ||
| positionals: { | ||
| environment: { description: "Environment domain", required: true }, | ||
| }, | ||
| } satisfies CommandConfig; | ||
|
|
||
| export default createCommand(config, async ({ positionals }) => { | ||
| const [environment] = positionals; | ||
|
|
||
| const repo = await getRepositoryName(); | ||
| const { token, host } = await getCredentials(); | ||
| const environments = await getUserEnvironments({ repo, token, host }); | ||
|
|
||
| const validEnvironment = environments.some((env) => env.domain === environment); | ||
| if (!validEnvironment) throw new InvalidEnvironmentError(environment, environments, repo); | ||
|
|
||
| const adapter = await getAdapter(); | ||
|
|
||
| if (environment === repo) { | ||
| await adapter.unsetEnvironment(); | ||
| console.info(`Reset to the production environment "${repo}".`); | ||
| return; | ||
| } | ||
|
|
||
| await adapter.setEnvironment(environment); | ||
| console.info(`Set the active environment to "${environment}".`); | ||
| }); |
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,19 @@ | ||
| import { getAdapter } from "../adapters"; | ||
| import { createCommand, type CommandConfig } from "../lib/command"; | ||
| import { getRepositoryName } from "../project"; | ||
|
|
||
| const config = { | ||
| name: "prismic env unset", | ||
| description: ` | ||
| Reset the active environment to the production environment. | ||
|
|
||
| @experimental - This command may change or be removed in any release and does not follow semver. | ||
| `, | ||
| } satisfies CommandConfig; | ||
|
|
||
| export default createCommand(config, async () => { | ||
| const repo = await getRepositoryName(); | ||
| const adapter = await getAdapter(); | ||
| await adapter.unsetEnvironment(); | ||
| console.info(`Reset to the production environment "${repo}".`); | ||
| }); |
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,32 @@ | ||
| 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. | ||
|
|
||
| @experimental - This command may change or be removed in any release and does not follow semver. | ||
| `, | ||
| 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", | ||
| }, | ||
| }, | ||
| }); |
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
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.