From 0efd6f9e6faad1b92723241654b601610e61236e Mon Sep 17 00:00:00 2001 From: Artem Samofalov Date: Mon, 30 Jan 2017 21:28:39 +0700 Subject: [PATCH 01/10] add pretty message if port already use(#927) --- bin/next-dev | 6 +++++- server/index.js | 7 +++++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/bin/next-dev b/bin/next-dev index 0df5f875292c5..3584aab3e92b9 100755 --- a/bin/next-dev +++ b/bin/next-dev @@ -61,6 +61,10 @@ srv.start(argv.port) } }) .catch((err) => { - console.error(err) + if (err.code === 'EADDRINUSE') { + console.error(`Port ${argv.port} is already in use.\nUse \`npm run dev -- -p \`.`) + } else { + console.error(err) + } process.exit(1) }) diff --git a/server/index.js b/server/index.js index 494e4422b2bd5..129ca6c778015 100644 --- a/server/index.js +++ b/server/index.js @@ -119,10 +119,13 @@ export default class Server { await this.prepare() this.http = http.createServer(this.getRequestHandler()) await new Promise((resolve, reject) => { - this.http.listen(port, (err) => { - if (err) return reject(err) + this.http.on('error', (error) => { + reject(error) + }) + this.http.on('listening', () => { resolve() }) + this.http.listen(port, () => {}) }) } From 30e77d1ea7fec53e89e367c1ca061fc75f833a56 Mon Sep 17 00:00:00 2001 From: Artem Samofalov Date: Mon, 30 Jan 2017 22:40:00 +0700 Subject: [PATCH 02/10] fix console async nature --- bin/next-dev | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/next-dev b/bin/next-dev index 3584aab3e92b9..e3d0f94f12696 100755 --- a/bin/next-dev +++ b/bin/next-dev @@ -66,5 +66,5 @@ srv.start(argv.port) } else { console.error(err) } - process.exit(1) + process.nextTick(function() { process.exit(1) }) }) From d675debe3822a294d0a80187cf886f883a10b52e Mon Sep 17 00:00:00 2001 From: Artem Samofalov Date: Mon, 30 Jan 2017 22:47:38 +0700 Subject: [PATCH 03/10] fix linter --- bin/next-dev | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/next-dev b/bin/next-dev index e3d0f94f12696..9d146075105fc 100755 --- a/bin/next-dev +++ b/bin/next-dev @@ -66,5 +66,5 @@ srv.start(argv.port) } else { console.error(err) } - process.nextTick(function() { process.exit(1) }) + process.nextTick(function () { process.exit(1) }) }) From cb9b3b3fea85c8d1bdedc810ee2de1a6acd681a7 Mon Sep 17 00:00:00 2001 From: Artem Samofalov Date: Tue, 31 Jan 2017 10:20:17 +0700 Subject: [PATCH 04/10] clean callbacks code --- bin/next-dev | 2 +- server/index.js | 8 ++------ 2 files changed, 3 insertions(+), 7 deletions(-) diff --git a/bin/next-dev b/bin/next-dev index 9d146075105fc..b6a4be0135cee 100755 --- a/bin/next-dev +++ b/bin/next-dev @@ -66,5 +66,5 @@ srv.start(argv.port) } else { console.error(err) } - process.nextTick(function () { process.exit(1) }) + process.nextTick(() => process.exit(1)) }) diff --git a/server/index.js b/server/index.js index 129ca6c778015..5718e0ef622b7 100644 --- a/server/index.js +++ b/server/index.js @@ -119,12 +119,8 @@ export default class Server { await this.prepare() this.http = http.createServer(this.getRequestHandler()) await new Promise((resolve, reject) => { - this.http.on('error', (error) => { - reject(error) - }) - this.http.on('listening', () => { - resolve() - }) + this.http.on('error', reject) + this.http.on('listening', () => resolve()) this.http.listen(port, () => {}) }) } From 9f6ccce0ba53ce77447c0e0b11f8513902d30272 Mon Sep 17 00:00:00 2001 From: Artem Samofalov Date: Tue, 31 Jan 2017 12:59:46 +0700 Subject: [PATCH 05/10] Check package.json for the startup script --- bin/next-dev | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/bin/next-dev b/bin/next-dev index b6a4be0135cee..8cc5077e0a069 100755 --- a/bin/next-dev +++ b/bin/next-dev @@ -2,7 +2,7 @@ import 'source-map-support/register' import { resolve, join } from 'path' import parseArgs from 'minimist' -import { existsSync } from 'fs' +import { existsSync, readFileSync } from 'fs' import Server from '../server' import { printAndExit } from '../lib/utils' @@ -62,7 +62,16 @@ srv.start(argv.port) }) .catch((err) => { if (err.code === 'EADDRINUSE') { - console.error(`Port ${argv.port} is already in use.\nUse \`npm run dev -- -p \`.`) + let errorMessage = `Port ${argv.port} is already in use.` + if (existsSync(join(dir, '..', 'package.json'))) { + try { + const appPackage = JSON.parse(readFileSync(join(dir, 'package.json'), 'utf8')) + const nextTuple = Object.entries(appPackage.scripts).find(i => i[1] === 'next') + errorMessage += `\nUse \`npm run ${nextTuple[0]} -- -p \`.` + } catch (e) { } + } + + console.error(errorMessage) } else { console.error(err) } From f55b901c6f19eb2d2a2bf602b9f616918e3ec8e6 Mon Sep 17 00:00:00 2001 From: Artem Samofalov Date: Tue, 31 Jan 2017 13:06:48 +0700 Subject: [PATCH 06/10] fix path to package --- bin/next-dev | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/next-dev b/bin/next-dev index 8cc5077e0a069..d2a44a80d07e4 100755 --- a/bin/next-dev +++ b/bin/next-dev @@ -63,7 +63,7 @@ srv.start(argv.port) .catch((err) => { if (err.code === 'EADDRINUSE') { let errorMessage = `Port ${argv.port} is already in use.` - if (existsSync(join(dir, '..', 'package.json'))) { + if (existsSync(join(dir, 'package.json'))) { try { const appPackage = JSON.parse(readFileSync(join(dir, 'package.json'), 'utf8')) const nextTuple = Object.entries(appPackage.scripts).find(i => i[1] === 'next') From 8de2b518fa79e8c0512f83fc9039eff37277af80 Mon Sep 17 00:00:00 2001 From: Artem Samofalov Date: Tue, 31 Jan 2017 14:25:01 +0700 Subject: [PATCH 07/10] omit callback --- server/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/index.js b/server/index.js index 5718e0ef622b7..4dea7fe9ff207 100644 --- a/server/index.js +++ b/server/index.js @@ -121,7 +121,7 @@ export default class Server { await new Promise((resolve, reject) => { this.http.on('error', reject) this.http.on('listening', () => resolve()) - this.http.listen(port, () => {}) + this.http.listen(port) }) } From 9404483c9b6ed06c408d2c7bdbd63ed9514fe346 Mon Sep 17 00:00:00 2001 From: Artem Samofalov Date: Tue, 31 Jan 2017 17:32:03 +0700 Subject: [PATCH 08/10] remove extra check, code execute in try block --- bin/next-dev | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/bin/next-dev b/bin/next-dev index d2a44a80d07e4..f46f6c97b5216 100755 --- a/bin/next-dev +++ b/bin/next-dev @@ -63,14 +63,11 @@ srv.start(argv.port) .catch((err) => { if (err.code === 'EADDRINUSE') { let errorMessage = `Port ${argv.port} is already in use.` - if (existsSync(join(dir, 'package.json'))) { - try { - const appPackage = JSON.parse(readFileSync(join(dir, 'package.json'), 'utf8')) - const nextTuple = Object.entries(appPackage.scripts).find(i => i[1] === 'next') - errorMessage += `\nUse \`npm run ${nextTuple[0]} -- -p \`.` - } catch (e) { } - } - + try { + const appPackage = JSON.parse(readFileSync(join(dir, 'package.json'), 'utf8')) + const nextTuple = Object.entries(appPackage.scripts).find(i => i[1] === 'next') + errorMessage += `\nUse \`npm run ${nextTuple[0]} -- -p \`.` + } catch (e) { } console.error(errorMessage) } else { console.error(err) From 0278e8b0fd8b7e72040dfd039d0f6ce651d3ef84 Mon Sep 17 00:00:00 2001 From: Artem Samofalov Date: Tue, 31 Jan 2017 18:43:23 +0700 Subject: [PATCH 09/10] + reason for change start listen port of node http --- server/index.js | 1 + 1 file changed, 1 insertion(+) diff --git a/server/index.js b/server/index.js index 4dea7fe9ff207..f08504ef6f883 100644 --- a/server/index.js +++ b/server/index.js @@ -119,6 +119,7 @@ export default class Server { await this.prepare() this.http = http.createServer(this.getRequestHandler()) await new Promise((resolve, reject) => { + // This code catches EADDRINUSE error if the port is already in use this.http.on('error', reject) this.http.on('listening', () => resolve()) this.http.listen(port) From 6d7945c5304cb7b9422d5a2b87ee4594275c24ac Mon Sep 17 00:00:00 2001 From: Artem Samofalov Date: Tue, 31 Jan 2017 18:53:43 +0700 Subject: [PATCH 10/10] remove extra code for search package --- bin/next-dev | 10 +++++----- package.json | 1 + 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/bin/next-dev b/bin/next-dev index f46f6c97b5216..d5055a3cf2004 100755 --- a/bin/next-dev +++ b/bin/next-dev @@ -5,6 +5,7 @@ import parseArgs from 'minimist' import { existsSync, readFileSync } from 'fs' import Server from '../server' import { printAndExit } from '../lib/utils' +import pkgUp from 'pkg-up' process.env.NODE_ENV = process.env.NODE_ENV || 'development' @@ -63,11 +64,10 @@ srv.start(argv.port) .catch((err) => { if (err.code === 'EADDRINUSE') { let errorMessage = `Port ${argv.port} is already in use.` - try { - const appPackage = JSON.parse(readFileSync(join(dir, 'package.json'), 'utf8')) - const nextTuple = Object.entries(appPackage.scripts).find(i => i[1] === 'next') - errorMessage += `\nUse \`npm run ${nextTuple[0]} -- -p \`.` - } catch (e) { } + const pkgAppPath = pkgUp.sync('.') + const appPackage = JSON.parse(readFileSync(pkgAppPath, 'utf8')) + const nextScript = Object.entries(appPackage.scripts).find(scriptLine => scriptLine[1] === 'next') + if (nextScript) errorMessage += `\nUse \`npm run ${nextScript[0]} -- -p \`.` console.error(errorMessage) } else { console.error(err) diff --git a/package.json b/package.json index dcf6adcc41cd9..2f69422514688 100644 --- a/package.json +++ b/package.json @@ -70,6 +70,7 @@ "mkdirp-then": "1.2.0", "mz": "2.6.0", "path-match": "1.2.4", + "pkg-up": "1.0.0", "react": "15.4.2", "react-dom": "15.4.2", "react-hot-loader": "3.0.0-beta.6",