@ -2,36 +2,11 @@
const EventEmitter = require ( 'events' ) ;
// Packages
const ansi = require ( 'ansi-escapes' ) ;
const io = require ( 'socket.io-client' ) ;
const chalk = require ( 'chalk' ) ;
const { compare , deserialize } = require ( './logs' ) ;
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 = [ ] ;
}
}
module . exports = class Logger extends EventEmitter {
constructor ( host , token , { debug = false , quiet = false } = { } ) {
super ( ) ;
@ -50,8 +25,6 @@ module.exports = class Logger extends EventEmitter {
this . socket . on ( 'logs' , this . onLog . bind ( this ) ) ;
this . socket . on ( 'backend' , this . onComplete . bind ( this ) ) ;
this . lines = new Lines ( 10 ) ;
// Log buffer
this . buf = [ ] ;
}
@ -100,18 +73,15 @@ module.exports = class Logger extends EventEmitter {
log = deserialize ( log ) ;
const timer = setTimeout (
( ) => {
this . buf . sort ( ( a , b ) => compare ( a . log , b . log ) ) ;
const idx = this . buf . findIndex ( b => b . log . id === log . id ) + 1 ;
for ( const b of this . buf . slice ( 0 , idx ) ) {
clearTimeout ( b . timer ) ;
this . printLog ( b . log ) ;
}
this . buf = this . buf . slice ( idx ) ;
} ,
300
) ;
const timer = setTimeout ( ( ) => {
this . buf . sort ( ( a , b ) => compare ( a . log , b . log ) ) ;
const idx = this . buf . findIndex ( b => b . log . id === log . id ) + 1 ;
for ( const b of this . buf . slice ( 0 , idx ) ) {
clearTimeout ( b . timer ) ;
this . printLog ( b . log ) ;
}
this . buf = this . buf . slice ( idx ) ;
} , 300 ) ;
this . buf . push ( { log , timer } ) ;
}
@ -146,18 +116,16 @@ module.exports = class Logger extends EventEmitter {
if ( log . type === 'command' ) {
console . log ( ` ${ chalk . gray ( '>' ) } ▲ ${ data } ` ) ;
this . lines . reset ( ) ;
} else if ( log . type === 'stderr' ) {
data . split ( '\n' ) . forEach ( v => {
if ( v . length > 0 ) {
console . error ( chalk . gray ( ` > ${ v } ` ) ) ;
}
} ) ;
this . lines . reset ( ) ;
} else if ( log . type === 'stdout' ) {
data . split ( '\n' ) . forEach ( v => {
if ( v . length > 0 ) {
this . lines . write ( ` ${ chalk . gray ( '>' ) } ${ v } ` ) ;
console . log ( ` ${ chalk . gray ( '>' ) } ${ v } ` ) ;
}
} ) ;
}