@@ -15,16 +15,33 @@ const path = require('path');
15
15
const paths = require ( '../../config/paths' ) ;
16
16
const os = require ( 'os' ) ;
17
17
const immer = require ( 'react-dev-utils/immer' ) . produce ;
18
+ const globby = require ( 'react-dev-utils/globby' ) . sync ;
18
19
19
20
function writeJson ( fileName , object ) {
20
21
fs . writeFileSync ( fileName , JSON . stringify ( object , null , 2 ) + os . EOL ) ;
21
22
}
22
23
24
+ function verifyNoTypeScript ( ) {
25
+ const typescriptFiles = globby ( '**/*.(ts|tsx)' , { cwd : paths . appSrc } ) ;
26
+ if ( typescriptFiles . length > 0 ) {
27
+ console . warn (
28
+ chalk . yellow (
29
+ `We detected TypeScript in your project (${ chalk . bold (
30
+ `src${ path . sep } ${ typescriptFiles [ 0 ] } `
31
+ ) } ) and created a ${ chalk . bold ( 'tsconfig.json' ) } file for you.`
32
+ )
33
+ ) ;
34
+ console . warn ( ) ;
35
+ return false ;
36
+ }
37
+ return true ;
38
+ }
39
+
23
40
function verifyTypeScriptSetup ( ) {
24
41
let firstTimeSetup = false ;
25
42
26
43
if ( ! fs . existsSync ( paths . appTsConfig ) ) {
27
- if ( ! paths . appIndexJs . match ( / \. t s x ? $ / ) ) {
44
+ if ( verifyNoTypeScript ( ) ) {
28
45
return ;
29
46
}
30
47
writeJson ( paths . appTsConfig , { } ) ;
@@ -41,14 +58,12 @@ function verifyTypeScriptSetup() {
41
58
} ) ) ;
42
59
} catch ( _ ) {
43
60
console . error (
44
- chalk . red (
45
- 'We detected a' ,
46
- chalk . bold ( 'tsconfig.json' ) ,
47
- "in your package root but couldn't find an installation of" ,
48
- chalk . bold ( 'typescript' ) + '.'
61
+ chalk . bold . red (
62
+ `It looks like you're trying to use TypeScript but do not have ${ chalk . bold (
63
+ 'typescript'
64
+ ) } installed.`
49
65
)
50
66
) ;
51
- console . error ( ) ;
52
67
console . error (
53
68
chalk . bold (
54
69
'Please install' ,
@@ -60,9 +75,11 @@ function verifyTypeScriptSetup() {
60
75
)
61
76
) ;
62
77
console . error (
63
- 'If you are not trying to use TypeScript, please remove the ' +
64
- chalk . cyan ( 'tsconfig.json' ) +
65
- ' file from your package root.'
78
+ chalk . bold (
79
+ 'If you are not trying to use TypeScript, please remove the ' +
80
+ chalk . cyan ( 'tsconfig.json' ) +
81
+ ' file from your package root (and any TypeScript files).'
82
+ )
66
83
) ;
67
84
console . error ( ) ;
68
85
process . exit ( 1 ) ;
0 commit comments