Skip to content

Commit c0f391a

Browse files
committed
Refactor and code cleanup
1 parent c1deb6a commit c0f391a

File tree

1 file changed

+43
-30
lines changed

1 file changed

+43
-30
lines changed

scripts/start.js

Lines changed: 43 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ var execSync = require('child_process').execSync;
1818
var opn = require('opn');
1919
var detect = require('detect-port');
2020
var readline = require('readline');
21-
var PORT = 3000;
21+
var DEFAULT_PORT = 3000;
2222

2323
// TODO: hide this behind a flag and eliminate dead code on eject.
2424
// This shouldn't be exposed to the user.
@@ -69,32 +69,38 @@ function clearConsole() {
6969
}
7070

7171
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-
});
9672

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(/yes|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) {
98104
compiler.plugin('invalid', function() {
99105
clearConsole();
100106
console.log('Compiling...');
@@ -107,7 +113,7 @@ function setupCompiler() {
107113
if (!hasErrors && !hasWarnings) {
108114
console.log(chalk.green('Compiled successfully!'));
109115
console.log();
110-
console.log('The app is running at http://localhost:' + PORT + '/');
116+
console.log('The app is running at http://localhost:' + port + '/');
111117
console.log();
112118
return;
113119
}
@@ -173,20 +179,27 @@ function openBrowser(port) {
173179
opn('http://localhost:' + port + '/');
174180
}
175181

176-
function runDevServer() {
182+
function runDevServer(port) {
177183
new WebpackDevServer(compiler, {
178184
historyApiFallback: true,
179185
hot: true, // Note: only CSS is currently hot reloaded
180186
publicPath: config.output.publicPath,
181187
quiet: true
182-
}).listen(PORT, 'localhost', (err, result) => {
188+
}).listen(port, (err, result) => {
183189
if (err) {
184190
return console.log(err);
185191
}
186192

187193
clearConsole();
188194
console.log(chalk.cyan('Starting the development server...'));
189195
console.log();
190-
openBrowser(PORT);
196+
openBrowser(port);
191197
});
192198
}
199+
200+
detect(DEFAULT_PORT)
201+
.then(promptForPort)
202+
.then(port => {
203+
setupCompiler(port);
204+
runDevServer(port);
205+
});

0 commit comments

Comments
 (0)