forked from vercel/next.js
/
getTypeScriptIntent.ts
43 lines (38 loc) · 1.41 KB
/
getTypeScriptIntent.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import { promises as fs } from 'fs'
import path from 'path'
import { NextConfigComplete } from '../../server/config-shared'
import { fileExists } from '../file-exists'
import { recursiveReadDir } from '../recursive-readdir'
export type TypeScriptIntent = { firstTimeSetup: boolean }
export async function getTypeScriptIntent(
baseDir: string,
intentDirs: string[],
config: NextConfigComplete
): Promise<TypeScriptIntent | false> {
const tsConfigPath = path.join(baseDir, config.typescript.tsconfigPath)
// The integration turns on if we find a `tsconfig.json` in the user's
// project.
const hasTypeScriptConfiguration = await fileExists(tsConfigPath)
if (hasTypeScriptConfiguration) {
const content = await fs.readFile(tsConfigPath, { encoding: 'utf8' }).then(
(txt) => txt.trim(),
() => null
)
return { firstTimeSetup: content === '' || content === '{}' }
}
// Next.js also offers a friendly setup mode that bootstraps a TypeScript
// project for the user when we detect TypeScript files. So, we need to check
// the `pages/` directory for a TypeScript file.
// Checking all directories is too slow, so this is a happy medium.
for (const dir of intentDirs) {
const typescriptFiles = await recursiveReadDir(
dir,
/.*\.(ts|tsx)$/,
/(node_modules|.*\.d\.ts)/
)
if (typescriptFiles.length) {
return { firstTimeSetup: true }
}
}
return false
}