Skip to content

Commit

Permalink
fix(cli) use Next.js specific defaults for new projects (#7465)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimeonGriggs authored and bjoerge committed Sep 10, 2024
1 parent ea3a79a commit de03c57
Showing 1 changed file with 36 additions and 3 deletions.
39 changes: 36 additions & 3 deletions packages/@sanity/cli/src/actions/init-project/initProject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,11 @@ export default async function initSanity(
throw new Error('`--reconfigure` is deprecated - manual configuration is now required')
}

const envFilename = typeof env === 'string' ? env : '.env'
let envFilenameDefault = '.env'
if (detectedFramework && detectedFramework.slug === 'nextjs') {
envFilenameDefault = '.env.local'
}
const envFilename = typeof env === 'string' ? env : envFilenameDefault
if (!envFilename.startsWith('.env')) {
throw new Error(`Env filename must start with .env`)
}
Expand Down Expand Up @@ -433,10 +437,39 @@ export default async function initSanity(
const appendEnv = unattended ? true : await promptForAppendEnv(prompt, envFilename)

if (appendEnv) {
await createOrAppendEnvVars(envFilename, detectedFramework, {
log: true,
await createOrAppendEnvVars(envFilename, detectedFramework, {log: true})
}

if (embeddedStudio) {
const nextjsLocalDevOrigin = 'http://localhost:3000'
const existingCorsOrigins = await apiClient({api: {projectId}}).request({
method: 'GET',
uri: '/cors',
})
const hasExistingCorsOrigin = existingCorsOrigins.some(
(item: {origin: string}) => item.origin === nextjsLocalDevOrigin,
)
if (!hasExistingCorsOrigin) {
await apiClient({api: {projectId}})
.request({
method: 'POST',
url: '/cors',
body: {origin: nextjsLocalDevOrigin, allowCredentials: true},
maxRedirects: 0,
})
.then((res) => {
print(
res.id
? `Added ${nextjsLocalDevOrigin} to CORS origins`
: `Failed to add ${nextjsLocalDevOrigin} to CORS origins`,
)
})
.catch((error) => {
print(`Failed to add ${nextjsLocalDevOrigin} to CORS origins`, error)
})
}
}

const {chosen} = await getPackageManagerChoice(workDir, {interactive: false})
trace.log({step: 'selectPackageManager', selectedOption: chosen})
const packages = ['@sanity/vision@3', 'sanity@3', '@sanity/image-url@1', 'styled-components@6']
Expand Down

0 comments on commit de03c57

Please sign in to comment.