import * as fs from "fs" ; import * as yaml from "js-yaml" ; import * as path from "path" ; import { IListener , ISerializedRequest , ISerializedResponse , } from "unmock-core" ; import { resolveUnmockRootDirectory } from "../utils" ; const fileSizeLimitOnInit = 5 * 1024 ** 2 ; export default class FSLogger implements IListener { private static toIndentedYaml ( input ) { return yaml . dump ( input ) . split ( "\n" ) . map ( ( line ) => `\t${ line . replace ( "!<tag:yaml.org,2002:js/undefined> ''" , "" ) }` ) . join ( "\n" ) ; } private readonly enabled : boolean ; private targetFile ? : string ; constructor ( { directory , filename = "unmock.log" , } ) { const absPath = directory || resolveUnmockRootDirectory ( ) ; if ( ! ( fs . existsSync ( absPath ) && fs . statSync ( absPath ) . isDirectory ( ) ) ) { this . enabled = false ; return ; } this . enabled = true ; this . targetFile = path . join ( absPath , filename ) ; if ( ! fs . existsSync ( this . targetFile ) || fs . statSync ( this . targetFile ) . size > fileSizeLimitOnInit ) { fs . writeFileSync ( this . targetFile , "### Request-Response log generated by unmock\n\n" ) ; } } public notify ( { req , res , } ) { if ( ! this . enabled ) { return ; } fs . appendFileSync ( this . targetFile ! , `[${ new Date ( ) . toISOString ( ) }]:\n\n` + `Intercepted request:\n${ FSLogger . toIndentedYaml ( req ) }\n\n\n` + ( res ? `Generated response:\n${ FSLogger . toIndentedYaml ( res ) }` : "No matching response" ) + `\n\n=============================================\n\n` ) ; } }