Skip to content

Commit

Permalink
fix(shell): Adapter loading and output
Browse files Browse the repository at this point in the history
  • Loading branch information
timkinnane committed Aug 12, 2018
1 parent 5941361 commit 914bf9e
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 13 deletions.
1 change: 0 additions & 1 deletion package.json
Expand Up @@ -53,7 +53,6 @@
"devDependencies": {
"@types/chai": "^4.1.0",
"@types/mocha": "^5.0.0",
"@types/mock-fs": "^3.6.0",
"@types/sinon": "^5.0.0",
"chai": "^4.0.0",
"codecov": "^3.0.0",
Expand Down
24 changes: 17 additions & 7 deletions src/adapters/shell.ts
@@ -1,7 +1,10 @@
import * as bBot from '..'
import Transport from 'winston-transport'
import * as inquirer from 'inquirer'
import chalk from 'chalk'

class ShellTransport extends Transport {}

/** Load prompts and render chat in shell, for testing interactions */
export class Shell extends bBot.MessageAdapter {
name = 'shell-message-adapter'
Expand All @@ -15,7 +18,7 @@ export class Shell extends bBot.MessageAdapter {

/** Update chat window and return to input prompt */
async render () {
let _ = '\n'
let _ = '\n\n'
let n = ' '
_ += chalk.cyan('╔═════════════════════════════════════════════════════════▶') + '\n'
for (let m of this.messages.slice(-this.settings.chatSize)) {
Expand All @@ -27,18 +30,20 @@ export class Shell extends bBot.MessageAdapter {
}

/** Route log events to the inquirer UI (only the combined log) */
log (transport: any, level: string, msg: string) {
if (transport.name === 'console') {
let item = `[${level}]${msg}`
log (logEvent: any, callback: any) {
if (this.ui) {
const { message, level } = logEvent
let item = `${level}: ${message}`
switch (level) {
case 'debug': item = chalk.gray(item)
break
case 'warn': item = chalk.magenta(item)
break
case 'error': item = chalk.red(item)
}
this.ui.writeLog(item)
this.ui.log.write(item.trim())
}
callback()
}

/** Register user and room, then render chat with welcome message */
Expand All @@ -55,7 +60,6 @@ export class Shell extends bBot.MessageAdapter {
message: 'And what about this "room"?',
default: 'shell'
}])
this.bot.logger.on('logging', this.log.bind(this))
this.user = new this.bot.User({ name: registration.username })
this.room = { name: registration.room }
const e = new this.bot.Envelope()
Expand Down Expand Up @@ -96,4 +100,10 @@ export class Shell extends bBot.MessageAdapter {
}
}

export const use = (bot: typeof bBot) => new Shell(bot)
export const use = (bot: typeof bBot) => {
const shell = new Shell(bot)
const transport = new ShellTransport()
transport.log = shell.log.bind(shell)
bot.logger.add(transport)
return shell
}
3 changes: 2 additions & 1 deletion src/lib/adapter.ts
Expand Up @@ -34,10 +34,11 @@ export function loadAdapter (adapterPath?: string) {
if (!isPath) adapterPath = `./adapters/${adapterPath}`
bot.logger.debug(`[adapter] loading adapter by path: ${adapterPath}`)
let sourcePath = 'src'
let distPath = 'dist'
let modulePath = 'node_modules/bbot/dist'
let currentPath = process.cwd()
let currentModule = path.resolve(currentPath, modulePath)
let resolver = { paths: [ sourcePath, currentPath, currentModule ] }
let resolver = { paths: [ sourcePath, distPath, currentPath, currentModule ] }
if (require.main) resolver.paths = resolver.paths.concat(...require.main.paths)
try {
adapterPath = require.resolve(adapterPath, resolver)
Expand Down
6 changes: 4 additions & 2 deletions src/lib/logger.ts
@@ -1,5 +1,5 @@
import * as winston from 'winston'
const { combine, timestamp, json, prettyPrint, colorize } = winston.format
const { combine, timestamp, json, colorize, align } = winston.format

/**
* Winston logger provides a logging interface common to many Node apps, with
Expand Down Expand Up @@ -32,7 +32,9 @@ export const logger = winston.createLogger({
format: combine(timestamp(), json())
}),
new winston.transports.Console({
format: combine(timestamp(), prettyPrint(), colorize())
format: combine(colorize(), align(), winston.format.printf((nfo: any) => {
return `${nfo.level}: ${nfo.message}`
}))
})
]
})
4 changes: 2 additions & 2 deletions yarn.lock
Expand Up @@ -2410,8 +2410,8 @@ rx@^4.1.0:
resolved "https://registry.yarnpkg.com/rx/-/rx-4.1.0.tgz#a5f13ff79ef3b740fe30aa803fb09f98805d4782"

rxjs@^6.1.0:
version "6.2.1"
resolved "https://registry.yarnpkg.com/rxjs/-/rxjs-6.2.1.tgz#246cebec189a6cbc143a3ef9f62d6f4c91813ca1"
version "6.2.2"
resolved "https://registry.yarnpkg.com/rxjs/-/rxjs-6.2.2.tgz#eb75fa3c186ff5289907d06483a77884586e1cf9"
dependencies:
tslib "^1.9.0"

Expand Down

0 comments on commit 914bf9e

Please sign in to comment.