-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add repo commands #69
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
+580
−4
Merged
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| import { getAdapter } from "../adapters"; | ||
| import { getHost, getToken } from "../auth"; | ||
| import { checkIsDomainAvailable, createRepository } from "../clients/wroom"; | ||
| import { CommandError, createCommand, type CommandConfig } from "../lib/command"; | ||
| import { UnknownRequestError } from "../lib/request"; | ||
|
|
||
| const MAX_DOMAIN_TRIES = 5; | ||
|
|
||
| const config = { | ||
| name: "prismic repo create", | ||
| description: "Create a new Prismic repository.", | ||
| options: { | ||
| name: { type: "string", short: "n", description: "Display name for the repository" }, | ||
| }, | ||
| } satisfies CommandConfig; | ||
|
|
||
| export default createCommand(config, async ({ values }) => { | ||
| const { name } = values; | ||
|
|
||
| const token = await getToken(); | ||
| const host = await getHost(); | ||
|
|
||
| const domain = await findAvailableDomain({ token, host }); | ||
| if (!domain) { | ||
| throw new CommandError("Failed to create a repository. Please try again."); | ||
| } | ||
|
|
||
| const adapter = await getAdapter().catch(() => undefined); | ||
| const framework = adapter?.id ?? "other"; | ||
|
|
||
| try { | ||
| await createRepository({ domain, name: name ?? domain, framework, token, host }); | ||
| } catch (error) { | ||
| if (error instanceof UnknownRequestError) { | ||
| const message = await error.text(); | ||
| throw new CommandError(`Failed to create repository: ${message}`); | ||
| } | ||
| throw error; | ||
| } | ||
|
|
||
| console.info(`Repository created: ${domain}`); | ||
| console.info(`URL: https://${domain}.${host}/`); | ||
| }); | ||
|
|
||
| async function findAvailableDomain(config: { | ||
| token: string | undefined; | ||
| host: string; | ||
| }): Promise<string | undefined> { | ||
| const { token, host } = config; | ||
| let domain; | ||
| for (let i = 0; i < MAX_DOMAIN_TRIES; i++) { | ||
| const candidate = crypto.randomUUID().replace(/-/g, "").slice(0, 8); | ||
| const available = await checkIsDomainAvailable({ domain: candidate, token, host }); | ||
| if (available) { | ||
| domain = candidate; | ||
| break; | ||
| } | ||
| } | ||
| return 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,58 @@ | ||
| import { getHost, getToken } from "../auth"; | ||
| import { getProfile } from "../clients/user"; | ||
| import { CommandError, createCommand, type CommandConfig } from "../lib/command"; | ||
| import { stringify } from "../lib/json"; | ||
| import { UnknownRequestError } from "../lib/request"; | ||
|
|
||
| const config = { | ||
| name: "prismic repo list", | ||
| description: "List all Prismic repositories associated with your account.", | ||
| options: { | ||
| json: { type: "boolean", description: "Output as JSON" }, | ||
| }, | ||
| } satisfies CommandConfig; | ||
|
|
||
| export default createCommand(config, async ({ values }) => { | ||
| const { json } = values; | ||
|
|
||
| const token = await getToken(); | ||
| const host = await getHost(); | ||
|
|
||
| let profile; | ||
| try { | ||
| profile = await getProfile({ token, host }); | ||
| } catch (error) { | ||
| if (error instanceof UnknownRequestError) { | ||
| const message = await error.text(); | ||
| throw new CommandError(`Failed to list repositories: ${message}`); | ||
| } | ||
| throw error; | ||
| } | ||
|
|
||
| const repos = profile.repositories; | ||
|
|
||
| if (json) { | ||
| console.info( | ||
| stringify( | ||
| repos.map((repo) => ({ | ||
| domain: repo.domain, | ||
| name: repo.name ?? null, | ||
| role: repo.role ?? null, | ||
| url: `https://${repo.domain}.${host}/`, | ||
| })), | ||
| ), | ||
| ); | ||
| return; | ||
| } | ||
|
|
||
| if (repos.length === 0) { | ||
| console.info("No repositories found."); | ||
| return; | ||
| } | ||
|
|
||
| for (const repo of repos) { | ||
| const name = repo.name || "(no name)"; | ||
| const role = repo.role ? ` ${repo.role}` : ""; | ||
| console.info(`${repo.domain} ${name}${role}`); | ||
| } | ||
| }); |
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 { getHost, getToken } from "../auth"; | ||
| import { type RepositoryAccessLevel, setRepositoryAccess } from "../clients/wroom"; | ||
| import { CommandError, createCommand, type CommandConfig } from "../lib/command"; | ||
| import { UnknownRequestError } from "../lib/request"; | ||
| import { getRepositoryName } from "../project"; | ||
|
|
||
| const VALID_LEVELS: RepositoryAccessLevel[] = ["private", "public", "open"]; | ||
|
|
||
| const config = { | ||
| name: "prismic repo set-api-access", | ||
| description: ` | ||
| Set the Content API access level of a Prismic repository. | ||
|
|
||
| By default, this command reads the repository from prismic.config.json at the | ||
| project root. | ||
| `, | ||
| positionals: { | ||
| level: { description: `Access level (${VALID_LEVELS.join(", ")})` }, | ||
| }, | ||
| options: { | ||
| repo: { type: "string", short: "r", description: "Repository domain" }, | ||
| }, | ||
| } satisfies CommandConfig; | ||
|
|
||
| export default createCommand(config, async ({ positionals, values }) => { | ||
| const [level] = positionals; | ||
| const { repo = await getRepositoryName() } = values; | ||
|
|
||
| if (!level) { | ||
| throw new CommandError("Missing required argument: <level>"); | ||
| } | ||
|
|
||
| if (!VALID_LEVELS.includes(level as RepositoryAccessLevel)) { | ||
| throw new CommandError( | ||
| `Invalid access level: ${level}. Must be one of: ${VALID_LEVELS.join(", ")}`, | ||
| ); | ||
| } | ||
|
|
||
| const token = await getToken(); | ||
| const host = await getHost(); | ||
|
|
||
| try { | ||
| await setRepositoryAccess(level as RepositoryAccessLevel, { repo, token, host }); | ||
| } catch (error) { | ||
| if (error instanceof UnknownRequestError) { | ||
| const message = await error.text(); | ||
| throw new CommandError(`Failed to set repository access: ${message}`); | ||
| } | ||
| throw error; | ||
| } | ||
|
|
||
| console.info(`Repository access set to: ${level}`); | ||
| }); |
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,46 @@ | ||
| import { getHost, getToken } from "../auth"; | ||
| import { setRepositoryName } from "../clients/wroom"; | ||
| import { CommandError, createCommand, type CommandConfig } from "../lib/command"; | ||
| import { UnknownRequestError } from "../lib/request"; | ||
| import { getRepositoryName } from "../project"; | ||
|
|
||
| const config = { | ||
| name: "prismic repo set-name", | ||
| description: ` | ||
| Set the display name of a Prismic repository. | ||
|
|
||
| By default, this command reads the repository from prismic.config.json at the | ||
| project root. | ||
| `, | ||
| positionals: { | ||
| name: { description: "Display name for the repository" }, | ||
| }, | ||
| options: { | ||
| repo: { type: "string", short: "r", description: "Repository domain" }, | ||
| }, | ||
| } satisfies CommandConfig; | ||
|
|
||
| export default createCommand(config, async ({ positionals, values }) => { | ||
| const [displayName] = positionals; | ||
| const { repo = await getRepositoryName() } = values; | ||
|
|
||
| if (!displayName) { | ||
| throw new CommandError("Missing required argument: <name>"); | ||
| } | ||
|
|
||
| const token = await getToken(); | ||
| const host = await getHost(); | ||
|
|
||
| let confirmedName; | ||
| try { | ||
| confirmedName = await setRepositoryName(displayName, { repo, token, host }); | ||
| } catch (error) { | ||
| if (error instanceof UnknownRequestError) { | ||
| const message = await error.text(); | ||
| throw new CommandError(`Failed to set repository name: ${message}`); | ||
| } | ||
| throw error; | ||
| } | ||
|
|
||
| console.info(`Repository name set to: ${confirmedName}`); | ||
| }); |
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.