Skip to content

Commit

Permalink
feat(cli): use @vercel/frameworks in bootstrapRemoteTemplate (#8001)
Browse files Browse the repository at this point in the history
  • Loading branch information
RostiMelk authored Dec 10, 2024
1 parent 6b322cb commit 60537c8
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -9,7 +12,6 @@ import {
downloadAndExtractRepo,
generateSanityApiReadToken,
getMonoRepo,
isNextJsTemplate,
type RepoInfo,
tryApplyPackageName,
validateRemoteTemplate,
Expand Down Expand Up @@ -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)
Expand Down
10 changes: 0 additions & 10 deletions packages/@sanity/cli/src/util/remoteTemplate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -410,16 +410,6 @@ export async function validateRemoteTemplate(
}
}

export async function isNextJsTemplate(root: string): Promise<boolean> {
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<boolean> {
try {
const templatePath = await Promise.any(
Expand Down

0 comments on commit 60537c8

Please sign in to comment.