import * as os from 'os' ; import { writeSync } from 'fs' ; const __stdout = process . stdout ; const __eol = os . EOL ; export interface Writer { write ( ... data ) : void ; writeEOL ( ) : void ; writeln ( ... data ) : void ; } export class StdoutWriter implements Writer { public constructor ( ) { } write ( ... data ) { for ( let chunk of data ) { __stdout . write ( chunk ) ; } } writeEOL ( ) { __stdout . write ( __eol ) ; } writeln ( ... data ) { for ( let chunk of data ) { __stdout . write ( chunk ) ; } __stdout . write ( __eol ) ; } } export class FileWriter implements Writer { public constructor ( private fd ) { } write ( ... data ) { if ( data . length === 0 ) { return ; } for ( let chunk of data ) { this . writeBuffer ( Buffer . from ( chunk , 'utf8' ) ) ; } } writeEOL ( ) { this . writeBuffer ( Buffer . from ( __eol , 'utf8' ) ) ; } writeln ( ... data ) { if ( data . length === 0 ) { this . writeEOL ( ) ; return ; } for ( let chunk of data ) { this . writeBuffer ( Buffer . from ( chunk , 'utf8' ) ) ; } this . writeEOL ( ) ; } private writeBuffer ( buffer ) { let offset = 0 ; while ( offset < buffer . length ) { offset += writeSync ( this . fd , buffer , offset ) ; } } }