cli rewrite
This commit is contained in:
72
bin/ws.js
72
bin/ws.js
@ -2,16 +2,68 @@
|
|||||||
'use strict'
|
'use strict'
|
||||||
const localWebServer = require('../')
|
const localWebServer = require('../')
|
||||||
const streamLogStats = require('stream-log-stats')
|
const streamLogStats = require('stream-log-stats')
|
||||||
|
const commandLineArgs = require('command-line-args')
|
||||||
|
const ansi = require('ansi-escape-sequences')
|
||||||
|
const cliOptions = require('../lib/cli-options')
|
||||||
|
const loadConfig = require('config-master')
|
||||||
|
const path = require('path')
|
||||||
|
|
||||||
const options = {
|
const options = {}
|
||||||
static: { root: '.' },
|
|
||||||
serveIndex: { path: '.' },
|
/* parse command line args */
|
||||||
logger: { format: 'common', options: {
|
const cli = commandLineArgs(cliOptions.definitions)
|
||||||
stream: streamLogStats({ refreshRate: 100 })}
|
const usage = cli.getUsage(cliOptions.usageData)
|
||||||
}
|
|
||||||
|
try {
|
||||||
|
options.cli = cli.parse()
|
||||||
|
} catch (err) {
|
||||||
|
halt(err.message)
|
||||||
}
|
}
|
||||||
|
|
||||||
localWebServer(options)
|
options.stored = loadConfig('local-web-server')
|
||||||
.listen(8000, () => {
|
options.builtIn = {
|
||||||
console.log(`listening`)
|
port: 8000,
|
||||||
})
|
directory: process.cwd(),
|
||||||
|
'refresh-rate': 500,
|
||||||
|
mime: {}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* override built-in defaults with stored config and then command line args */
|
||||||
|
options.cli.server = Object.assign(options.builtIn, options.stored, options.cli.server)
|
||||||
|
|
||||||
|
if (options.cli.misc.help) return console.log(usage)
|
||||||
|
if (options.cli.misc.config) return console.log(JSON.stringify(options.stored, null, ' '))
|
||||||
|
|
||||||
|
let log = {
|
||||||
|
format: options.cli.server['log-format']
|
||||||
|
}
|
||||||
|
|
||||||
|
if (log.format === 'none'){
|
||||||
|
log.format = undefined
|
||||||
|
} else if (log.format){
|
||||||
|
log.stream = process.stdout
|
||||||
|
} else {
|
||||||
|
log.format = 'common'
|
||||||
|
log.stream = streamLogStats({ refreshRate: 100 })
|
||||||
|
}
|
||||||
|
|
||||||
|
localWebServer({
|
||||||
|
static: { root: options.cli.server.directory },
|
||||||
|
serveIndex: { path: options.cli.server.directory, options: { icons: true } },
|
||||||
|
logger: { format: log.format, options: { stream: log.stream } }
|
||||||
|
}).listen(options.cli.server.port, serverUp)
|
||||||
|
|
||||||
|
function halt (message) {
|
||||||
|
console.log(ansi.format(`Error: ${message}`, 'red'))
|
||||||
|
console.log(usage)
|
||||||
|
process.exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
function serverUp () {
|
||||||
|
/* write launch information to stderr (stdout is reserved for web log output) */
|
||||||
|
if (path.resolve(options.cli.server.directory) === process.cwd()) {
|
||||||
|
console.error(ansi.format(`serving at [underline]{http://localhost:${options.cli.server.port}}`))
|
||||||
|
} else {
|
||||||
|
console.error(ansi.format(`serving [underline]{options.cli.server.directory} at [underline]{http://localhost:${options.cli.server.port}}`))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -1,30 +1,46 @@
|
|||||||
module.exports = [
|
module.exports = {
|
||||||
{
|
definitions: [
|
||||||
name: 'port', alias: 'p', type: Number, defaultOption: true,
|
{
|
||||||
description: 'Web server port', group: 'server'
|
name: 'port', alias: 'p', type: Number, defaultOption: true,
|
||||||
},
|
description: 'Web server port', group: 'server'
|
||||||
{
|
},
|
||||||
name: 'log-format', alias: 'f', type: String,
|
{
|
||||||
description: "If a format is supplied an access log is written to stdout. If not, a statistics view is displayed. Use a preset ('none', 'dev','combined', 'short', 'tiny' or 'logstalgia') or supply a custom format (e.g. ':method -> :url').", group: 'server'
|
name: 'log-format', alias: 'f', type: String,
|
||||||
},
|
description: "If a format is supplied an access log is written to stdout. If not, a statistics view is displayed. Use a preset ('none', 'dev','combined', 'short', 'tiny' or 'logstalgia') or supply a custom format (e.g. ':method -> :url').", group: 'server'
|
||||||
{
|
},
|
||||||
name: 'directory', alias: 'd', type: String,
|
{
|
||||||
description: 'Root directory, defaults to the current directory', group: 'server'
|
name: 'directory', alias: 'd', type: String,
|
||||||
},
|
description: 'Root directory, defaults to the current directory', group: 'server'
|
||||||
{
|
},
|
||||||
name: 'compress', alias: 'c', type: Boolean,
|
{
|
||||||
description: 'Enable gzip compression, reduces bandwidth.', group: 'server'
|
name: 'compress', alias: 'c', type: Boolean,
|
||||||
},
|
description: 'Enable gzip compression, reduces bandwidth.', group: 'server'
|
||||||
{
|
},
|
||||||
name: 'refresh-rate', alias: 'r', type: Number,
|
{
|
||||||
description: 'Statistics view refresh rate in ms. Defaults to 500.', group: 'server'
|
name: 'refresh-rate', alias: 'r', type: Number,
|
||||||
},
|
description: 'Statistics view refresh rate in ms. Defaults to 500.', group: 'server'
|
||||||
{
|
},
|
||||||
name: 'help', alias: 'h', type: Boolean,
|
{
|
||||||
description: 'Print these usage instructions', group: 'misc'
|
name: 'help', alias: 'h', type: Boolean,
|
||||||
},
|
description: 'Print these usage instructions', group: 'misc'
|
||||||
{
|
},
|
||||||
name: 'config', type: Boolean,
|
{
|
||||||
description: 'Print the stored config', group: 'misc'
|
name: 'config', type: Boolean,
|
||||||
|
description: 'Print the stored config', group: 'misc'
|
||||||
|
}
|
||||||
|
],
|
||||||
|
usageData: {
|
||||||
|
title: 'local-web-server',
|
||||||
|
description: 'Lightweight static web server, zero configuration.',
|
||||||
|
footer: 'Project home: [underline]{https://github.com/75lb/local-web-server}',
|
||||||
|
synopsis: [
|
||||||
|
'$ ws <server options>',
|
||||||
|
'$ ws --config',
|
||||||
|
'$ ws --help'
|
||||||
|
],
|
||||||
|
groups: {
|
||||||
|
server: 'Server',
|
||||||
|
misc: 'Misc'
|
||||||
|
}
|
||||||
}
|
}
|
||||||
]
|
}
|
||||||
|
@ -19,7 +19,7 @@
|
|||||||
"dependencies": {
|
"dependencies": {
|
||||||
"command-line-args": "^2.0.2",
|
"command-line-args": "^2.0.2",
|
||||||
"compression": "^1.0.2",
|
"compression": "^1.0.2",
|
||||||
"config-master": "^1",
|
"config-master": "^2",
|
||||||
"console-dope": "~0.3.0",
|
"console-dope": "~0.3.0",
|
||||||
"deep-extend": "^0.4.0",
|
"deep-extend": "^0.4.0",
|
||||||
"express": "^4.13.3",
|
"express": "^4.13.3",
|
||||||
|
Reference in New Issue
Block a user