From 47b08240ad9acc95fd9a21cff1e655fb3655afff Mon Sep 17 00:00:00 2001 From: Angelo Ashmore Date: Wed, 8 Jul 2026 23:46:30 +0000 Subject: [PATCH] refactor: resolve commands against the active environment Migrate pull, push, status, sync, and the locale/preview/token/webhook commands to read the active environment from CLI state, falling back to the repository name. `--env` becomes a deprecated alias for `--repo`, and `resolveEnvironment` is removed. Co-Authored-By: Claude Opus 4.8 --- src/commands/locale-add.ts | 14 ++++++++---- src/commands/locale-list.ts | 9 ++++---- src/commands/locale-remove.ts | 9 ++++---- src/commands/locale-set-master.ts | 9 ++++---- src/commands/preview-add.ts | 15 ++++++------ src/commands/preview-list.ts | 9 ++++---- src/commands/preview-remove.ts | 9 ++++---- src/commands/preview-set-simulator.ts | 9 ++++---- src/commands/pull.ts | 14 +++++------- src/commands/push.ts | 33 ++++++++++++--------------- src/commands/repo-set-api-access.ts | 5 ++-- src/commands/status.ts | 22 +++++++++--------- src/commands/sync.ts | 16 +++++-------- src/commands/token-create.ts | 9 ++++---- src/commands/token-delete.ts | 9 ++++---- src/commands/token-list.ts | 9 ++++---- src/commands/webhook-create.ts | 15 ++++++++---- src/commands/webhook-disable.ts | 9 ++++---- src/commands/webhook-enable.ts | 9 ++++---- src/commands/webhook-list.ts | 9 ++++---- src/commands/webhook-remove.ts | 9 ++++---- src/commands/webhook-set-triggers.ts | 9 ++++---- src/commands/webhook-view.ts | 9 ++++---- src/environments.ts | 11 --------- test/pull.test.ts | 6 ----- test/push.serial.test.ts | 6 ----- test/status.serial.test.ts | 14 ------------ 27 files changed, 126 insertions(+), 180 deletions(-) diff --git a/src/commands/locale-add.ts b/src/commands/locale-add.ts index 3ded8b82..fb18e667 100644 --- a/src/commands/locale-add.ts +++ b/src/commands/locale-add.ts @@ -1,6 +1,6 @@ import { getHost, getToken } from "../auth"; import { upsertLocale } from "../clients/locale"; -import { resolveEnvironment } from "../environments"; +import { getEnvironment } from "../environments"; import { CommandError, createCommand, type CommandConfig } from "../lib/command"; import { UnknownRequestError } from "../lib/request"; import { getRepositoryName } from "../project"; @@ -19,18 +19,22 @@ const config = { options: { master: { type: "boolean", description: "Set as the master locale" }, name: { type: "string", short: "n", description: "Custom display name (for custom locales)" }, - repo: { type: "string", short: "r", description: "Repository domain" }, - env: { type: "string", short: "e", description: "Environment domain" }, + repo: { type: "string", short: "r", description: "Repository or environment domain" }, + env: { type: "string", short: "e", description: "(deprecated) Alias for --repo" }, }, } satisfies CommandConfig; export default createCommand(config, async ({ positionals, values }) => { const [code] = positionals; - const { repo: parentRepo = await getRepositoryName(), env, master = false, name } = values; + const { + env, + repo = env ?? (await getEnvironment()) ?? (await getRepositoryName()), + master = false, + name, + } = values; const token = await getToken(); const host = await getHost(); - const repo = env ? await resolveEnvironment(env, { repo: parentRepo, token, host }) : parentRepo; try { await upsertLocale({ id: code, isMaster: master, customName: name }, { repo, token, host }); diff --git a/src/commands/locale-list.ts b/src/commands/locale-list.ts index 2e704801..ca485c75 100644 --- a/src/commands/locale-list.ts +++ b/src/commands/locale-list.ts @@ -1,6 +1,6 @@ import { getHost, getToken } from "../auth"; import { getLocales } from "../clients/locale"; -import { resolveEnvironment } from "../environments"; +import { getEnvironment } from "../environments"; import { CommandError, createCommand, type CommandConfig } from "../lib/command"; import { stringify } from "../lib/json"; import { UnknownRequestError } from "../lib/request"; @@ -17,17 +17,16 @@ const config = { `, options: { json: { type: "boolean", description: "Output as JSON" }, - repo: { type: "string", short: "r", description: "Repository domain" }, - env: { type: "string", short: "e", description: "Environment domain" }, + repo: { type: "string", short: "r", description: "Repository or environment domain" }, + env: { type: "string", short: "e", description: "(deprecated) Alias for --repo" }, }, } satisfies CommandConfig; export default createCommand(config, async ({ values }) => { - const { repo: parentRepo = await getRepositoryName(), env, json } = values; + const { env, repo = env ?? (await getEnvironment()) ?? (await getRepositoryName()), json } = values; const token = await getToken(); const host = await getHost(); - const repo = env ? await resolveEnvironment(env, { repo: parentRepo, token, host }) : parentRepo; let locales; try { diff --git a/src/commands/locale-remove.ts b/src/commands/locale-remove.ts index 5522aa3d..d909cc03 100644 --- a/src/commands/locale-remove.ts +++ b/src/commands/locale-remove.ts @@ -1,6 +1,6 @@ import { getHost, getToken } from "../auth"; import { removeLocale } from "../clients/locale"; -import { resolveEnvironment } from "../environments"; +import { getEnvironment } from "../environments"; import { CommandError, createCommand, type CommandConfig } from "../lib/command"; import { UnknownRequestError } from "../lib/request"; import { getRepositoryName } from "../project"; @@ -17,18 +17,17 @@ const config = { code: { description: "Locale code (e.g. en-us, fr-fr)", required: true }, }, options: { - repo: { type: "string", short: "r", description: "Repository domain" }, - env: { type: "string", short: "e", description: "Environment domain" }, + repo: { type: "string", short: "r", description: "Repository or environment domain" }, + env: { type: "string", short: "e", description: "(deprecated) Alias for --repo" }, }, } satisfies CommandConfig; export default createCommand(config, async ({ positionals, values }) => { const [code] = positionals; - const { repo: parentRepo = await getRepositoryName(), env } = values; + const { env, repo = env ?? (await getEnvironment()) ?? (await getRepositoryName()) } = values; const token = await getToken(); const host = await getHost(); - const repo = env ? await resolveEnvironment(env, { repo: parentRepo, token, host }) : parentRepo; try { await removeLocale(code, { repo, token, host }); diff --git a/src/commands/locale-set-master.ts b/src/commands/locale-set-master.ts index 01eb422e..eb708432 100644 --- a/src/commands/locale-set-master.ts +++ b/src/commands/locale-set-master.ts @@ -1,6 +1,6 @@ import { getHost, getToken } from "../auth"; import { getLocales, upsertLocale } from "../clients/locale"; -import { resolveEnvironment } from "../environments"; +import { getEnvironment } from "../environments"; import { CommandError, createCommand, type CommandConfig } from "../lib/command"; import { UnknownRequestError } from "../lib/request"; import { getRepositoryName } from "../project"; @@ -17,18 +17,17 @@ const config = { code: { description: "Locale code (e.g. en-us, fr-fr)", required: true }, }, options: { - repo: { type: "string", short: "r", description: "Repository domain" }, - env: { type: "string", short: "e", description: "Environment domain" }, + repo: { type: "string", short: "r", description: "Repository or environment domain" }, + env: { type: "string", short: "e", description: "(deprecated) Alias for --repo" }, }, } satisfies CommandConfig; export default createCommand(config, async ({ positionals, values }) => { const [code] = positionals; - const { repo: parentRepo = await getRepositoryName(), env } = values; + const { env, repo = env ?? (await getEnvironment()) ?? (await getRepositoryName()) } = values; const token = await getToken(); const host = await getHost(); - const repo = env ? await resolveEnvironment(env, { repo: parentRepo, token, host }) : parentRepo; let locales; try { diff --git a/src/commands/preview-add.ts b/src/commands/preview-add.ts index cff60ec7..9fe95824 100644 --- a/src/commands/preview-add.ts +++ b/src/commands/preview-add.ts @@ -1,6 +1,6 @@ import { getHost, getToken } from "../auth"; import { addPreview } from "../clients/core"; -import { resolveEnvironment } from "../environments"; +import { getEnvironment } from "../environments"; import { CommandError, createCommand, type CommandConfig } from "../lib/command"; import { UnknownRequestError } from "../lib/request"; import { getRepositoryName } from "../project"; @@ -18,14 +18,17 @@ const config = { }, options: { name: { type: "string", short: "n", description: "Display name (defaults to hostname)" }, - repo: { type: "string", short: "r", description: "Repository domain" }, - env: { type: "string", short: "e", description: "Environment domain" }, + repo: { type: "string", short: "r", description: "Repository or environment domain" }, + env: { type: "string", short: "e", description: "(deprecated) Alias for --repo" }, }, } satisfies CommandConfig; export default createCommand(config, async ({ positionals, values }) => { const [previewUrl] = positionals; - const { repo: parentRepo = await getRepositoryName(), env, name } = values; + const { env, name, repo = env ?? (await getEnvironment()) ?? (await getRepositoryName()) } = values; + + const token = await getToken(); + const host = await getHost(); let parsed: URL; try { @@ -38,10 +41,6 @@ export default createCommand(config, async ({ positionals, values }) => { const websiteURL = `${parsed.protocol}//${parsed.host}`; const resolverPath = parsed.pathname === "/" ? undefined : parsed.pathname; - const token = await getToken(); - const host = await getHost(); - const repo = env ? await resolveEnvironment(env, { repo: parentRepo, token, host }) : parentRepo; - try { await addPreview({ name: displayName, websiteURL, resolverPath }, { repo, token, host }); } catch (error) { diff --git a/src/commands/preview-list.ts b/src/commands/preview-list.ts index b6476154..0774802f 100644 --- a/src/commands/preview-list.ts +++ b/src/commands/preview-list.ts @@ -1,6 +1,6 @@ import { getHost, getToken } from "../auth"; import { getPreviews, getSimulatorUrl } from "../clients/core"; -import { resolveEnvironment } from "../environments"; +import { getEnvironment } from "../environments"; import { CommandError, createCommand, type CommandConfig } from "../lib/command"; import { stringify } from "../lib/json"; import { UnknownRequestError } from "../lib/request"; @@ -17,17 +17,16 @@ const config = { `, options: { json: { type: "boolean", description: "Output as JSON" }, - repo: { type: "string", short: "r", description: "Repository domain" }, - env: { type: "string", short: "e", description: "Environment domain" }, + repo: { type: "string", short: "r", description: "Repository or environment domain" }, + env: { type: "string", short: "e", description: "(deprecated) Alias for --repo" }, }, } satisfies CommandConfig; export default createCommand(config, async ({ values }) => { - const { repo: parentRepo = await getRepositoryName(), env, json } = values; + const { env, repo = env ?? (await getEnvironment()) ?? (await getRepositoryName()), json } = values; const token = await getToken(); const host = await getHost(); - const repo = env ? await resolveEnvironment(env, { repo: parentRepo, token, host }) : parentRepo; let previews; let simulatorUrl; diff --git a/src/commands/preview-remove.ts b/src/commands/preview-remove.ts index fa292ed7..c9ca8452 100644 --- a/src/commands/preview-remove.ts +++ b/src/commands/preview-remove.ts @@ -1,6 +1,6 @@ import { getHost, getToken } from "../auth"; import { getPreviews, removePreview } from "../clients/core"; -import { resolveEnvironment } from "../environments"; +import { getEnvironment } from "../environments"; import { CommandError, createCommand, type CommandConfig } from "../lib/command"; import { UnknownRequestError } from "../lib/request"; import { getRepositoryName } from "../project"; @@ -17,18 +17,17 @@ const config = { url: { description: "Preview URL to remove", required: true }, }, options: { - repo: { type: "string", short: "r", description: "Repository domain" }, - env: { type: "string", short: "e", description: "Environment domain" }, + repo: { type: "string", short: "r", description: "Repository or environment domain" }, + env: { type: "string", short: "e", description: "(deprecated) Alias for --repo" }, }, } satisfies CommandConfig; export default createCommand(config, async ({ positionals, values }) => { const [previewUrl] = positionals; - const { repo: parentRepo = await getRepositoryName(), env } = values; + const { env, repo = env ?? (await getEnvironment()) ?? (await getRepositoryName()) } = values; const token = await getToken(); const host = await getHost(); - const repo = env ? await resolveEnvironment(env, { repo: parentRepo, token, host }) : parentRepo; let previews; try { diff --git a/src/commands/preview-set-simulator.ts b/src/commands/preview-set-simulator.ts index 1332e520..d25414bb 100644 --- a/src/commands/preview-set-simulator.ts +++ b/src/commands/preview-set-simulator.ts @@ -1,6 +1,6 @@ import { getHost, getToken } from "../auth"; import { setSimulatorUrl } from "../clients/core"; -import { resolveEnvironment } from "../environments"; +import { getEnvironment } from "../environments"; import { CommandError, createCommand, type CommandConfig } from "../lib/command"; import { UnknownRequestError } from "../lib/request"; import { getRepositoryName } from "../project"; @@ -23,14 +23,14 @@ const config = { }, }, options: { - repo: { type: "string", short: "r", description: "Repository domain" }, - env: { type: "string", short: "e", description: "Environment domain" }, + repo: { type: "string", short: "r", description: "Repository or environment domain" }, + env: { type: "string", short: "e", description: "(deprecated) Alias for --repo" }, }, } satisfies CommandConfig; export default createCommand(config, async ({ positionals, values }) => { const [urlArg] = positionals; - const { repo: parentRepo = await getRepositoryName(), env } = values; + const { env, repo = env ?? (await getEnvironment()) ?? (await getRepositoryName()) } = values; let parsed: URL; try { @@ -46,7 +46,6 @@ export default createCommand(config, async ({ positionals, values }) => { const token = await getToken(); const host = await getHost(); - const repo = env ? await resolveEnvironment(env, { repo: parentRepo, token, host }) : parentRepo; try { await setSimulatorUrl(simulatorUrl, { repo, token, host }); diff --git a/src/commands/pull.ts b/src/commands/pull.ts index 866609b1..9969a053 100644 --- a/src/commands/pull.ts +++ b/src/commands/pull.ts @@ -2,7 +2,7 @@ import { getAdapter } from "../adapters"; import { getHost, getToken } from "../auth"; import { getCustomTypes, getSlices } from "../clients/custom-types"; import { completeOnboardingStepsSilently } from "../clients/repository"; -import { resolveEnvironment } from "../environments"; +import { getEnvironment } from "../environments"; import { CommandError, createCommand, type CommandConfig } from "../lib/command"; import { diffArrays } from "../lib/diff"; import { getDirtyPaths, getGitRoot } from "../lib/git"; @@ -20,22 +20,20 @@ const config = { `, options: { force: { type: "boolean", short: "f", description: "Overwrite local changes" }, - repo: { type: "string", short: "r", description: "Repository domain" }, - env: { type: "string", short: "e", description: "Environment domain" }, + repo: { type: "string", short: "r", description: "Repository or environment domain" }, + env: { type: "string", short: "e", description: "(deprecated) Alias for --repo" }, }, } satisfies CommandConfig; export default createCommand(config, async ({ values }) => { - const { force = false, repo: parentRepo = await getRepositoryName(), env } = values; + const { env, force = false, repo = env ?? (await getEnvironment()) ?? (await getRepositoryName()) } = values; const token = await getToken(); const host = await getHost(); const adapter = await getAdapter(); const projectRoot = await findProjectRoot(); - const repo = env ? await resolveEnvironment(env, { repo: parentRepo, token, host }) : parentRepo; - - console.info(`Pulling from repository: ${parentRepo}${env ? ` (env: ${env})` : ""}`); + console.info(`Pulling from repository: ${repo}`); const [gitRoot, customTypeLibraries, sliceLibraries] = await Promise.all([ getGitRoot(projectRoot), @@ -140,7 +138,7 @@ export default createCommand(config, async ({ values }) => { await adapter.generateTypes(); await completeOnboardingStepsSilently({ - repo: parentRepo, + repo, token, host, stepIds: ["connectPrismic"], diff --git a/src/commands/push.ts b/src/commands/push.ts index 1d204f50..6aa45ec7 100644 --- a/src/commands/push.ts +++ b/src/commands/push.ts @@ -1,7 +1,7 @@ -import { pascalCase } from "change-case"; - import type { CustomType } from "@prismicio/types-internal/lib/customtypes"; +import { pascalCase } from "change-case"; + import { getAdapter } from "../adapters"; import { getHost, getToken } from "../auth"; import { getDocumentTotalByCustomTypes } from "../clients/core"; @@ -16,12 +16,9 @@ import { updateCustomType, updateSlice, } from "../clients/custom-types"; -import { - completeOnboardingStepsSilently, - type OnboardingStep, -} from "../clients/repository"; +import { completeOnboardingStepsSilently, type OnboardingStep } from "../clients/repository"; import { getWorkingDocumentsUrlForCustomType, getCustomTypeListUrl } from "../clients/wroom"; -import { resolveEnvironment } from "../environments"; +import { getEnvironment } from "../environments"; import { CommandError, createCommand, type CommandConfig } from "../lib/command"; import { diffArrays } from "../lib/diff"; import { getDirtyPaths, getGitRoot } from "../lib/git"; @@ -40,22 +37,20 @@ const config = { `, options: { force: { type: "boolean", short: "f", description: "Skip safety checks" }, - repo: { type: "string", short: "r", description: "Repository domain" }, - env: { type: "string", short: "e", description: "Environment domain" }, + repo: { type: "string", short: "r", description: "Repository or environment domain" }, + env: { type: "string", short: "e", description: "(deprecated) Alias for --repo" }, }, } satisfies CommandConfig; export default createCommand(config, async ({ values }) => { - const { force = false, repo: parentRepo = await getRepositoryName(), env } = values; + const { env, force = false, repo = env ?? (await getEnvironment()) ?? (await getRepositoryName()) } = values; const token = await getToken(); const host = await getHost(); const adapter = await getAdapter(); const projectRoot = await findProjectRoot(); - const repo = env ? await resolveEnvironment(env, { repo: parentRepo, token, host }) : parentRepo; - - console.info(`Pushing to repository: ${parentRepo}${env ? ` (env: ${env})` : ""}`); + console.info(`Pushing to repository: ${repo}`); const [gitRoot, customTypeLibraries, sliceLibraries] = await Promise.all([ getGitRoot(projectRoot), @@ -166,7 +161,7 @@ export default createCommand(config, async ({ values }) => { } if (onboardingSteps.length > 0) { await completeOnboardingStepsSilently({ - repo: parentRepo, + repo, token, host, stepIds: onboardingSteps, @@ -201,9 +196,9 @@ async function removeCustomTypeWithDocumentHandling( if (!(await isDocumentsInUseError(error))) { const errorMessage = error instanceof Error ? error.message : String(error); throw new CommandError( - `Could not delete type "${id}": ${errorMessage}"` + - "\nPlease try again, or manually deleting the type at: " + - getCustomTypeListUrl({ repo, host, format: format ?? "custom" }) + `Could not delete type "${id}": ${errorMessage}"` + + "\nPlease try again, or manually deleting the type at: " + + getCustomTypeListUrl({ repo, host, format: format ?? "custom" }), ); } @@ -213,7 +208,7 @@ async function removeCustomTypeWithDocumentHandling( } catch { throw new CommandError( `Could not check whether type "${id}" has associated pages. ` + - "\nPlease try again, or manually delete any associated pages at: " + + "\nPlease try again, or manually delete any associated pages at: " + getWorkingDocumentsUrlForCustomType({ repo, host, customTypeId: id }), ); } @@ -222,7 +217,7 @@ async function removeCustomTypeWithDocumentHandling( const pluralPages = documentCount === 1 ? "page" : "pages"; throw new CommandError( `Could not delete type "${id}" because it has${countLabel} associated ${pluralPages}. ` + - `\nDelete any associated pages manually before pushing at: ` + + `\nDelete any associated pages manually before pushing at: ` + getWorkingDocumentsUrlForCustomType({ repo, host, customTypeId: id }), ); } diff --git a/src/commands/repo-set-api-access.ts b/src/commands/repo-set-api-access.ts index afe81037..1bd79855 100644 --- a/src/commands/repo-set-api-access.ts +++ b/src/commands/repo-set-api-access.ts @@ -1,5 +1,6 @@ import { getHost, getToken } from "../auth"; import { type RepositoryAccessLevel, setRepositoryAccess } from "../clients/wroom"; +import { getEnvironment } from "../environments"; import { CommandError, createCommand, type CommandConfig } from "../lib/command"; import { UnknownRequestError } from "../lib/request"; import { getRepositoryName } from "../project"; @@ -18,13 +19,13 @@ const config = { level: { description: `Access level (${VALID_LEVELS.join(", ")})`, required: true }, }, options: { - repo: { type: "string", short: "r", description: "Repository domain" }, + repo: { type: "string", short: "r", description: "Repository or environment domain" }, }, } satisfies CommandConfig; export default createCommand(config, async ({ positionals, values }) => { const [level] = positionals; - const { repo = await getRepositoryName() } = values; + const { repo = (await getEnvironment()) ?? (await getRepositoryName()) } = values; if (!VALID_LEVELS.includes(level as RepositoryAccessLevel)) { throw new CommandError( diff --git a/src/commands/status.ts b/src/commands/status.ts index f148559f..4e6953a3 100644 --- a/src/commands/status.ts +++ b/src/commands/status.ts @@ -4,7 +4,7 @@ import { getAdapter } from "../adapters"; import { getHost, getToken } from "../auth"; import { getCustomTypes, getSlices } from "../clients/custom-types"; import { getProfile } from "../clients/user"; -import { resolveEnvironment } from "../environments"; +import { getEnvironment } from "../environments"; import { createCommand, type CommandConfig } from "../lib/command"; import { diffArrays, type ArrayDiff } from "../lib/diff"; import { getDirtyPaths, getGitRoot } from "../lib/git"; @@ -22,13 +22,17 @@ const config = { model files with uncommitted git changes that would block pull and push. `, options: { - repo: { type: "string", short: "r", description: "Repository domain" }, - env: { type: "string", short: "e", description: "Environment domain" }, + repo: { type: "string", short: "r", description: "Repository or environment domain" }, + env: { type: "string", short: "e", description: "(deprecated) Alias for --repo" }, }, } satisfies CommandConfig; export default createCommand(config, async ({ values }) => { - const { repo: parentRepo = await getRepositoryName(), env } = values; + const { env, repo: repoArg } = values; + const repoFlag = repoArg ?? env; + const environment = repoFlag ? undefined : await getEnvironment(); + const baseRepo = repoFlag ?? (await getRepositoryName()); + const repo = environment ?? baseRepo; const token = await getToken(); const host = await getHost(); @@ -44,14 +48,10 @@ export default createCommand(config, async ({ values }) => { adapter.getSlices(), ]); - let repo = parentRepo; let userEmail: string | undefined; let customTypeOps: ArrayDiff | undefined; let sliceOps: ArrayDiff | undefined; if (token) { - if (env) { - repo = await resolveEnvironment(env, { repo: parentRepo, token, host }); - } const [profile, remoteCustomTypes, remoteSlices] = await Promise.all([ getProfile({ token, host }), getCustomTypes({ repo, token, host }), @@ -92,9 +92,9 @@ export default createCommand(config, async ({ values }) => { .map((path) => relativePathname(projectRoot, path)); } - console.info(`Repository: ${parentRepo}`); - if (env) { - console.info(`Environment: ${env}`); + console.info(`Repository: ${baseRepo}`); + if (environment) { + console.info(`Environment: ${environment}`); } if (userEmail) { console.info(`Authenticated as: ${userEmail}`); diff --git a/src/commands/sync.ts b/src/commands/sync.ts index 18492690..f123b499 100644 --- a/src/commands/sync.ts +++ b/src/commands/sync.ts @@ -6,7 +6,7 @@ import { getHost, getToken } from "../auth"; import { getCustomTypes, getSlices } from "../clients/custom-types"; import { completeOnboardingStepsSilently } from "../clients/repository"; import { env } from "../env"; -import { resolveEnvironment } from "../environments"; +import { getEnvironment } from "../environments"; import { createCommand, type CommandConfig } from "../lib/command"; import { diffArrays } from "../lib/diff"; import { getRepositoryName } from "../project"; @@ -29,22 +29,18 @@ const config = { description: "Watch for changes and sync continuously", required: true, }, - repo: { type: "string", short: "r", description: "Repository domain" }, - env: { type: "string", short: "e", description: "Environment domain" }, + repo: { type: "string", short: "r", description: "Repository or environment domain" }, + env: { type: "string", short: "e", description: "(deprecated) Alias for --repo" }, }, } satisfies CommandConfig; export default createCommand(config, async ({ values }) => { - const { repo: parentRepo = await getRepositoryName(), env: envFlag } = values; + const { env, repo = env ?? (await getEnvironment()) ?? (await getRepositoryName()) } = values; const token = await getToken(); const host = await getHost(); const adapter = await getAdapter(); - const repo = envFlag - ? await resolveEnvironment(envFlag, { repo: parentRepo, token, host }) - : parentRepo; - trackCommandStart("sync", { watch: true }); process.on("SIGINT", () => { console.info("\nWatch stopped. Goodbye!"); @@ -53,7 +49,7 @@ export default createCommand(config, async ({ values }) => { }); console.info( - `Watching repository: ${parentRepo}${envFlag ? ` (env: ${envFlag})` : ""} (polling every ${POLL_INTERVAL_MS / 1000}s, Ctrl+C to stop)`, + `Watching repository: ${repo} (polling every ${POLL_INTERVAL_MS / 1000}s, Ctrl+C to stop)`, ); let lastHash = ""; @@ -118,7 +114,7 @@ export default createCommand(config, async ({ values }) => { if (isInitial) { await completeOnboardingStepsSilently({ - repo: parentRepo, + repo, token, host, stepIds: ["connectPrismic"], diff --git a/src/commands/token-create.ts b/src/commands/token-create.ts index 50d9dc7f..608787f7 100644 --- a/src/commands/token-create.ts +++ b/src/commands/token-create.ts @@ -5,7 +5,7 @@ import { createWriteToken, getOAuthApps, } from "../clients/wroom"; -import { resolveEnvironment } from "../environments"; +import { getEnvironment } from "../environments"; import { CommandError, createCommand, type CommandConfig } from "../lib/command"; import { stringify } from "../lib/json"; import { UnknownRequestError } from "../lib/request"; @@ -33,15 +33,15 @@ const config = { description: `Name to identify the token (default: "${CLI_APP_NAME}")`, }, json: { type: "boolean", description: "Output as JSON" }, - repo: { type: "string", short: "r", description: "Repository domain" }, - env: { type: "string", short: "e", description: "Environment domain" }, + repo: { type: "string", short: "r", description: "Repository or environment domain" }, + env: { type: "string", short: "e", description: "(deprecated) Alias for --repo" }, }, } satisfies CommandConfig; export default createCommand(config, async ({ values }) => { const { - repo: parentRepo = await getRepositoryName(), env, + repo = env ?? (await getEnvironment()) ?? (await getRepositoryName()), write, "allow-releases": allowReleases, name = CLI_APP_NAME, @@ -54,7 +54,6 @@ export default createCommand(config, async ({ values }) => { const token = await getToken(); const host = await getHost(); - const repo = env ? await resolveEnvironment(env, { repo: parentRepo, token, host }) : parentRepo; let createdToken: string; let scope: string | undefined; diff --git a/src/commands/token-delete.ts b/src/commands/token-delete.ts index 663fc9a7..a0811fe1 100644 --- a/src/commands/token-delete.ts +++ b/src/commands/token-delete.ts @@ -5,7 +5,7 @@ import { getOAuthApps, getWriteTokens, } from "../clients/wroom"; -import { resolveEnvironment } from "../environments"; +import { getEnvironment } from "../environments"; import { CommandError, createCommand, type CommandConfig } from "../lib/command"; import { UnknownRequestError } from "../lib/request"; import { getRepositoryName } from "../project"; @@ -22,18 +22,17 @@ const config = { token: { description: "Token value", required: true }, }, options: { - repo: { type: "string", short: "r", description: "Repository domain" }, - env: { type: "string", short: "e", description: "Environment domain" }, + repo: { type: "string", short: "r", description: "Repository or environment domain" }, + env: { type: "string", short: "e", description: "(deprecated) Alias for --repo" }, }, } satisfies CommandConfig; export default createCommand(config, async ({ positionals, values }) => { const [tokenValue] = positionals; - const { repo: parentRepo = await getRepositoryName(), env } = values; + const { env, repo = env ?? (await getEnvironment()) ?? (await getRepositoryName()) } = values; const token = await getToken(); const host = await getHost(); - const repo = env ? await resolveEnvironment(env, { repo: parentRepo, token, host }) : parentRepo; let apps; let writeTokensInfo; diff --git a/src/commands/token-list.ts b/src/commands/token-list.ts index e9841fcd..9d9661b8 100644 --- a/src/commands/token-list.ts +++ b/src/commands/token-list.ts @@ -1,6 +1,6 @@ import { getHost, getToken } from "../auth"; import { getOAuthApps, getWriteTokens } from "../clients/wroom"; -import { resolveEnvironment } from "../environments"; +import { getEnvironment } from "../environments"; import { CommandError, createCommand, type CommandConfig } from "../lib/command"; import { stringify } from "../lib/json"; import { UnknownRequestError } from "../lib/request"; @@ -17,17 +17,16 @@ const config = { `, options: { json: { type: "boolean", description: "Output as JSON" }, - repo: { type: "string", short: "r", description: "Repository domain" }, - env: { type: "string", short: "e", description: "Environment domain" }, + repo: { type: "string", short: "r", description: "Repository or environment domain" }, + env: { type: "string", short: "e", description: "(deprecated) Alias for --repo" }, }, } satisfies CommandConfig; export default createCommand(config, async ({ values }) => { - const { repo: parentRepo = await getRepositoryName(), env, json } = values; + const { env, repo = env ?? (await getEnvironment()) ?? (await getRepositoryName()), json } = values; const token = await getToken(); const host = await getHost(); - const repo = env ? await resolveEnvironment(env, { repo: parentRepo, token, host }) : parentRepo; let apps; let writeTokensInfo; diff --git a/src/commands/webhook-create.ts b/src/commands/webhook-create.ts index a8425b4f..75b4dcdc 100644 --- a/src/commands/webhook-create.ts +++ b/src/commands/webhook-create.ts @@ -1,6 +1,6 @@ import { getHost, getToken } from "../auth"; import { createWebhook, WEBHOOK_TRIGGERS } from "../clients/wroom"; -import { resolveEnvironment } from "../environments"; +import { getEnvironment } from "../environments"; import { CommandError, createCommand, type CommandConfig } from "../lib/command"; import { UnknownRequestError } from "../lib/request"; import { getRepositoryName } from "../project"; @@ -25,8 +25,8 @@ const config = { short: "t", description: "Trigger events (can be repeated)", }, - repo: { type: "string", short: "r", description: "Repository domain" }, - env: { type: "string", short: "e", description: "Environment domain" }, + repo: { type: "string", short: "r", description: "Repository or environment domain" }, + env: { type: "string", short: "e", description: "(deprecated) Alias for --repo" }, }, sections: { TRIGGERS: ` @@ -54,7 +54,13 @@ const config = { export default createCommand(config, async ({ positionals, values }) => { const [webhookUrl] = positionals; - const { repo: parentRepo = await getRepositoryName(), env, name, secret, trigger = [] } = values; + const { + env, + repo = env ?? (await getEnvironment()) ?? (await getRepositoryName()), + name, + secret, + trigger = [], + } = values; // Validate triggers for (const t of trigger) { @@ -67,7 +73,6 @@ export default createCommand(config, async ({ positionals, values }) => { const token = await getToken(); const host = await getHost(); - const repo = env ? await resolveEnvironment(env, { repo: parentRepo, token, host }) : parentRepo; const defaultValue = trigger.length > 0 ? false : true; diff --git a/src/commands/webhook-disable.ts b/src/commands/webhook-disable.ts index 203d7c40..2526f16c 100644 --- a/src/commands/webhook-disable.ts +++ b/src/commands/webhook-disable.ts @@ -1,6 +1,6 @@ import { getHost, getToken } from "../auth"; import { getWebhooks, updateWebhook } from "../clients/wroom"; -import { resolveEnvironment } from "../environments"; +import { getEnvironment } from "../environments"; import { CommandError, createCommand, type CommandConfig } from "../lib/command"; import { UnknownRequestError } from "../lib/request"; import { getRepositoryName } from "../project"; @@ -17,18 +17,17 @@ const config = { url: { description: "Webhook URL", required: true }, }, options: { - repo: { type: "string", short: "r", description: "Repository domain" }, - env: { type: "string", short: "e", description: "Environment domain" }, + repo: { type: "string", short: "r", description: "Repository or environment domain" }, + env: { type: "string", short: "e", description: "(deprecated) Alias for --repo" }, }, } satisfies CommandConfig; export default createCommand(config, async ({ positionals, values }) => { const [webhookUrl] = positionals; - const { repo: parentRepo = await getRepositoryName(), env } = values; + const { env, repo = env ?? (await getEnvironment()) ?? (await getRepositoryName()) } = values; const token = await getToken(); const host = await getHost(); - const repo = env ? await resolveEnvironment(env, { repo: parentRepo, token, host }) : parentRepo; let webhooks; try { webhooks = await getWebhooks({ repo, token, host }); diff --git a/src/commands/webhook-enable.ts b/src/commands/webhook-enable.ts index 9a26d9ee..4c3d9a59 100644 --- a/src/commands/webhook-enable.ts +++ b/src/commands/webhook-enable.ts @@ -1,6 +1,6 @@ import { getHost, getToken } from "../auth"; import { getWebhooks, updateWebhook } from "../clients/wroom"; -import { resolveEnvironment } from "../environments"; +import { getEnvironment } from "../environments"; import { CommandError, createCommand, type CommandConfig } from "../lib/command"; import { UnknownRequestError } from "../lib/request"; import { getRepositoryName } from "../project"; @@ -17,18 +17,17 @@ const config = { url: { description: "Webhook URL", required: true }, }, options: { - repo: { type: "string", short: "r", description: "Repository domain" }, - env: { type: "string", short: "e", description: "Environment domain" }, + repo: { type: "string", short: "r", description: "Repository or environment domain" }, + env: { type: "string", short: "e", description: "(deprecated) Alias for --repo" }, }, } satisfies CommandConfig; export default createCommand(config, async ({ positionals, values }) => { const [webhookUrl] = positionals; - const { repo: parentRepo = await getRepositoryName(), env } = values; + const { env, repo = env ?? (await getEnvironment()) ?? (await getRepositoryName()) } = values; const token = await getToken(); const host = await getHost(); - const repo = env ? await resolveEnvironment(env, { repo: parentRepo, token, host }) : parentRepo; let webhooks; try { webhooks = await getWebhooks({ repo, token, host }); diff --git a/src/commands/webhook-list.ts b/src/commands/webhook-list.ts index c6ada2ef..a242c2e8 100644 --- a/src/commands/webhook-list.ts +++ b/src/commands/webhook-list.ts @@ -1,6 +1,6 @@ import { getHost, getToken } from "../auth"; import { getWebhooks } from "../clients/wroom"; -import { resolveEnvironment } from "../environments"; +import { getEnvironment } from "../environments"; import { CommandError, createCommand, type CommandConfig } from "../lib/command"; import { stringify } from "../lib/json"; import { UnknownRequestError } from "../lib/request"; @@ -17,17 +17,16 @@ const config = { `, options: { json: { type: "boolean", description: "Output as JSON" }, - repo: { type: "string", short: "r", description: "Repository domain" }, - env: { type: "string", short: "e", description: "Environment domain" }, + repo: { type: "string", short: "r", description: "Repository or environment domain" }, + env: { type: "string", short: "e", description: "(deprecated) Alias for --repo" }, }, } satisfies CommandConfig; export default createCommand(config, async ({ values }) => { - const { repo: parentRepo = await getRepositoryName(), env, json } = values; + const { env, repo = env ?? (await getEnvironment()) ?? (await getRepositoryName()), json } = values; const token = await getToken(); const host = await getHost(); - const repo = env ? await resolveEnvironment(env, { repo: parentRepo, token, host }) : parentRepo; let webhooks; try { webhooks = await getWebhooks({ repo, token, host }); diff --git a/src/commands/webhook-remove.ts b/src/commands/webhook-remove.ts index f5e850aa..104047a2 100644 --- a/src/commands/webhook-remove.ts +++ b/src/commands/webhook-remove.ts @@ -1,6 +1,6 @@ import { getHost, getToken } from "../auth"; import { deleteWebhook, getWebhooks } from "../clients/wroom"; -import { resolveEnvironment } from "../environments"; +import { getEnvironment } from "../environments"; import { CommandError, createCommand, type CommandConfig } from "../lib/command"; import { UnknownRequestError } from "../lib/request"; import { getRepositoryName } from "../project"; @@ -17,18 +17,17 @@ const config = { url: { description: "Webhook URL", required: true }, }, options: { - repo: { type: "string", short: "r", description: "Repository domain" }, - env: { type: "string", short: "e", description: "Environment domain" }, + repo: { type: "string", short: "r", description: "Repository or environment domain" }, + env: { type: "string", short: "e", description: "(deprecated) Alias for --repo" }, }, } satisfies CommandConfig; export default createCommand(config, async ({ positionals, values }) => { const [webhookUrl] = positionals; - const { repo: parentRepo = await getRepositoryName(), env } = values; + const { env, repo = env ?? (await getEnvironment()) ?? (await getRepositoryName()) } = values; const token = await getToken(); const host = await getHost(); - const repo = env ? await resolveEnvironment(env, { repo: parentRepo, token, host }) : parentRepo; let webhooks; try { webhooks = await getWebhooks({ repo, token, host }); diff --git a/src/commands/webhook-set-triggers.ts b/src/commands/webhook-set-triggers.ts index aa897525..b08fb2ca 100644 --- a/src/commands/webhook-set-triggers.ts +++ b/src/commands/webhook-set-triggers.ts @@ -1,6 +1,6 @@ import { getHost, getToken } from "../auth"; import { getWebhooks, updateWebhook, WEBHOOK_TRIGGERS } from "../clients/wroom"; -import { resolveEnvironment } from "../environments"; +import { getEnvironment } from "../environments"; import { CommandError, createCommand, type CommandConfig } from "../lib/command"; import { UnknownRequestError } from "../lib/request"; import { getRepositoryName } from "../project"; @@ -24,8 +24,8 @@ const config = { description: "Trigger events (can be repeated)", required: true, }, - repo: { type: "string", short: "r", description: "Repository domain" }, - env: { type: "string", short: "e", description: "Environment domain" }, + repo: { type: "string", short: "r", description: "Repository or environment domain" }, + env: { type: "string", short: "e", description: "(deprecated) Alias for --repo" }, }, sections: { TRIGGERS: ` @@ -41,7 +41,7 @@ const config = { export default createCommand(config, async ({ positionals, values }) => { const [webhookUrl] = positionals; - const { repo: parentRepo = await getRepositoryName(), env, trigger = [] } = values; + const { env, repo = env ?? (await getEnvironment()) ?? (await getRepositoryName()), trigger = [] } = values; // Validate triggers for (const t of trigger) { @@ -54,7 +54,6 @@ export default createCommand(config, async ({ positionals, values }) => { const token = await getToken(); const host = await getHost(); - const repo = env ? await resolveEnvironment(env, { repo: parentRepo, token, host }) : parentRepo; let webhooks; try { webhooks = await getWebhooks({ repo, token, host }); diff --git a/src/commands/webhook-view.ts b/src/commands/webhook-view.ts index b8bbe371..7a7ea122 100644 --- a/src/commands/webhook-view.ts +++ b/src/commands/webhook-view.ts @@ -1,6 +1,6 @@ import { getHost, getToken } from "../auth"; import { getWebhooks, WEBHOOK_TRIGGERS } from "../clients/wroom"; -import { resolveEnvironment } from "../environments"; +import { getEnvironment } from "../environments"; import { CommandError, createCommand, type CommandConfig } from "../lib/command"; import { UnknownRequestError } from "../lib/request"; import { getRepositoryName } from "../project"; @@ -17,18 +17,17 @@ const config = { url: { description: "Webhook URL", required: true }, }, options: { - repo: { type: "string", short: "r", description: "Repository domain" }, - env: { type: "string", short: "e", description: "Environment domain" }, + repo: { type: "string", short: "r", description: "Repository or environment domain" }, + env: { type: "string", short: "e", description: "(deprecated) Alias for --repo" }, }, } satisfies CommandConfig; export default createCommand(config, async ({ positionals, values }) => { const [webhookUrl] = positionals; - const { repo: parentRepo = await getRepositoryName(), env } = values; + const { env, repo = env ?? (await getEnvironment()) ?? (await getRepositoryName()) } = values; const token = await getToken(); const host = await getHost(); - const repo = env ? await resolveEnvironment(env, { repo: parentRepo, token, host }) : parentRepo; let webhooks; try { webhooks = await getWebhooks({ repo, token, host }); diff --git a/src/environments.ts b/src/environments.ts index 3ed8a75f..46a2efb4 100644 --- a/src/environments.ts +++ b/src/environments.ts @@ -56,17 +56,6 @@ export async function getUserEnvironments(config: { return userEnvironments; } -export async function resolveEnvironment( - env: string, - config: { repo: string; token: string | undefined; host: string }, -): Promise { - const availableEnvironments = await getUserEnvironments(config); - const match = availableEnvironments.find((environment) => environment.domain === env); - if (match) return match.domain; - - throw new InvalidEnvironmentError(env, availableEnvironments, config.repo); -} - export class InvalidEnvironmentError extends Error { name = "InvalidEnvironmentError"; repo: string; diff --git a/test/pull.test.ts b/test/pull.test.ts index af3bbb6e..6b8dd40b 100644 --- a/test/pull.test.ts +++ b/test/pull.test.ts @@ -20,12 +20,6 @@ it.sequential("supports --help", async ({ expect, prismic }) => { expect(stdout).toContain("prismic pull [options]"); }); -it.sequential("rejects an unknown --env", async ({ expect, prismic, repo }) => { - const { stderr, exitCode } = await prismic("pull", ["--repo", repo, "--env", "does-not-exist"]); - expect(exitCode).toBe(1); - expect(stderr).toContain(`No environments available on repository "${repo}".`); -}); - it.sequential("pulls slices and custom types from remote", async ({ expect, project, diff --git a/test/push.serial.test.ts b/test/push.serial.test.ts index 5ed8ace8..804ceefa 100644 --- a/test/push.serial.test.ts +++ b/test/push.serial.test.ts @@ -15,12 +15,6 @@ it("supports --help", async ({ expect, prismic }) => { expect(stdout).toContain("prismic push [options]"); }); -it("rejects an unknown --env", async ({ expect, prismic, repo }) => { - const { stderr, exitCode } = await prismic("push", ["--repo", repo, "--env", "does-not-exist"]); - expect(exitCode).toBe(1); - expect(stderr).toContain(`No environments available on repository "${repo}".`); -}); - it("pushes a new local custom type to remote", async ({ expect, project, diff --git a/test/status.serial.test.ts b/test/status.serial.test.ts index dd5c4668..760941b0 100644 --- a/test/status.serial.test.ts +++ b/test/status.serial.test.ts @@ -57,20 +57,6 @@ it("reports remote-only models when added remotely but not pulled", async ({ expect(stdout).toContain("prismic pull"); }); -it("rejects an unknown --env", async ({ expect, prismic, repo }) => { - const { stderr, exitCode } = await prismic("status", ["--repo", repo, "--env", "does-not-exist"]); - expect(exitCode).toBe(1); - expect(stderr).toContain(`No environments available on repository "${repo}".`); -}); - -it("warns and skips --env when not logged in", async ({ expect, prismic, logout, repo }) => { - await logout(); - const { stdout, exitCode } = await prismic("status", ["--repo", repo, "--env", "anything"]); - expect(exitCode).toBe(0); - expect(stdout).toContain(`Repository: ${repo}`); - expect(stdout).toContain("Environment: anything"); -}); - it("reports differing models when local and remote disagree", async ({ expect, project,