Browse Source
server now created on contruction, before views intantiated.. refactor view API
master
server now created on contruction, before views intantiated.. refactor view API
master
Lloyd Brookes
9 years ago
6 changed files with 119 additions and 92 deletions
-
3bin/cli.js
-
18doc/api.md
-
2doc/visualisation.md
-
65lib/cli-view.js
-
111lib/local-web-server.js
-
12test/test.js
@ -1,5 +1,4 @@ |
|||
#!/usr/bin/env node
|
|||
'use strict' |
|||
const LocalWebServer = require('../') |
|||
const ws = new LocalWebServer() |
|||
ws.getServer() |
|||
new LocalWebServer() |
@ -1,31 +1,48 @@ |
|||
'use strict' |
|||
|
|||
class CliView { |
|||
constructor (localWebServer) { |
|||
this.options = localWebServer.options |
|||
this.localWebServer = localWebServer |
|||
} |
|||
info (key, value) { |
|||
if (key && value) { |
|||
const ansi = require('ansi-escape-sequences') |
|||
const tableLayout = require('table-layout') |
|||
const output = tableLayout({ key: ansi.format(key, 'bold'), value: value}, { |
|||
padding: { left: '', right: ' ' }, |
|||
columns: [ |
|||
{ name: 'key', width: 18 }, |
|||
{ name: 'value', nowrap: true } |
|||
] |
|||
}) |
|||
process.stderr.write(output) |
|||
} else { |
|||
console.error(key) |
|||
} |
|||
} |
|||
verbose (key, value) { |
|||
if (this.options.verbose) { |
|||
this.info(key, value) |
|||
} |
|||
} |
|||
error (msg) { |
|||
console.error(ansi.format(msg, 'red')) |
|||
write (msg) { |
|||
const writeToStdout = [ 'log', 'info' ] |
|||
Object.keys(msg).forEach(key => { |
|||
if (writeToStdout.includes(key)) { |
|||
console.log(msg[key]) |
|||
} else if (key === 'config' && msg.config && this.localWebServer.options.verbose) { |
|||
printLine(msg.config) |
|||
} else if (key === 'error') { |
|||
const ansi = require('ansi-escape-sequences') |
|||
console.error(ansi.format(msg.error, 'red')) |
|||
} |
|||
}) |
|||
} |
|||
} |
|||
|
|||
module.exports = CliView |
|||
|
|||
function printLine (config) { |
|||
const output = objectToTable(config) |
|||
process.stderr.write(output) |
|||
} |
|||
|
|||
function objectToTable (object) { |
|||
const ansi = require('ansi-escape-sequences') |
|||
const tableLayout = require('table-layout') |
|||
const t = require('typical') |
|||
|
|||
const data = Object.keys(object).map(key => { |
|||
if (t.isObject(object[key])) { |
|||
return { key: ansi.format(key, 'bold'), value: objectToTable(object[key]) } |
|||
} else { |
|||
return { key: ansi.format(key, 'bold'), value: object[key] } |
|||
} |
|||
}) |
|||
return tableLayout(data, { |
|||
padding: { left: '', right: ' ' }, |
|||
columns: [ |
|||
// { name: 'key', width: 18 },
|
|||
// { name: 'value', nowrap: true }
|
|||
] |
|||
}) |
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue