Skip to content

Commit

Permalink
feat: copy additions
Browse files Browse the repository at this point in the history
  • Loading branch information
RostiMelk committed Dec 25, 2024
1 parent ddb65fa commit a646f86
Showing 1 changed file with 16 additions and 4 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})`)
}
}

0 comments on commit a646f86

Please sign in to comment.