Commit 0b15540a authored by Luis Fernando Alvarez D's avatar Luis Fernando Alvarez D Committed by Joe Haddad

Don't look up for TS files in dev (#8374)

* Only read for typescript files if there's no tsconfig

* Update verifyTypeScriptSetup.ts

* Remove the recursive check for TS files

* Remove unused import

* Updated tests

* Add a check for the content of tsConfig

* Clean up

* utf8
parent cffa2e3f
......@@ -3,11 +3,11 @@ import os from 'os'
import path from 'path'
import chalk from 'chalk'
import { promisify } from 'util'
import { recursiveReadDir } from './recursive-readdir'
import { fileExists } from './file-exists'
import resolve from 'next/dist/compiled/resolve/index.js'
const writeFile = promisify(fs.writeFile)
const readFile = promisify(fs.readFile)
function writeJson(fileName: string, object: object): Promise<void> {
return writeFile(
......@@ -16,16 +16,6 @@ function writeJson(fileName: string, object: object): Promise<void> {
)
}
async function hasTypeScript(dir: string): Promise<boolean> {
const typescriptFiles = await recursiveReadDir(
dir,
/.*\.(ts|tsx)$/,
/(node_modules|.*\.d\.ts)/
)
return typescriptFiles.length > 0
}
async function checkDependencies({
dir,
isYarn,
......@@ -98,13 +88,15 @@ export async function verifyTypeScriptSetup(dir: string): Promise<void> {
const hasTsConfig = await fileExists(tsConfigPath)
const isYarn = await fileExists(yarnLockFile)
const hasTypeScriptFiles = await hasTypeScript(dir)
let firstTimeSetup = !hasTsConfig && hasTypeScriptFiles
if (!(hasTsConfig || hasTypeScriptFiles)) {
if (!hasTsConfig) {
return
}
const tsConfig = await readFile(tsConfigPath, 'utf8').then(val => val.trim())
let firstTimeSetup = tsConfig === '' || tsConfig === '{}'
await checkDependencies({ dir, isYarn })
const ts = await import('typescript')
......
......@@ -30,28 +30,17 @@ describe('Fork ts checker plugin', () => {
await remove(tsConfig)
})
it('Creates a default tsconfig.json when one is missing', async () => {
expect(await exists(tsConfig)).toBe(true)
const tsConfigContent = await readFile(tsConfig)
let parsedTsConfig
expect(() => {
parsedTsConfig = JSON.parse(tsConfigContent)
}).not.toThrow()
expect(parsedTsConfig.exclude[0]).toBe('node_modules')
})
it('Works with an empty tsconfig.json (docs)', async () => {
await killApp(app)
await remove(tsConfig)
await new Promise(resolve => setTimeout(resolve, 500))
expect(await exists(tsConfig)).toBe(false)
await createFile(tsConfig)
await new Promise(resolve => setTimeout(resolve, 500))
expect(await readFile(tsConfig, 'utf8')).toBe('')
await killApp(app)
await new Promise(resolve => setTimeout(resolve, 500))
appPort = await findPort()
app = await launchApp(appDir, appPort)
......
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