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

100 lines
2.3 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')
const ansi = require('ansi-escape-sequences')
const loadConfig = require('config-master')
const path = require('path')
2014-06-17 00:40:41 +01:00
2015-11-11 17:53:01 +00:00
const cli = commandLineArgs(cliOptions.definitions)
const usage = cli.getUsage(cliOptions.usageData)
2015-11-15 21:15:25 +00:00
const stored = loadConfig('local-web-server')
const options = collectOptions()
2015-11-11 17:53:01 +00:00
// TODO summary line on server launch
2015-11-15 21:15:25 +00:00
if (options.misc.help) {
console.log(usage)
process.exit(0)
2015-11-11 17:53:01 +00:00
}
2015-11-15 21:15:25 +00:00
if (options.misc.config) {
2015-11-17 12:55:25 +00:00
console.log(JSON.stringify(options.server, null, ' '))
2015-11-15 21:15:25 +00:00
process.exit(0)
2015-11-11 17:53:01 +00:00
}
localWebServer({
2015-11-15 21:15:25 +00:00
static: {
root: options.server.directory,
options: {
hidden: true
}
},
serveIndex: {
path: options.server.directory,
options: {
icons: true,
hidden: true
}
},
2015-11-16 23:02:27 +00:00
log: {
format: options.server['log-format']
},
2015-11-15 21:15:25 +00:00
compress: options.server.compress,
mime: options.server.mime,
forbid: options.server.forbid,
2015-11-15 21:15:25 +00:00
spa: options.server.spa,
2015-11-16 13:22:51 +00:00
'no-cache': options.server['no-cache'],
rewrite: options.server.rewrite
2015-11-15 21:15:25 +00:00
}).listen(options.server.port, onServerUp)
2015-11-11 17:53:01 +00:00
2015-11-15 21:15:25 +00:00
function halt (err) {
console.log(ansi.format(`Error: ${err.message}`, 'red'))
2015-11-11 17:53:01 +00:00
console.log(usage)
process.exit(1)
2015-11-10 21:50:56 +00:00
}
2015-11-13 11:26:02 +00:00
function onServerUp () {
console.error(ansi.format(
2015-11-15 21:15:25 +00:00
path.resolve(options.server.directory) === process.cwd()
2015-11-16 23:02:27 +00:00
? `serving at [underline]{http://localhost:${options.server.port}}`
: `serving [underline]{${options.server.directory}} at [underline]{http://localhost:${options.server.port}}`
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 */
try {
options = cli.parse()
} catch (err) {
halt(err)
}
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]
}
})
}