refactor to avoid node v6 issue where the process ends before process.stdout is flushed
This commit is contained in:
@ -84,7 +84,7 @@ For the examples below, we assume we're in a project directory looking like this
|
|||||||
└── package.json
|
└── package.json
|
||||||
```
|
```
|
||||||
|
|
||||||
All paths/routes are specified using [express syntax](http://expressjs.com/guide/routing.html#route-paths). To run the example projects linked below, clone the project, move into the example directory specified, run `ws`.
|
**All paths/routes are specified using [express syntax](http://expressjs.com/guide/routing.html#route-paths)**. To run the example projects linked below, clone the project, move into the example directory specified, run `ws`.
|
||||||
|
|
||||||
### Static site
|
### Static site
|
||||||
|
|
||||||
|
36
bin/cli.js
36
bin/cli.js
@ -14,12 +14,26 @@ const flatten = require('reduce-flatten')
|
|||||||
const cli = commandLineArgs(cliOptions.definitions)
|
const cli = commandLineArgs(cliOptions.definitions)
|
||||||
const usage = cli.getUsage(cliOptions.usageData)
|
const usage = cli.getUsage(cliOptions.usageData)
|
||||||
const stored = loadConfig('local-web-server')
|
const stored = loadConfig('local-web-server')
|
||||||
const options = collectOptions()
|
let options
|
||||||
|
let isHttps = false
|
||||||
|
|
||||||
if (options.misc.help) stop(usage, 0)
|
try {
|
||||||
if (options.misc.config) stop(JSON.stringify(options.server, null, ' '), 0)
|
options = collectOptions()
|
||||||
|
} catch (err) {
|
||||||
|
stop([ `[red]{Error}: ${err.message}`, usage ], 1)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
validateOptions(options)
|
if (options.misc.help) {
|
||||||
|
stop(usage, 0)
|
||||||
|
} else if (options.misc.config) {
|
||||||
|
stop(JSON.stringify(options.server, null, ' '), 0)
|
||||||
|
} else {
|
||||||
|
const valid = validateOptions(options)
|
||||||
|
if (!valid) {
|
||||||
|
/* gracefully end the process */
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
const app = localWebServer({
|
const app = localWebServer({
|
||||||
static: {
|
static: {
|
||||||
@ -53,7 +67,6 @@ if (options.server.https) {
|
|||||||
options.server.cert = path.resolve(__dirname, '..', 'ssl', '127.0.0.1.crt')
|
options.server.cert = path.resolve(__dirname, '..', 'ssl', '127.0.0.1.crt')
|
||||||
}
|
}
|
||||||
|
|
||||||
let isHttps = false
|
|
||||||
if (options.server.key && options.server.cert) {
|
if (options.server.key && options.server.cert) {
|
||||||
const https = require('https')
|
const https = require('https')
|
||||||
const fs = require('fs')
|
const fs = require('fs')
|
||||||
@ -69,10 +82,11 @@ if (options.server.key && options.server.cert) {
|
|||||||
} else {
|
} else {
|
||||||
app.listen(options.server.port, onServerUp)
|
app.listen(options.server.port, onServerUp)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function stop (msgs, exitCode) {
|
function stop (msgs, exitCode) {
|
||||||
arrayify(msgs).forEach(msg => console.error(ansi.format(msg)))
|
arrayify(msgs).forEach(msg => console.error(ansi.format(msg)))
|
||||||
process.exit(exitCode)
|
process.exitCode = exitCode
|
||||||
}
|
}
|
||||||
|
|
||||||
function onServerUp () {
|
function onServerUp () {
|
||||||
@ -96,11 +110,7 @@ function collectOptions () {
|
|||||||
let options = {}
|
let options = {}
|
||||||
|
|
||||||
/* parse command line args */
|
/* parse command line args */
|
||||||
try {
|
|
||||||
options = cli.parse()
|
options = cli.parse()
|
||||||
} catch (err) {
|
|
||||||
stop([ `[red]{Error}: ${err.message}`, usage ], 1)
|
|
||||||
}
|
|
||||||
|
|
||||||
const builtIn = {
|
const builtIn = {
|
||||||
port: 8000,
|
port: 8000,
|
||||||
@ -129,10 +139,14 @@ function parseRewriteRules (rules) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function validateOptions (options) {
|
function validateOptions (options) {
|
||||||
|
let valid = true
|
||||||
function invalid (msg) {
|
function invalid (msg) {
|
||||||
return `[red underline]{Invalid:} [bold]{${msg}}`
|
return `[red underline]{Invalid:} [bold]{${msg}}`
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!t.isNumber(options.server.port)) {
|
if (!t.isNumber(options.server.port)) {
|
||||||
stop([ invalid(`--port must be numeric [value=${options.server.port}]`), usage ], 1)
|
stop([ invalid(`--port must be numeric`), usage ], 1)
|
||||||
|
valid = false
|
||||||
}
|
}
|
||||||
|
return valid
|
||||||
}
|
}
|
||||||
|
@ -2,7 +2,7 @@ module.exports = {
|
|||||||
response: function (ctx) {
|
response: function (ctx) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
ctx.body = `<h1>You waited 2s for this</h1>`
|
ctx.body = '<h1>You waited 2s for this</h1>'
|
||||||
resolve()
|
resolve()
|
||||||
}, 2000)
|
}, 2000)
|
||||||
})
|
})
|
||||||
|
Reference in New Issue
Block a user