All files / owid-grapher/baker ProgressStream.ts

70.97% Statements 22/31
100% Branches 1/1
20% Functions 1/5
70.97% Lines 22/31

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 371x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x     1x 1x       1x 1x     1x 1x                
import { WriteStream } from "tty"
 
// Wrap stderr before passing it to ProgressBar so we can save all writes
// and replay them at the end of the bake. Without this the progress bar class
// works fine, but there is no way to show the summary once the job is complete.
export class ProgressStream implements Partial<WriteStream> {
    private wrappedStream: WriteStream
    constructor(wrap: WriteStream) {
        this.wrappedStream = wrap
    }
 
    isTTY = true
 
    private allWrites: string[] = []
 
    replay() {
        console.log(this.allWrites.join(""))
    }
 
    write(buffer: string) {
        this.allWrites.push(buffer)
        return this.wrappedStream.write(buffer)
    }
 
    cursorTo(index: number) {
        return this.wrappedStream.cursorTo(index)
    }
 
    clearLine(direction: 1) {
        return this.wrappedStream.clearLine(direction)
    }
 
    get columns() {
        return this.wrappedStream.columns
    }
}