Files
hiring-test-one/bin/cli.js

168 lines
4.2 KiB
JavaScript
Raw Normal View History

2014-02-05 15:28:23 +01:00
#!/usr/bin/env node
2015-10-30 11:31:59 +00:00
'use strict'
2015-11-08 22:09:07 +00:00
const localWebServer = require('../')
2015-11-16 23:02:27 +00:00
const cliOptions = require('../lib/cli-options')
2015-11-11 17:53:01 +00:00
const commandLineArgs = require('command-line-args')
2016-05-29 18:38:12 +01:00
const commandLineUsage = require('command-line-usage')
2015-11-11 17:53:01 +00:00
const ansi = require('ansi-escape-sequences')
const loadConfig = require('config-master')
const path = require('path')
const os = require('os')
2015-11-24 11:46:47 +00:00
const arrayify = require('array-back')
2015-11-30 11:06:22 +00:00
const t = require('typical')
2016-03-10 10:52:56 +00:00
const flatten = require('reduce-flatten')
2014-06-17 00:40:41 +01:00
2016-05-29 18:38:12 +01:00
const usage = commandLineUsage(cliOptions.usageData)
2015-11-15 21:15:25 +00:00
const stored = loadConfig('local-web-server')
2016-05-29 18:38:12 +01:00
let options
let isHttps = false
2015-11-11 17:53:01 +00:00
try {
options = collectOptions()
} catch (err) {
stop([ `[red]{Error}: ${err.message}`, usage ], 1)
return
}
2015-11-11 17:53:01 +00:00
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
}
2015-11-30 11:06:22 +00:00
2016-05-18 11:35:11 +01:00
const convert = require('koa-convert')
const Koa = require('koa')
const app = new Koa()
const _use = app.use
app.use = x => _use.call(app, convert(x))
app.on('error', err => {
if (options.server['log-format']) {
console.error(ansi.format(err.message, 'red'))
}
2016-05-18 11:35:11 +01:00
const ws = localWebServer({
static: {
root: options.server.directory,
options: {
hidden: true
}
},
serveIndex: {
path: options.server.directory,
options: {
icons: true,
hidden: true
}
},
log: {
format: options.server['log-format']
},
2016-04-18 14:08:28 +03:00
cacheControl: options.server.cacheControl,
compress: options.server.compress,
mime: options.server.mime,
forbid: options.server.forbid,
spa: options.server.spa,
'no-cache': options.server['no-cache'],
rewrite: options.server.rewrite,
verbose: options.server.verbose,
mocks: options.server.mocks
})
2016-03-10 10:52:56 +00:00
2016-05-18 11:35:11 +01:00
app.use(ws)
if (options.server.https) {
options.server.key = path.resolve(__dirname, '..', 'ssl', '127.0.0.1.key')
options.server.cert = path.resolve(__dirname, '..', 'ssl', '127.0.0.1.crt')
2015-11-30 11:06:22 +00:00
}
if (options.server.key && options.server.cert) {
const https = require('https')
const fs = require('fs')
isHttps = true
const serverOptions = {
key: fs.readFileSync(options.server.key),
cert: fs.readFileSync(options.server.cert)
}
const server = https.createServer(serverOptions, app.callback())
server.listen(options.server.port, onServerUp)
} else {
app.listen(options.server.port, onServerUp)
}
2015-11-30 11:06:22 +00:00
}
2015-11-11 17:53:01 +00:00
2015-11-24 11:46:47 +00:00
function stop (msgs, exitCode) {
arrayify(msgs).forEach(msg => console.error(ansi.format(msg)))
process.exitCode = exitCode
2015-11-10 21:50:56 +00:00
}
2015-11-13 11:26:02 +00:00
function onServerUp () {
let ipList = Object.keys(os.networkInterfaces())
.map(key => os.networkInterfaces()[key])
2016-03-10 10:52:56 +00:00
.reduce(flatten, [])
.filter(iface => iface.family === 'IPv4')
ipList.unshift({ address: os.hostname() })
ipList = ipList
2015-11-30 11:06:22 +00:00
.map(iface => `[underline]{${isHttps ? 'https' : 'http'}://${iface.address}:${options.server.port}}`)
.join(', ')
2015-11-13 11:26:02 +00:00
console.error(ansi.format(
2015-11-15 21:15:25 +00:00
path.resolve(options.server.directory) === process.cwd()
2015-11-23 10:27:26 +00:00
? `serving at ${ipList}`
: `serving [underline]{${options.server.directory}} at ${ipList}`
2015-11-13 11:26:02 +00:00
))
2015-11-11 17:53:01 +00:00
}
2015-11-15 21:15:25 +00:00
function collectOptions () {
let options = {}
/* parse command line args */
2016-05-29 18:38:12 +01:00
options = commandLineArgs(cliOptions.definitions)
2015-11-15 21:15:25 +00:00
const builtIn = {
port: 8000,
directory: process.cwd(),
2015-11-16 13:22:51 +00:00
forbid: [],
rewrite: []
}
if (options.server.rewrite) {
options.server.rewrite = parseRewriteRules(options.server.rewrite)
2015-11-15 21:15:25 +00:00
}
/* override built-in defaults with stored config and then command line args */
options.server = Object.assign(builtIn, stored, options.server)
return options
}
2015-11-16 23:02:27 +00:00
function parseRewriteRules (rules) {
return rules && rules.map(rule => {
2015-11-16 23:02:27 +00:00
const matches = rule.match(/(\S*)\s*->\s*(\S*)/)
return {
from: matches[1],
to: matches[2]
}
})
}
2015-11-30 11:06:22 +00:00
function validateOptions (options) {
let valid = true
2015-11-30 11:06:22 +00:00
function invalid (msg) {
return `[red underline]{Invalid:} [bold]{${msg}}`
}
2015-11-30 11:06:22 +00:00
if (!t.isNumber(options.server.port)) {
stop([ invalid(`--port must be numeric`), usage ], 1)
valid = false
2015-11-30 11:06:22 +00:00
}
return valid
2015-11-30 11:06:22 +00:00
}