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
This commit is contained in:
Joe Haddad 2019-06-10 09:02:16 -04:00 committed by GitHub
parent ffba702608
commit ac5a1c62de
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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(
dir,
/.*\.(ts|tsx)$/,
/(node_modules|.*\.d\.ts)/
)
if (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
return typescriptFiles.length > 0
}
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 toInstall: string[] = []
const yarnLockFile = path.join(dir, 'yarn.lock')
if (!(await exists(tsConfigPath))) {
if (await verifyNoTypeScript(dir)) {
return
}
await writeJson(tsConfigPath, {})
firstTimeSetup = true
}
const hasTsConfig = await exists(tsConfigPath)
const isYarn = await exists(yarnLockFile)
const hasTypeScriptFiles = await hasTypeScript(dir)
if (!(hasTsConfig || hasTypeScriptFiles)) {
return
}
let firstTimeSetup = !hasTsConfig && hasTypeScriptFiles
// Ensure TypeScript is installed
let typescriptPath = ''
let ts: typeof import('typescript')
const toInstall: string[] = []
try {
await resolveP('@types/react/index.d.ts', { basedir: dir })
} catch (_) {
@ -153,6 +142,19 @@ export async function verifyTypeScriptSetup(dir: string): Promise<void> {
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 = []
let appTsConfig
let parsedTsConfig