Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/adapters/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export class NoSupportedFrameworkError extends Error {

export abstract class Adapter {
abstract readonly id: string;
abstract readonly repositoryEnvVar: string;

abstract onProjectInitialized(): Promise<void> | void;
abstract onSliceCreated(model: SharedSlice, library: URL): Promise<void> | void;
Expand Down
6 changes: 4 additions & 2 deletions src/adapters/nextjs.templates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,8 @@ export function prismicIOFileTemplate(args: {
/**
* The project's Prismic repository name.
*/
export const repositoryName = prismicConfig.repositoryName;
export const repositoryName =
process.env.NEXT_PUBLIC_PRISMIC_ENVIRONMENT || prismicConfig.repositoryName;

${createClientContents}
`;
Expand All @@ -369,7 +370,8 @@ export function prismicIOFileTemplate(args: {
/**
* The project's Prismic repository name.
*/
export const repositoryName = prismicConfig.repositoryName;
export const repositoryName =
process.env.NEXT_PUBLIC_PRISMIC_ENVIRONMENT || prismicConfig.repositoryName;

${createClientContents}
`;
Expand Down
1 change: 1 addition & 0 deletions src/adapters/nextjs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {

export class NextJsAdapter extends Adapter {
readonly id = "next";
readonly repositoryEnvVar = "NEXT_PUBLIC_PRISMIC_ENVIRONMENT";

async setupProject(): Promise<void> {
await addDependencies({
Expand Down
1 change: 1 addition & 0 deletions src/adapters/nuxt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const NUXT_PRISMIC = "@nuxtjs/prismic";

export class NuxtAdapter extends Adapter {
readonly id = "nuxt";
readonly repositoryEnvVar = "NUXT_PUBLIC_PRISMIC_ENVIRONMENT";

async setupProject(): Promise<void> {
await addDependencies({
Expand Down
6 changes: 4 additions & 2 deletions src/adapters/sveltekit.templates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ export function prismicIOFileTemplate(args: { typescript: boolean }): string {
/**
* The project's Prismic repository name.
*/
export const repositoryName = prismicConfig.repositoryName;
export const repositoryName =
import.meta.env.VITE_PRISMIC_ENVIRONMENT || prismicConfig.repositoryName;

/**
* Creates a Prismic client for the project's repository. The client is used to
Expand Down Expand Up @@ -45,7 +46,8 @@ export function prismicIOFileTemplate(args: { typescript: boolean }): string {
/**
* The project's Prismic repository name.
*/
export const repositoryName = prismicConfig.repositoryName;
export const repositoryName =
import.meta.env.VITE_PRISMIC_ENVIRONMENT || prismicConfig.repositoryName;

/**
* Creates a Prismic client for the project's repository. The client is used to
Expand Down
1 change: 1 addition & 0 deletions src/adapters/sveltekit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {

export class SvelteKitAdapter extends Adapter {
readonly id = "sveltekit";
readonly repositoryEnvVar = "VITE_PRISMIC_ENVIRONMENT";

async setupProject(): Promise<void> {
await addDependencies({
Expand Down
19 changes: 19 additions & 0 deletions src/commands/env-active.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { getActiveRepositoryName } from "../environments";
import { createCommand, type CommandConfig } from "../lib/command";
import { getRepositoryName } from "../project";

const config = {
name: "prismic env active",
description: `
Print the active environment, or production if none is set.
`,
} satisfies CommandConfig;

export default createCommand(config, async () => {
const active = await getActiveRepositoryName();
if (active) {
console.info(active);
return;
}
console.info(`${await getRepositoryName()} (production)`);
});
32 changes: 32 additions & 0 deletions src/commands/env-list.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { getHost, getToken } from "../auth";
import { getActiveRepositoryName, getAvailableEnvironments } from "../environments";
import { createCommand, type CommandConfig } from "../lib/command";
import { formatTable } from "../lib/string";
import { getRepositoryName } from "../project";

const config = {
name: "prismic env list",
description: `
List environments available on a Prismic repository, including production.
`,
options: {
repo: { type: "string", short: "r", description: "Repository domain" },
},
} satisfies CommandConfig;

export default createCommand(config, async ({ values }) => {
const { repo = await getRepositoryName() } = values;

const token = await getToken();
const host = await getHost();

const environments = await getAvailableEnvironments({ repo, token, host });
const active = (await getActiveRepositoryName()) ?? repo;

const rows = environments.map((environment) => {
const label = environment.kind === "prod" ? "production" : environment.name;
const marker = environment.domain === active ? " (active)" : "";
return [environment.domain, `${label}${marker}`];
});
console.info(formatTable(rows, { headers: ["DOMAIN", "NAME"] }));
});
43 changes: 43 additions & 0 deletions src/commands/env-set.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { getAdapter } from "../adapters";
import { getHost, getToken } from "../auth";
import { resolveEnvironment } from "../environments";
import { createCommand, type CommandConfig } from "../lib/command";
import { removeEnvVar, setEnvVar } from "../lib/env-file";
import { findProjectRoot, getRepositoryName } from "../project";

const config = {
name: "prismic env set",
description: `
Set the active environment for local development.

Writes the environment to .env.local. The website and CLI read it as the
active repository. Setting production removes it.
`,
positionals: {
environment: { description: "Environment domain", required: true },
},
options: {
repo: { type: "string", short: "r", description: "Repository domain" },
},
} satisfies CommandConfig;

export default createCommand(config, async ({ positionals, values }) => {
const [environment] = positionals;
const { repo = await getRepositoryName() } = values;

const token = await getToken();
const host = await getHost();

const domain = await resolveEnvironment(environment, { repo, token, host });
const adapter = await getAdapter();
const path = new URL(".env.local", await findProjectRoot());

if (domain === repo) {
await removeEnvVar(path, adapter.repositoryEnvVar);
console.info(`Active environment set to production: ${domain}`);
return;
}

await setEnvVar(path, adapter.repositoryEnvVar, domain);
console.info(`Active environment set: ${domain}`);
});
18 changes: 18 additions & 0 deletions src/commands/env-unset.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { getAdapter } from "../adapters";
import { createCommand, type CommandConfig } from "../lib/command";
import { removeEnvVar } from "../lib/env-file";
import { findProjectRoot } from "../project";

const config = {
name: "prismic env unset",
description: `
Revert to production by removing the active environment from .env.local.
`,
} satisfies CommandConfig;

export default createCommand(config, async () => {
const adapter = await getAdapter();
const projectRoot = await findProjectRoot();
await removeEnvVar(new URL(".env.local", projectRoot), adapter.repositoryEnvVar);
console.info("Active environment reverted to production.");
});
28 changes: 28 additions & 0 deletions src/commands/env.ts
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 Prismic environment.",
commands: {
list: {
handler: envList,
description: "List environments",
},
set: {
handler: envSet,
description: "Set the active environment",
},
unset: {
handler: envUnset,
description: "Revert to production",
},
active: {
handler: envActive,
description: "Print the active environment",
},
},
});
5 changes: 2 additions & 3 deletions src/commands/locale-add.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { getHost, getToken } from "../auth";
import { upsertLocale } from "../clients/locale";
import { resolveEnvironment } from "../environments";
import { resolveEnvironment, resolveRepositoryName } from "../environments";
import { CommandError, createCommand, type CommandConfig } from "../lib/command";
import { UnknownRequestError } from "../lib/request";
import { getRepositoryName } from "../project";

const config = {
name: "prismic locale add",
Expand All @@ -26,7 +25,7 @@ const config = {

export default createCommand(config, async ({ positionals, values }) => {
const [code] = positionals;
const { repo: parentRepo = await getRepositoryName(), env, master = false, name } = values;
const { repo: parentRepo = await resolveRepositoryName(), env, master = false, name } = values;

const token = await getToken();
const host = await getHost();
Expand Down
5 changes: 2 additions & 3 deletions src/commands/locale-list.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { getHost, getToken } from "../auth";
import { getLocales } from "../clients/locale";
import { resolveEnvironment } from "../environments";
import { resolveEnvironment, resolveRepositoryName } from "../environments";
import { CommandError, createCommand, type CommandConfig } from "../lib/command";
import { stringify } from "../lib/json";
import { UnknownRequestError } from "../lib/request";
import { formatTable } from "../lib/string";
import { getRepositoryName } from "../project";

const config = {
name: "prismic locale list",
Expand All @@ -23,7 +22,7 @@ const config = {
} satisfies CommandConfig;

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

const token = await getToken();
const host = await getHost();
Expand Down
5 changes: 2 additions & 3 deletions src/commands/locale-remove.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { getHost, getToken } from "../auth";
import { removeLocale } from "../clients/locale";
import { resolveEnvironment } from "../environments";
import { resolveEnvironment, resolveRepositoryName } from "../environments";
import { CommandError, createCommand, type CommandConfig } from "../lib/command";
import { UnknownRequestError } from "../lib/request";
import { getRepositoryName } from "../project";

const config = {
name: "prismic locale remove",
Expand All @@ -24,7 +23,7 @@ const config = {

export default createCommand(config, async ({ positionals, values }) => {
const [code] = positionals;
const { repo: parentRepo = await getRepositoryName(), env } = values;
const { repo: parentRepo = await resolveRepositoryName(), env } = values;

const token = await getToken();
const host = await getHost();
Expand Down
5 changes: 2 additions & 3 deletions src/commands/locale-set-master.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { getHost, getToken } from "../auth";
import { getLocales, upsertLocale } from "../clients/locale";
import { resolveEnvironment } from "../environments";
import { resolveEnvironment, resolveRepositoryName } from "../environments";
import { CommandError, createCommand, type CommandConfig } from "../lib/command";
import { UnknownRequestError } from "../lib/request";
import { getRepositoryName } from "../project";

const config = {
name: "prismic locale set-master",
Expand All @@ -24,7 +23,7 @@ const config = {

export default createCommand(config, async ({ positionals, values }) => {
const [code] = positionals;
const { repo: parentRepo = await getRepositoryName(), env } = values;
const { repo: parentRepo = await resolveRepositoryName(), env } = values;

const token = await getToken();
const host = await getHost();
Expand Down
5 changes: 2 additions & 3 deletions src/commands/preview-add.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { getHost, getToken } from "../auth";
import { addPreview } from "../clients/core";
import { resolveEnvironment } from "../environments";
import { resolveEnvironment, resolveRepositoryName } from "../environments";
import { CommandError, createCommand, type CommandConfig } from "../lib/command";
import { UnknownRequestError } from "../lib/request";
import { getRepositoryName } from "../project";

const config = {
name: "prismic preview add",
Expand All @@ -25,7 +24,7 @@ const config = {

export default createCommand(config, async ({ positionals, values }) => {
const [previewUrl] = positionals;
const { repo: parentRepo = await getRepositoryName(), env, name } = values;
const { repo: parentRepo = await resolveRepositoryName(), env, name } = values;

let parsed: URL;
try {
Expand Down
5 changes: 2 additions & 3 deletions src/commands/preview-list.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { getHost, getToken } from "../auth";
import { getPreviews, getSimulatorUrl } from "../clients/core";
import { resolveEnvironment } from "../environments";
import { resolveEnvironment, resolveRepositoryName } from "../environments";
import { CommandError, createCommand, type CommandConfig } from "../lib/command";
import { stringify } from "../lib/json";
import { UnknownRequestError } from "../lib/request";
import { formatTable } from "../lib/string";
import { getRepositoryName } from "../project";

const config = {
name: "prismic preview list",
Expand All @@ -23,7 +22,7 @@ const config = {
} satisfies CommandConfig;

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

const token = await getToken();
const host = await getHost();
Expand Down
5 changes: 2 additions & 3 deletions src/commands/preview-remove.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { getHost, getToken } from "../auth";
import { getPreviews, removePreview } from "../clients/core";
import { resolveEnvironment } from "../environments";
import { resolveEnvironment, resolveRepositoryName } from "../environments";
import { CommandError, createCommand, type CommandConfig } from "../lib/command";
import { UnknownRequestError } from "../lib/request";
import { getRepositoryName } from "../project";

const config = {
name: "prismic preview remove",
Expand All @@ -24,7 +23,7 @@ const config = {

export default createCommand(config, async ({ positionals, values }) => {
const [previewUrl] = positionals;
const { repo: parentRepo = await getRepositoryName(), env } = values;
const { repo: parentRepo = await resolveRepositoryName(), env } = values;

const token = await getToken();
const host = await getHost();
Expand Down
5 changes: 2 additions & 3 deletions src/commands/preview-set-simulator.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { getHost, getToken } from "../auth";
import { setSimulatorUrl } from "../clients/core";
import { resolveEnvironment } from "../environments";
import { resolveEnvironment, resolveRepositoryName } from "../environments";
import { CommandError, createCommand, type CommandConfig } from "../lib/command";
import { UnknownRequestError } from "../lib/request";
import { getRepositoryName } from "../project";

const config = {
name: "prismic preview set-simulator",
Expand All @@ -30,7 +29,7 @@ const config = {

export default createCommand(config, async ({ positionals, values }) => {
const [urlArg] = positionals;
const { repo: parentRepo = await getRepositoryName(), env } = values;
const { repo: parentRepo = await resolveRepositoryName(), env } = values;

let parsed: URL;
try {
Expand Down
Loading
Loading