4 changed files with 136 additions and 9 deletions
@ -0,0 +1,94 @@ |
|||
import ansi from 'ansi-escapes'; |
|||
import io from 'socket.io-client'; |
|||
import chalk from 'chalk'; |
|||
import EventEmitter from 'events'; |
|||
|
|||
export default class Logger extends EventEmitter { |
|||
|
|||
constructor (host) { |
|||
super(); |
|||
this.host = host; |
|||
this.socket = io(`https://io.now.sh?host=${host}`); |
|||
this.socket.once('error', this.onError.bind(this)); |
|||
this.socket.on('state', this.onState.bind(this)); |
|||
this.socket.on('logs', this.onLog.bind(this)); |
|||
this.socket.on('backend', this.onComplete.bind(this)); |
|||
this.lines = new Lines(10); |
|||
} |
|||
|
|||
onState (state) { |
|||
if (!state.id) { |
|||
console.error('> Deployment not found'); |
|||
this.emit('error'); |
|||
} |
|||
|
|||
if (state.backend) { |
|||
this.onComplete(); |
|||
return; |
|||
} |
|||
|
|||
if (state.logs) { |
|||
state.logs.forEach(this.onLog, this); |
|||
} |
|||
} |
|||
|
|||
onLog (log) { |
|||
if ('command' === log.type) { |
|||
if ('npm install' === log.data) { |
|||
console.log('> Building'); |
|||
} |
|||
|
|||
console.log(`${chalk.gray('>')} ▲ ${log.data}`); |
|||
this.lines.reset(); |
|||
} else if ('stderr' === log.type) { |
|||
log.data.split('\n').forEach((v) => { |
|||
if (v.length) { |
|||
console.error(chalk.red(`> ${v}`)); |
|||
} |
|||
}); |
|||
this.lines.reset(); |
|||
} else if ('stdout' === log.type) { |
|||
log.data.split('\n').forEach((v) => { |
|||
if (v.length) { |
|||
this.lines.write(`${chalk.gray('>')} ${v}`); |
|||
} |
|||
}); |
|||
} |
|||
} |
|||
|
|||
onComplete () { |
|||
this.socket.disconnect(); |
|||
this.emit('close'); |
|||
} |
|||
|
|||
onError () { |
|||
this.emit('error'); |
|||
} |
|||
|
|||
} |
|||
|
|||
class Lines { |
|||
|
|||
constructor (maxLines = 100) { |
|||
this.max = maxLines; |
|||
this.buf = []; |
|||
} |
|||
|
|||
write (str) { |
|||
const { max, buf } = this; |
|||
|
|||
if (buf.length === max) { |
|||
process.stdout.write(ansi.eraseLines(max + 1)); |
|||
buf.shift(); |
|||
buf.forEach((line) => console.log(line)); |
|||
} |
|||
|
|||
buf.push(str); |
|||
console.log(str); |
|||
} |
|||
|
|||
reset () { |
|||
this.buf = []; |
|||
} |
|||
|
|||
} |
Loading…
Reference in new issue