Commit 1cf80482 authored by JJ Kasper's avatar JJ Kasper Committed by Tim Neutkens

Show helpful warning when @types/react isn't found (#7345)

* Show helpful warning when @types/react isn't found

* Apply suggestions from code review
Co-Authored-By: 's avatarResi Respati <resir014@gmail.com>

* Update warning for yarn

* Check earlier so we can log one install command for TypeScript deps
parent 9b27e56c
...@@ -36,7 +36,7 @@ async function verifyNoTypeScript(dir: string) { ...@@ -36,7 +36,7 @@ async function verifyNoTypeScript(dir: string) {
console.warn( console.warn(
chalk.yellow( chalk.yellow(
`We detected TypeScript in your project (${chalk.bold( `We detected TypeScript in your project (${chalk.bold(
`${typescriptFiles[0]}`, `.${typescriptFiles[0]}`,
)}) and created a ${chalk.bold('tsconfig.json')} file for you.`, )}) and created a ${chalk.bold('tsconfig.json')} file for you.`,
), ),
) )
...@@ -50,6 +50,7 @@ export async function verifyTypeScriptSetup(dir: string): Promise<void> { ...@@ -50,6 +50,7 @@ export async function verifyTypeScriptSetup(dir: string): Promise<void> {
let firstTimeSetup = false let firstTimeSetup = false
const yarnLockFile = path.join(dir, 'yarn.lock') const yarnLockFile = path.join(dir, 'yarn.lock')
const tsConfigPath = path.join(dir, 'tsconfig.json') const tsConfigPath = path.join(dir, 'tsconfig.json')
const toInstall: string[] = []
if (!(await exists(tsConfigPath))) { if (!(await exists(tsConfigPath))) {
if (await verifyNoTypeScript(dir)) { if (await verifyNoTypeScript(dir)) {
...@@ -63,24 +64,33 @@ export async function verifyTypeScriptSetup(dir: string): Promise<void> { ...@@ -63,24 +64,33 @@ export async function verifyTypeScriptSetup(dir: string): Promise<void> {
// Ensure TypeScript is installed // Ensure TypeScript is installed
let typescriptPath = '' let typescriptPath = ''
let ts: typeof import('typescript') let ts: typeof import('typescript')
try {
await resolveP('@types/react/index.d.ts', { basedir: dir })
} catch (_) {
toInstall.push('@types/react')
}
try { try {
typescriptPath = await resolveP('typescript', { basedir: dir }) typescriptPath = await resolveP('typescript', { basedir: dir })
ts = require(typescriptPath) ts = require(typescriptPath)
} catch (_) { } catch (_) {
toInstall.push('typescript')
const toInstallStr = toInstall.join(' ')
console.error( console.error(
chalk.bold.red( chalk.bold.red(
`It looks like you're trying to use TypeScript but do not have ${chalk.bold( `It looks like you're trying to use TypeScript but do not have ${chalk.bold(
'typescript', toInstallStr,
)} installed.`, )} installed.`,
), ),
) )
console.error( console.error(
chalk.bold( chalk.bold(
'Please install', 'Please install',
chalk.cyan.bold('typescript'), chalk.cyan.bold(toInstallStr),
'by running', 'by running',
chalk.cyan.bold( chalk.cyan.bold(
isYarn ? 'yarn add typescript' : 'npm install typescript', (isYarn ? 'yarn add --dev' : 'npm install --save-dev') + ' ' + toInstallStr,
) + '.', ) + '.',
), ),
) )
...@@ -255,5 +265,8 @@ export async function verifyTypeScriptSetup(dir: string): Promise<void> { ...@@ -255,5 +265,8 @@ export async function verifyTypeScriptSetup(dir: string): Promise<void> {
} }
await writeJson(tsConfigPath, appTsConfig) await writeJson(tsConfigPath, appTsConfig)
} }
return
if (toInstall.length > 0) {
console.warn(chalk.red(`\n${toInstall.join(' ')} ${toInstall.length === 1 ? 'is' : 'are'} needed when using TypeScript with Next.js. Please install ${toInstall.length === 1 ? 'it' : 'them'} with ${isYarn ? 'yarn add --dev' : 'npm install --save-dev'} ${toInstall.join(' ')}\n`))
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment