Skip to content

Commit

Permalink
feat(cli): add warning and docs for react-19 and Next.Js combination
Browse files Browse the repository at this point in the history
  • Loading branch information
pedrobonamin committed Oct 23, 2024
1 parent f352db8 commit 4e2206f
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
22 changes: 22 additions & 0 deletions packages/@sanity/cli/src/actions/init-project/initProject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import execa, {type CommonOptions} from 'execa'
import {deburr, noop} from 'lodash'
import pFilter from 'p-filter'
import resolveFrom from 'resolve-from'
import semver from 'semver'
import {evaluate, patch} from 'silver-fleece'
import which from 'which'

Expand Down Expand Up @@ -55,6 +56,7 @@ import {
promptForNextTemplate,
promptForStudioPath,
} from './prompts/nextjs'
import {readPackageJson} from './readPackageJson'
import {reconfigureV2Project} from './reconfigureV2Project'
import templates from './templates'
import {
Expand Down Expand Up @@ -128,6 +130,8 @@ export default async function initSanity(
const cliFlags = args.extOptions
const unattended = cliFlags.y || cliFlags.yes
const print = unattended ? noop : output.print
const warn = (msg: string) => output.warn(chalk.yellow.bgBlack(msg))

const intendedPlan = cliFlags['project-plan']
const intendedCoupon = cliFlags.coupon
const reconfigure = cliFlags.reconfigure
Expand Down Expand Up @@ -327,6 +331,24 @@ export default async function initSanity(
// Ensure we are using the output path provided by user
outputPath = answers.outputPath

const packageJson = readPackageJson(`${outputPath}/package.json`)
const reactVersion = packageJson.dependencies?.react
const isUsingReact19 = semver.coerce(reactVersion)?.major === 19
if (
detectedFramework?.slug === 'nextjs' &&
// @ts-expect-error - Detected version is not typed into Framework interface
detectedFramework?.detectedVersion?.startsWith('15') &&
isUsingReact19
) {
warn('╭────────────────────────────────────────────────────────────╮')
warn('│ │')
warn('│ It looks like you are using Next.js 15 and React 19 │')
warn('│ Please read our compatibility guide. │')
warn('│ https://www.sanity.io/help/react-19 │')
warn('│ │')
warn('╰────────────────────────────────────────────────────────────╯')
}

if (initNext) {
const useTypeScript = unattended ? true : await promptForTypeScript(prompt)
trace.log({step: 'useTypeScript', selectedOption: useTypeScript ? 'yes' : 'no'})
Expand Down
18 changes: 18 additions & 0 deletions packages/@sanity/cli/src/actions/init-project/readPackageJson.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import fs from 'node:fs'

import {type PackageJson} from '@sanity/cli'

/**
* Read the `package.json` file at the given path
*
* @param filePath - Path to package.json to read
* @returns The parsed package.json
*/
export function readPackageJson(filePath: string): PackageJson {
try {
// eslint-disable-next-line no-sync
return JSON.parse(fs.readFileSync(filePath, 'utf8'))
} catch (err) {
throw new Error(`Failed to read "${filePath}": ${err.message}`)
}
}

0 comments on commit 4e2206f

Please sign in to comment.