-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f659fe4
commit bff0cd3
Showing
6 changed files
with
241 additions
and
378 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,84 +1,14 @@ | ||
/* @flow weak */ | ||
import detect from "detect-port" | ||
import Hapi from "hapi" | ||
import opn from "opn" | ||
import rl from "readline" | ||
|
||
const rlInterface = rl.createInterface({ | ||
input: process.stdin, | ||
output: process.stdout, | ||
}) | ||
|
||
const debug = require(`debug`)(`gatsby:application`) | ||
|
||
function startServer(program, launchPort) { | ||
const directory = program.directory | ||
const serverPort = launchPort || program.port | ||
|
||
debug(`Serving /public`) | ||
const server = new Hapi.Server() | ||
|
||
server.connection({ | ||
host: program.host, | ||
port: serverPort, | ||
}) | ||
|
||
server.route({ | ||
method: `GET`, | ||
path: `/{path*}`, | ||
handler: { | ||
directory: { | ||
path: `${directory}/public`, | ||
listing: false, | ||
index: true, | ||
}, | ||
}, | ||
}) | ||
|
||
server.start(e => { | ||
if (e) { | ||
if (e.code === `EADDRINUSE`) { | ||
// eslint-disable-next-line max-len | ||
console.log( | ||
`Unable to start Gatsby on port ${serverPort} as there's already a process listing on that port.` | ||
) | ||
} else { | ||
console.log(e) | ||
} | ||
|
||
process.exit() | ||
} else { | ||
if (program.open) { | ||
opn(server.info.uri) | ||
} | ||
console.log(`Listening at:`, server.info.uri) | ||
} | ||
}) | ||
} | ||
const serve = require(`serve`) | ||
const signalExit = require(`signal-exit`) | ||
|
||
module.exports = program => { | ||
const port = | ||
typeof program.port === `string` ? parseInt(program.port, 10) : program.port | ||
|
||
detect(port, (err, _port) => { | ||
if (err) { | ||
console.error(err) | ||
process.exit() | ||
} | ||
|
||
if (port !== _port) { | ||
// eslint-disable-next-line max-len | ||
const question = `Something is already running at port ${port} \nWould you like to run the app at another port instead? [Y/n] ` | ||
|
||
return rlInterface.question(question, answer => { | ||
if (answer.length === 0 || answer.match(/^yes|y$/i)) { | ||
program.port = _port | ||
} | ||
let { port, open, directory } = program | ||
port = typeof port === `string` ? parseInt(port, 10) : port | ||
|
||
return startServer(program, program.port) | ||
}) | ||
} | ||
let server = serve(`${directory}/public`, { port, open }) | ||
|
||
return startServer(program) | ||
signalExit((code, signal) => { | ||
server.stop() | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.