Skip to content

Commit

Permalink
feat(cli): copy additions for remote project bootstrapper (#8141)
Browse files Browse the repository at this point in the history
  • Loading branch information
RostiMelk authored Jan 6, 2025
1 parent 4e60fe9 commit f453cbc
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
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})`)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -846,7 +846,7 @@ export default async function initSanity(

const projectChoices = projects.map((project) => ({
value: project.id,
name: `${project.displayName} [${project.id}]`,
name: `${project.displayName} (${project.id})`,
}))

const selected = await prompt.single({
Expand Down

0 comments on commit f453cbc

Please sign in to comment.