diff --git a/packages/@sanity/cli/src/actions/init-project/bootstrapRemoteTemplate.ts b/packages/@sanity/cli/src/actions/init-project/bootstrapRemoteTemplate.ts index efa32999057..7eca8f489ca 100644 --- a/packages/@sanity/cli/src/actions/init-project/bootstrapRemoteTemplate.ts +++ b/packages/@sanity/cli/src/actions/init-project/bootstrapRemoteTemplate.ts @@ -1,6 +1,9 @@ import {mkdir} from 'node:fs/promises' import {join} from 'node:path' +import {type Framework, frameworks} from '@vercel/frameworks' +import {detectFrameworkRecord, LocalFileSystemDetector} from '@vercel/fs-detectors' + import {debug} from '../../debug' import {type CliCommandContext} from '../../types' import { @@ -9,7 +12,6 @@ import { downloadAndExtractRepo, generateSanityApiReadToken, getMonoRepo, - isNextJsTemplate, type RepoInfo, tryApplyPackageName, validateRemoteTemplate, @@ -56,12 +58,16 @@ export async function bootstrapRemoteTemplate( const readToken = needsReadToken ? await generateSanityApiReadToken('API Read Token', variables.projectId, apiClient) : undefined - const isNext = await isNextJsTemplate(outputPath) - const envName = isNext ? '.env.local' : '.env' - for (const folder of packages ?? ['']) { - const path = join(outputPath, folder) - await applyEnvVariables(path, {...variables, readToken}, envName) + for (const pkg of packages ?? ['']) { + const packagePath = join(outputPath, pkg) + const packageFramework: Framework | null = await detectFrameworkRecord({ + fs: new LocalFileSystemDetector(packagePath), + frameworkList: frameworks as readonly Framework[], + }) + // Next.js uses `.env.local` for local environment variables + const envName = packageFramework?.slug === 'nextjs' ? '.env.local' : '.env' + await applyEnvVariables(packagePath, {...variables, readToken}, envName) } debug('Setting package name to %s', packageName) diff --git a/packages/@sanity/cli/src/util/remoteTemplate.ts b/packages/@sanity/cli/src/util/remoteTemplate.ts index 53e987fccc6..fbfc184e3f8 100644 --- a/packages/@sanity/cli/src/util/remoteTemplate.ts +++ b/packages/@sanity/cli/src/util/remoteTemplate.ts @@ -410,16 +410,6 @@ export async function validateRemoteTemplate( } } -export async function isNextJsTemplate(root: string): Promise { - try { - const packageJson = await readFile(join(root, 'package.json'), 'utf8') - const pkg = JSON.parse(packageJson) - return !!(pkg.dependencies?.next || pkg.devDependencies?.next) - } catch { - return false - } -} - export async function checkNeedsReadToken(root: string): Promise { try { const templatePath = await Promise.any(