import * as fse from "fs-extra" ; export const alwaysEchoTestLog = /^(true|1)$/i . test ( process . env . ALWAYS_ECHO_TEST_LOG ?? '' ) ; interface ITestLog { writeLine ( message ) : void ; toString ( ) : string ; } class UninitializedLog implements ITestLog { public writeLine ( message ) { createTestLog ( ) ; testLog . writeLine ( message ) ; } public toString ( ) { return "" ; } } class StringTestLog implements ITestLog { private _data : string [ ] = [ ] ; public writeLine ( message ) { message = message === undefined ? '(undefined)' : message ; this . _data . push ( message ) ; } public toString ( ) { return this . _data . join ( ' \n' ) ; } } function writeToLogFile ( message , ... args ) { message = new Date ( ) . toLocaleDateString ( ) + ": " + message + "\n" ; if ( testLogOutputFile ) { fse . appendFileSync ( testLogOutputFile , message ) ; } return message ; } export function writeToWarning ( message ) { message = "** WARNING: " + message ; testLog . writeLine ( message ) ; writeToLogFile ( message ) ; console . warn ( message ) ; } export function writeToError ( message ) { message = "** ERROR: " + message ; testLog . writeLine ( message ) ; writeToLogFile ( message ) ; console . error ( message ) ; } export function writeToLog ( message = "" , echoToConsole = false ) { testLog . writeLine ( message ) ; writeToLogFile ( message ) ; if ( echoToConsole || alwaysEchoTestLog ) { console . log ( `testLog: ${ message }` ) ; } } let testLog = new UninitializedLog ( ) ; let testLogOutputFile ; export function setTestLogOutputFile ( filePath ) { testLogOutputFile = filePath ; writeToLog ( `Writing logs to ${ testLogOutputFile }` ) ; } export function createTestLog ( ) { testLog = new StringTestLog ( ) ; } export function deleteTestLog ( ) { testLog = new UninitializedLog ( ) ; } export function getTestLogContents ( ) { return testLog . toString ( ) ; }