@@ -18,7 +18,7 @@ var execSync = require('child_process').execSync;
18
18
var opn = require ( 'opn' ) ;
19
19
var detect = require ( 'detect-port' ) ;
20
20
var readline = require ( 'readline' ) ;
21
- var PORT = 3000 ;
21
+ var DEFAULT_PORT = 3000 ;
22
22
23
23
// TODO: hide this behind a flag and eliminate dead code on eject.
24
24
// This shouldn't be exposed to the user.
@@ -69,32 +69,38 @@ function clearConsole() {
69
69
}
70
70
71
71
var compiler = webpack ( config , handleCompile ) ;
72
- detect ( PORT , ( error , _port ) => {
73
-
74
- if ( PORT !== _port ) {
75
- var rl = readline . createInterface ( {
76
- input : process . stdin ,
77
- output : process . stdout
78
- } ) ;
79
-
80
- rl . question ( 'Something is already running at http://localhost:' + PORT + '. Would you like to run the app at another port instead? [Y/n] ' , ( answer ) => {
81
- if ( answer === 'Y' ) {
82
- PORT = _port ;
83
- // Replace the port in webpack config
84
- config . entry = config . entry . map ( c => c . replace ( / ( \d + ) / g, PORT ) ) ;
85
- compiler = webpack ( config , handleCompile ) ;
86
- setupCompiler ( ) ;
87
- runDevServer ( ) ;
88
- }
89
- rl . close ( ) ;
90
- } ) ;
91
- } else {
92
- runDevServer ( ) ;
93
- setupCompiler ( ) ;
94
- }
95
- } ) ;
96
72
97
- function setupCompiler ( ) {
73
+ function promptForPort ( suggestedPort ) {
74
+ return new Promise ( ( resolve , reject ) => {
75
+ if ( DEFAULT_PORT !== suggestedPort ) {
76
+ var rl = readline . createInterface ( {
77
+ input : process . stdin ,
78
+ output : process . stdout
79
+ } ) ;
80
+
81
+ var question = chalk . red ( 'Something is already running at port ' + suggestedPort ) +
82
+ '\nWould you like to run the app at another port instead? [Y/n] ' ;
83
+
84
+ rl . question ( question , answer => {
85
+ var shouldChangePort = (
86
+ answer . length === 0 ||
87
+ answer . match ( / y e s | y / i)
88
+ ) ;
89
+
90
+ if ( shouldChangePort ) {
91
+ // Replace the port in webpack config
92
+ config . entry . map ( c => c . replace ( / ( \d + ) / g, suggestedPort ) ) ;
93
+ resolve ( suggestedPort ) ;
94
+ }
95
+ rl . close ( ) ;
96
+ } ) ;
97
+ } else {
98
+ resolve ( DEFAULT_PORT ) ;
99
+ }
100
+ } ) ;
101
+ }
102
+
103
+ function setupCompiler ( port ) {
98
104
compiler . plugin ( 'invalid' , function ( ) {
99
105
clearConsole ( ) ;
100
106
console . log ( 'Compiling...' ) ;
@@ -107,7 +113,7 @@ function setupCompiler() {
107
113
if ( ! hasErrors && ! hasWarnings ) {
108
114
console . log ( chalk . green ( 'Compiled successfully!' ) ) ;
109
115
console . log ( ) ;
110
- console . log ( 'The app is running at http://localhost:' + PORT + '/' ) ;
116
+ console . log ( 'The app is running at http://localhost:' + port + '/' ) ;
111
117
console . log ( ) ;
112
118
return ;
113
119
}
@@ -173,20 +179,27 @@ function openBrowser(port) {
173
179
opn ( 'http://localhost:' + port + '/' ) ;
174
180
}
175
181
176
- function runDevServer ( ) {
182
+ function runDevServer ( port ) {
177
183
new WebpackDevServer ( compiler , {
178
184
historyApiFallback : true ,
179
185
hot : true , // Note: only CSS is currently hot reloaded
180
186
publicPath : config . output . publicPath ,
181
187
quiet : true
182
- } ) . listen ( PORT , 'localhost' , ( err , result ) => {
188
+ } ) . listen ( port , ( err , result ) => {
183
189
if ( err ) {
184
190
return console . log ( err ) ;
185
191
}
186
192
187
193
clearConsole ( ) ;
188
194
console . log ( chalk . cyan ( 'Starting the development server...' ) ) ;
189
195
console . log ( ) ;
190
- openBrowser ( PORT ) ;
196
+ openBrowser ( port ) ;
191
197
} ) ;
192
198
}
199
+
200
+ detect ( DEFAULT_PORT )
201
+ . then ( promptForPort )
202
+ . then ( port => {
203
+ setupCompiler ( port ) ;
204
+ runDevServer ( port ) ;
205
+ } ) ;
0 commit comments