@@ -15,7 +15,26 @@ const fs = require('fs');
15
15
16
16
const defaultBrowsers = [ '>0.25%' , 'not op_mini all' , 'ie 11' ] ;
17
17
18
- function checkBrowsers ( dir , retry = true ) {
18
+ function shouldSetBrowsers ( isInteractive ) {
19
+ if ( ! isInteractive ) {
20
+ return Promise . resolve ( true ) ;
21
+ }
22
+
23
+ const question = {
24
+ type : 'confirm' ,
25
+ name : 'shouldSetBrowsers' ,
26
+ message :
27
+ chalk . yellow ( "We're unable to detect target browsers." ) +
28
+ `\n\nWould you like to add the defaults to your ${ chalk . bold (
29
+ 'package.json'
30
+ ) } ?`,
31
+ default : true ,
32
+ } ;
33
+
34
+ return inquirer . prompt ( question ) . then ( answer => answer . shouldSetBrowsers ) ;
35
+ }
36
+
37
+ function checkBrowsers ( dir , isInteractive , retry = true ) {
19
38
const current = browserslist . findConfig ( dir ) ;
20
39
if ( current != null ) {
21
40
return Promise . resolve ( current ) ;
@@ -35,44 +54,34 @@ function checkBrowsers(dir, retry = true) {
35
54
) ;
36
55
}
37
56
38
- const question = {
39
- type : 'confirm' ,
40
- name : 'shouldSetBrowsers' ,
41
- message :
42
- chalk . yellow ( "We're unable to detect target browsers." ) +
43
- `\n\nWould you like to add the defaults to your ${ chalk . bold (
44
- 'package.json'
45
- ) } ?`,
46
- default : true ,
47
- } ;
48
- return inquirer . prompt ( question ) . then ( answer => {
49
- if ( answer . shouldSetBrowsers ) {
50
- return (
51
- pkgUp ( dir )
52
- . then ( filePath => {
53
- if ( filePath == null ) {
54
- return Promise . reject ( ) ;
55
- }
56
- const pkg = JSON . parse ( fs . readFileSync ( filePath ) ) ;
57
- pkg [ 'browserslist' ] = defaultBrowsers ;
58
- fs . writeFileSync ( filePath , JSON . stringify ( pkg , null , 2 ) + os . EOL ) ;
59
-
60
- browserslist . clearCaches ( ) ;
61
- console . log ( ) ;
62
- console . log (
63
- `${ chalk . green ( 'Set target browsers:' ) } ${ chalk . cyan (
64
- defaultBrowsers . join ( ', ' )
65
- ) } `
66
- ) ;
67
- console . log ( ) ;
68
- } )
69
- // Swallow any error
70
- . catch ( ( ) => { } )
71
- . then ( ( ) => checkBrowsers ( dir , false ) )
72
- ) ;
73
- } else {
74
- return checkBrowsers ( dir , false ) ;
57
+ return shouldSetBrowsers ( isInteractive ) . then ( shouldSetBrowsers => {
58
+ if ( ! shouldSetBrowsers ) {
59
+ return checkBrowsers ( dir , isInteractive , false ) ;
75
60
}
61
+
62
+ return (
63
+ pkgUp ( dir )
64
+ . then ( filePath => {
65
+ if ( filePath == null ) {
66
+ return Promise . reject ( ) ;
67
+ }
68
+ const pkg = JSON . parse ( fs . readFileSync ( filePath ) ) ;
69
+ pkg [ 'browserslist' ] = defaultBrowsers ;
70
+ fs . writeFileSync ( filePath , JSON . stringify ( pkg , null , 2 ) + os . EOL ) ;
71
+
72
+ browserslist . clearCaches ( ) ;
73
+ console . log ( ) ;
74
+ console . log (
75
+ `${ chalk . green ( 'Set target browsers:' ) } ${ chalk . cyan (
76
+ defaultBrowsers . join ( ', ' )
77
+ ) } `
78
+ ) ;
79
+ console . log ( ) ;
80
+ } )
81
+ // Swallow any error
82
+ . catch ( ( ) => { } )
83
+ . then ( ( ) => checkBrowsers ( dir , isInteractive , false ) )
84
+ ) ;
76
85
} ) ;
77
86
}
78
87
0 commit comments