Skip to content

Commit

Permalink
Use detect-port to check if desired is already used.
Browse files Browse the repository at this point in the history
Based on create-react-app and specifically
facebook/create-react-app#101

In production, will exit(1) if the port is in use.
  • Loading branch information
STRML committed Oct 26, 2016
1 parent 3d44b58 commit c0a443e
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 7 deletions.
4 changes: 2 additions & 2 deletions bin/next-dev
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import parseArgs from 'minimist'
import { exists } from 'mz/fs'
import Server from '../server'
import clean from '../server/build/clean'
import run from './util/run';

const argv = parseArgs(process.argv.slice(2), {
alias: {
Expand All @@ -21,8 +22,7 @@ const dir = resolve(argv._[0] || '.')
clean(dir)
.then(async () => {
const srv = new Server({ dir, dev: true, hotReload: true })
await srv.start(argv.port)
console.log('> Ready on http://localhost:%d', argv.port)
await run({ srv, port: argv.port })

// Check if pages dir exists and warn if not
if (!(await exists(join(dir, 'pages')))) {
Expand Down
7 changes: 3 additions & 4 deletions bin/next-start
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import { resolve } from 'path'
import parseArgs from 'minimist'
import Server from '../server'
import run from './util/run';

const argv = parseArgs(process.argv.slice(2), {
alias: {
Expand All @@ -18,10 +19,8 @@ const argv = parseArgs(process.argv.slice(2), {
const dir = resolve(argv._[0] || '.')

const srv = new Server({ dir })
srv.start(argv.port)
.then(() => {
console.log('> Ready on http://localhost:%d', argv.port)
})

run({ srv, port: argv.port, shouldPrompt: false })
.catch((err) => {
console.error(err)
process.exit(1)
Expand Down
15 changes: 15 additions & 0 deletions bin/util/prompt.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import rl from 'readline';

export default function (question) {
const rlInterface = rl.createInterface({
input: process.stdin,
output: process.stdout,
});

return new Promise((resolve) => {
rlInterface.question(question + '\n', function(answer) {
rlInterface.close();
resolve(answer);
});
});
};
31 changes: 31 additions & 0 deletions bin/util/run.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import prompt from './prompt';
import detect from 'detect-port';
import chalk from 'chalk';

const defaults = { shouldPrompt: true };
export default async function run(opts) {
const { srv, port: desiredPort, shouldPrompt } = { ...defaults, ...opts };
const port = await detect(desiredPort);
if (port !== desiredPort) {
if (!shouldPrompt) {
// Fail early if no prompt
console.error(`Error: Something is already running at port ${desiredPort}. Exiting.`);
process.exit(1);
}

// Prompt the user to change the port.
let shouldChangePort = false;
if (shouldPrompt) {
const question = chalk.red(`Something is already running at port ${desiredPort}.\n` +
`Would you like to run the app on port ${port} instead? [Y/n]`);
const answer = await prompt(question);
shouldChangePort = !answer.length || answer.match(/^yes|y$/i);
}
if (!shouldChangePort) {
console.log(chalk.red('Exiting.'));
process.exit(0);
}
}
await srv.start(port);
console.log(`Ready on ${chalk.cyan(`http://localhost:${port}`)}`);
}
2 changes: 1 addition & 1 deletion gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ gulp.task('compile', [
])

gulp.task('compile-bin', () => {
return gulp.src('bin/*')
return gulp.src(['bin/*', 'bin/**/*.js'])
.pipe(cache('bin'))
.pipe(babel(babelOptions))
.pipe(gulp.dest('dist/bin'))
Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,10 @@
"babel-preset-es2015": "6.16.0",
"babel-preset-react": "6.16.0",
"babel-runtime": "6.11.6",
"chalk": "^1.1.3",
"cross-spawn": "4.0.2",
"del": "2.2.2",
"detect-port": "^1.0.1",
"glamor": "2.17.10",
"glob-promise": "1.0.6",
"htmlescape": "1.1.1",
Expand All @@ -59,6 +61,7 @@
"react": "15.3.2",
"react-dom": "15.3.2",
"react-hot-loader": "3.0.0-beta.6",
"readline": "^1.3.0",
"send": "0.14.1",
"strip-ansi": "3.0.1",
"url": "0.11.0",
Expand Down

0 comments on commit c0a443e

Please sign in to comment.