Unverified Commit ac5a1c62 authored by Joe Haddad's avatar Joe Haddad Committed by GitHub

Don't confuse the user with an eagerly created `tsconfig.json` (#7546)

* Don't confuse the user with an eagerly created `tsconfig.json`

* Fix errors

* Extra log

* Bail when doing nothing
parent ffba7026
...@@ -25,46 +25,35 @@ function writeJson(fileName: string, object: object): Promise<void> { ...@@ -25,46 +25,35 @@ function writeJson(fileName: string, object: object): Promise<void> {
) )
} }
async function verifyNoTypeScript(dir: string) { async function hasTypeScript(dir: string): Promise<boolean> {
const typescriptFiles = await recursiveReadDir( const typescriptFiles = await recursiveReadDir(
dir, dir,
/.*\.(ts|tsx)$/, /.*\.(ts|tsx)$/,
/(node_modules|.*\.d\.ts)/ /(node_modules|.*\.d\.ts)/
) )
if (typescriptFiles.length > 0) { return typescriptFiles.length > 0
console.warn(
chalk.yellow(
`We detected TypeScript in your project (${chalk.bold(
`.${typescriptFiles[0]}`
)}) and created a ${chalk.bold('tsconfig.json')} file for you.`
)
)
console.warn()
return false
}
return true
} }
export async function verifyTypeScriptSetup(dir: string): Promise<void> { export async function verifyTypeScriptSetup(dir: string): Promise<void> {
let firstTimeSetup = false
const yarnLockFile = path.join(dir, 'yarn.lock')
const tsConfigPath = path.join(dir, 'tsconfig.json') const tsConfigPath = path.join(dir, 'tsconfig.json')
const toInstall: string[] = [] const yarnLockFile = path.join(dir, 'yarn.lock')
if (!(await exists(tsConfigPath))) { const hasTsConfig = await exists(tsConfigPath)
if (await verifyNoTypeScript(dir)) {
return
}
await writeJson(tsConfigPath, {})
firstTimeSetup = true
}
const isYarn = await exists(yarnLockFile) const isYarn = await exists(yarnLockFile)
const hasTypeScriptFiles = await hasTypeScript(dir)
if (!(hasTsConfig || hasTypeScriptFiles)) {
return
}
let firstTimeSetup = !hasTsConfig && hasTypeScriptFiles
// Ensure TypeScript is installed // Ensure TypeScript is installed
let typescriptPath = '' let typescriptPath = ''
let ts: typeof import('typescript') let ts: typeof import('typescript')
const toInstall: string[] = []
try { try {
await resolveP('@types/react/index.d.ts', { basedir: dir }) await resolveP('@types/react/index.d.ts', { basedir: dir })
} catch (_) { } catch (_) {
...@@ -153,6 +142,19 @@ export async function verifyTypeScriptSetup(dir: string): Promise<void> { ...@@ -153,6 +142,19 @@ export async function verifyTypeScriptSetup(dir: string): Promise<void> {
getNewLine: () => os.EOL, getNewLine: () => os.EOL,
} }
if (firstTimeSetup) {
console.log(
chalk.yellow(
`We detected TypeScript in your project and created a ${chalk.bold(
'tsconfig.json'
)} file for you.`
)
)
console.log()
await writeJson(tsConfigPath, {})
}
const messages = [] const messages = []
let appTsConfig let appTsConfig
let parsedTsConfig let parsedTsConfig
......
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