Skip to content
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

feat(cli): copy additions for remote project bootstrapper #8141

Merged
merged 2 commits into from
Jan 6, 2025
Merged
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ export interface BootstrapRemoteOptions {
variables: GenerateConfigOptions['variables']
}

const SANITY_DEFAULT_PORT = 3333
const READ_TOKEN_LABEL = 'Live Preview API'
const INITIAL_COMMIT_MESSAGE = 'Initial commit from Sanity CLI'

export async function bootstrapRemoteTemplate(
Expand All @@ -45,6 +47,7 @@ export async function bootstrapRemoteTemplate(
headers.Authorization = `Bearer ${bearerToken}`
}
const spinner = output.spinner(`Bootstrapping files from template "${name}"`).start()
const corsAdded: number[] = [SANITY_DEFAULT_PORT]

debug('Validating remote template')
const fileReader = new GitHubFileReader(contentsUrl, headers)
Expand All @@ -67,7 +70,7 @@ export async function bootstrapRemoteTemplate(

debug('Applying environment variables')
const readToken = needsReadToken
? await generateSanityApiReadToken('API Read Token', variables.projectId, apiClient)
? await generateSanityApiReadToken(READ_TOKEN_LABEL, variables.projectId, apiClient)
: undefined

for (const pkg of packages ?? ['']) {
Expand All @@ -76,10 +79,13 @@ export async function bootstrapRemoteTemplate(
fs: new LocalFileSystemDetector(packagePath),
frameworkList: frameworks as readonly Framework[],
})
const port = getDefaultPortForFramework(packageFramework?.slug)

debug('Setting CORS origin to http://localhost:%d', port)
await setCorsOrigin(`http://localhost:${port}`, variables.projectId, apiClient)
const port = getDefaultPortForFramework(packageFramework?.slug)
if (corsAdded.includes(port) === false) {
debug('Setting CORS origin to http://localhost:%d', port)
await setCorsOrigin(`http://localhost:${port}`, variables.projectId, apiClient)
corsAdded.push(port)
}

debug('Applying environment variables to %s', pkg)
// Next.js uses `.env.local` for local environment variables
Expand All @@ -97,4 +103,10 @@ export async function bootstrapRemoteTemplate(
await updateInitialTemplateMetadata(apiClient, variables.projectId, `external-${name}`)

spinner.succeed()
if (corsAdded.length) {
output.success(`CORS origins added (${corsAdded.map((p) => `localhost:${p}`).join(', ')})`)
}
if (readToken) {
output.success(`API token generated (${READ_TOKEN_LABEL})`)
}
}
Loading