From 988077268b9376f507625c6f4a0d1199737d3435 Mon Sep 17 00:00:00 2001 From: RostiMelk Date: Tue, 10 Dec 2024 13:14:43 +0100 Subject: [PATCH 1/4] feat: use framework detector in remote template bootstrapper --- .../init-project/bootstrapRemoteTemplate.ts | 17 +++++++++++------ packages/@sanity/cli/src/util/remoteTemplate.ts | 10 ---------- 2 files changed, 11 insertions(+), 16 deletions(-) diff --git a/packages/@sanity/cli/src/actions/init-project/bootstrapRemoteTemplate.ts b/packages/@sanity/cli/src/actions/init-project/bootstrapRemoteTemplate.ts index efa32999057..58a8f0dee92 100644 --- a/packages/@sanity/cli/src/actions/init-project/bootstrapRemoteTemplate.ts +++ b/packages/@sanity/cli/src/actions/init-project/bootstrapRemoteTemplate.ts @@ -1,5 +1,7 @@ import {mkdir} from 'node:fs/promises' import {join} from 'node:path' +import {detectFrameworkRecord, LocalFileSystemDetector} from '@vercel/fs-detectors' +import {type Framework, frameworks} from '@vercel/frameworks' import {debug} from '../../debug' import {type CliCommandContext} from '../../types' @@ -9,7 +11,6 @@ import { downloadAndExtractRepo, generateSanityApiReadToken, getMonoRepo, - isNextJsTemplate, type RepoInfo, tryApplyPackageName, validateRemoteTemplate, @@ -56,12 +57,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 === 'next' ? '.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( From d2b85fcfa1fff115b7d9771451dd38f4c2668ae6 Mon Sep 17 00:00:00 2001 From: RostiMelk Date: Tue, 10 Dec 2024 13:27:47 +0100 Subject: [PATCH 2/4] feat: autofix --- .../cli/src/actions/init-project/bootstrapRemoteTemplate.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/@sanity/cli/src/actions/init-project/bootstrapRemoteTemplate.ts b/packages/@sanity/cli/src/actions/init-project/bootstrapRemoteTemplate.ts index 58a8f0dee92..d9a49c357be 100644 --- a/packages/@sanity/cli/src/actions/init-project/bootstrapRemoteTemplate.ts +++ b/packages/@sanity/cli/src/actions/init-project/bootstrapRemoteTemplate.ts @@ -1,7 +1,8 @@ import {mkdir} from 'node:fs/promises' import {join} from 'node:path' -import {detectFrameworkRecord, LocalFileSystemDetector} from '@vercel/fs-detectors' + import {type Framework, frameworks} from '@vercel/frameworks' +import {detectFrameworkRecord, LocalFileSystemDetector} from '@vercel/fs-detectors' import {debug} from '../../debug' import {type CliCommandContext} from '../../types' From 3ba17c2841e2f7f1841a23037e5c255c35ffbd30 Mon Sep 17 00:00:00 2001 From: RostiMelk Date: Tue, 10 Dec 2024 14:00:45 +0100 Subject: [PATCH 3/4] feat: next - --- packages/@sanity/cli/nextjs | 0 .../cli/src/actions/init-project/bootstrapRemoteTemplate.ts | 2 +- 2 files changed, 1 insertion(+), 1 deletion(-) create mode 100644 packages/@sanity/cli/nextjs diff --git a/packages/@sanity/cli/nextjs b/packages/@sanity/cli/nextjs new file mode 100644 index 00000000000..e69de29bb2d diff --git a/packages/@sanity/cli/src/actions/init-project/bootstrapRemoteTemplate.ts b/packages/@sanity/cli/src/actions/init-project/bootstrapRemoteTemplate.ts index d9a49c357be..7eca8f489ca 100644 --- a/packages/@sanity/cli/src/actions/init-project/bootstrapRemoteTemplate.ts +++ b/packages/@sanity/cli/src/actions/init-project/bootstrapRemoteTemplate.ts @@ -66,7 +66,7 @@ export async function bootstrapRemoteTemplate( frameworkList: frameworks as readonly Framework[], }) // Next.js uses `.env.local` for local environment variables - const envName = packageFramework?.slug === 'next' ? '.env.local' : '.env' + const envName = packageFramework?.slug === 'nextjs' ? '.env.local' : '.env' await applyEnvVariables(packagePath, {...variables, readToken}, envName) } From a016d2341fd2d0b41e88d3ab4a937799e0066f02 Mon Sep 17 00:00:00 2001 From: RostiMelk Date: Tue, 10 Dec 2024 14:05:25 +0100 Subject: [PATCH 4/4] fix: remove wrongfully added file --- packages/@sanity/cli/nextjs | 0 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 packages/@sanity/cli/nextjs diff --git a/packages/@sanity/cli/nextjs b/packages/@sanity/cli/nextjs deleted file mode 100644 index e69de29bb2d..00000000000