ignoreCli option.. fixed tests.. removed .listen() and close().. refactor
This commit is contained in:
35
bin/cli.js
35
bin/cli.js
@ -1,7 +1,36 @@
|
|||||||
#!/usr/bin/env node
|
#!/usr/bin/env node
|
||||||
'use strict'
|
'use strict'
|
||||||
const LocalWebServer = require('../')
|
const path = require('path')
|
||||||
|
|
||||||
|
function onServerUp (port, directory, isHttps) {
|
||||||
|
const ansi = require('ansi-escape-sequences')
|
||||||
|
|
||||||
|
const ipList = getIPList()
|
||||||
|
.map(iface => `[underline]{${isHttps ? 'https' : 'http'}://${iface.address}:${port}}`)
|
||||||
|
.join(', ')
|
||||||
|
|
||||||
|
console.error(ansi.format(
|
||||||
|
path.resolve(directory || '') === process.cwd()
|
||||||
|
? `serving at ${ipList}`
|
||||||
|
: `serving [underline]{${directory}} at ${ipList}`
|
||||||
|
))
|
||||||
|
}
|
||||||
|
|
||||||
|
function getIPList () {
|
||||||
|
const flatten = require('reduce-flatten')
|
||||||
|
const os = require('os')
|
||||||
|
|
||||||
|
let ipList = Object.keys(os.networkInterfaces())
|
||||||
|
.map(key => os.networkInterfaces()[key])
|
||||||
|
.reduce(flatten, [])
|
||||||
|
.filter(iface => iface.family === 'IPv4')
|
||||||
|
ipList.unshift({ address: os.hostname() })
|
||||||
|
return ipList
|
||||||
|
}
|
||||||
|
|
||||||
|
const LocalWebServer = require('../')
|
||||||
const ws = new LocalWebServer()
|
const ws = new LocalWebServer()
|
||||||
ws.listen()
|
const server = ws.getServer()
|
||||||
.catch(err => console.error(err.stack))
|
server.on('listening', function () {
|
||||||
|
onServerUp(ws.options.port, ws.options['static.root'], server.isHttps)
|
||||||
|
})
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
#!/usr/bin/env node
|
#!/usr/bin/env node
|
||||||
'use strict'
|
'use strict'
|
||||||
const ansi = require('ansi-escape-sequences')
|
|
||||||
const path = require('path')
|
const path = require('path')
|
||||||
const CommandLineTool = require('command-line-tool')
|
const CommandLineTool = require('command-line-tool')
|
||||||
const flatten = require('reduce-flatten')
|
const flatten = require('reduce-flatten')
|
||||||
@ -83,22 +82,34 @@ class LocalWebServer {
|
|||||||
|
|
||||||
let options = {}
|
let options = {}
|
||||||
const allOptionDefinitions = cli.optionDefinitions.concat(middlewareOptionDefinitions)
|
const allOptionDefinitions = cli.optionDefinitions.concat(middlewareOptionDefinitions)
|
||||||
try {
|
if (!initOptions.ignoreCli) {
|
||||||
options = commandLineArgs(allOptionDefinitions)
|
try {
|
||||||
} catch (err) {
|
options = commandLineArgs(allOptionDefinitions)
|
||||||
tool.printError(err)
|
} catch (err) {
|
||||||
tool.printError(allOptionDefinitions.map(def => {
|
tool.printError(err)
|
||||||
return `name: ${def.name}${def.alias ? ', alias: ' + def.alias : ''}`
|
tool.printError(allOptionDefinitions.map(def => {
|
||||||
}).join('\n'))
|
return `name: ${def.name}${def.alias ? ', alias: ' + def.alias : ''}`
|
||||||
console.error(usage)
|
}).join('\n'))
|
||||||
tool.halt()
|
console.error(usage)
|
||||||
|
tool.halt()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* combine in stored config */
|
/* combine in stored config */
|
||||||
options = Object.assign({ port: 8000 }, initOptions, stored || {}, options.server, options.middleware, options.misc)
|
options = Object.assign(
|
||||||
this.options = options
|
{ port: 8000 },
|
||||||
|
initOptions,
|
||||||
|
stored,
|
||||||
|
options.server,
|
||||||
|
options.middleware,
|
||||||
|
options.misc
|
||||||
|
)
|
||||||
|
|
||||||
// console.log(initOptions, stored, options)
|
/**
|
||||||
|
* Config
|
||||||
|
* @type {object}
|
||||||
|
*/
|
||||||
|
this.options = options
|
||||||
|
|
||||||
if (options.verbose) {
|
if (options.verbose) {
|
||||||
// debug.setLevel(1)
|
// debug.setLevel(1)
|
||||||
@ -146,9 +157,10 @@ class LocalWebServer {
|
|||||||
return app
|
return app
|
||||||
}
|
}
|
||||||
|
|
||||||
getServer () {
|
getServer (onListening) {
|
||||||
const app = this.getApplication()
|
const app = this.getApplication()
|
||||||
const options = this.options
|
const options = this.options
|
||||||
|
|
||||||
let key = options.key
|
let key = options.key
|
||||||
let cert = options.cert
|
let cert = options.cert
|
||||||
|
|
||||||
@ -172,48 +184,10 @@ class LocalWebServer {
|
|||||||
const http = require('http')
|
const http = require('http')
|
||||||
server = http.createServer(app.callback())
|
server = http.createServer(app.callback())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
server.listen(options.port, onListening)
|
||||||
return server
|
return server
|
||||||
}
|
}
|
||||||
|
|
||||||
listen () {
|
|
||||||
const options = this.options
|
|
||||||
const server = this._server = this.getServer()
|
|
||||||
// console.log(options)
|
|
||||||
return new Promise((resolve, reject) => {
|
|
||||||
server.listen(options.port, () => {
|
|
||||||
onServerUp(options.port, options['static.root'], server.isHttps)
|
|
||||||
resolve(server)
|
|
||||||
})
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
close () {
|
|
||||||
this._server.close()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function onServerUp (port, directory, isHttps) {
|
|
||||||
const ipList = getIPList()
|
|
||||||
.map(iface => `[underline]{${isHttps ? 'https' : 'http'}://${iface.address}:${port}}`)
|
|
||||||
.join(', ')
|
|
||||||
|
|
||||||
console.error(ansi.format(
|
|
||||||
path.resolve(directory || '') === process.cwd()
|
|
||||||
? `serving at ${ipList}`
|
|
||||||
: `serving [underline]{${directory}} at ${ipList}`
|
|
||||||
))
|
|
||||||
}
|
|
||||||
|
|
||||||
function getIPList () {
|
|
||||||
const flatten = require('reduce-flatten')
|
|
||||||
const os = require('os')
|
|
||||||
|
|
||||||
let ipList = Object.keys(os.networkInterfaces())
|
|
||||||
.map(key => os.networkInterfaces()[key])
|
|
||||||
.reduce(flatten, [])
|
|
||||||
.filter(iface => iface.family === 'IPv4')
|
|
||||||
ipList.unshift({ address: os.hostname() })
|
|
||||||
return ipList
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
32
test/test.js
32
test/test.js
@ -9,16 +9,15 @@ test('stack', function (t) {
|
|||||||
t.plan(2)
|
t.plan(2)
|
||||||
const ws = new LocalWebServer({
|
const ws = new LocalWebServer({
|
||||||
stack: [ path.resolve(__dirname, 'test-middleware.js') ],
|
stack: [ path.resolve(__dirname, 'test-middleware.js') ],
|
||||||
port: 8100
|
port: 8100,
|
||||||
|
ignoreCli: true
|
||||||
|
})
|
||||||
|
const server = ws.getServer(() => {
|
||||||
|
return request('http://localhost:8100/')
|
||||||
|
.then(c.checkResponse(t, 200, /1234512345/))
|
||||||
|
.then(server.close.bind(server))
|
||||||
|
.catch(c.fail(t))
|
||||||
})
|
})
|
||||||
ws.listen()
|
|
||||||
.then(() => {
|
|
||||||
return request('http://localhost:8100/')
|
|
||||||
.then(c.checkResponse(t, 200, /1234512345/))
|
|
||||||
.then(ws.close.bind(ws))
|
|
||||||
.catch(c.fail(t))
|
|
||||||
})
|
|
||||||
.catch(c.fail(t))
|
|
||||||
})
|
})
|
||||||
|
|
||||||
test('https', function (t) {
|
test('https', function (t) {
|
||||||
@ -26,13 +25,12 @@ test('https', function (t) {
|
|||||||
const ws = new LocalWebServer({
|
const ws = new LocalWebServer({
|
||||||
stack: [ path.resolve(__dirname, 'test-middleware.js') ],
|
stack: [ path.resolve(__dirname, 'test-middleware.js') ],
|
||||||
https: true,
|
https: true,
|
||||||
port: 8100
|
port: 8100,
|
||||||
|
ignoreCli: true
|
||||||
|
})
|
||||||
|
const server = ws.getServer(() => {
|
||||||
|
return request('https://localhost:8100/')
|
||||||
|
.then(c.checkResponse(t, 200, /1234512345/))
|
||||||
|
.then(server.close.bind(server))
|
||||||
})
|
})
|
||||||
ws.listen()
|
|
||||||
.then(() => {
|
|
||||||
return request('https://localhost:8100/')
|
|
||||||
.then(c.checkResponse(t, 200, /1234512345/))
|
|
||||||
.then(ws.close.bind(ws))
|
|
||||||
})
|
|
||||||
.catch(c.fail(t))
|
|
||||||
})
|
})
|
||||||
|
Reference in New Issue
Block a user