import { join } from 'path' ; import { dir } from 'tmp-promise' ; import { outputFile , remove } from 'fs-extra' ; import { stdin } from 'mock-stdin' ; import { isTestEnvAndShouldNotPrompt } from '@app-config/logging' ; import { isWindows } from '@app-config/utils' ; type JoinDir = ( filename : string ) => string ; export async function withTempFiles ( files , callback ) ; export async function withTempFiles ( files , callback ) ; export async function withTempFiles ( files , callback ) { const { path : folder } = await dir ( ) ; try { const entries = Array . isArray ( files ) ? files : Object . entries ( files ) ; for ( const [ filename , contents ] of entries ) { await outputFile ( join ( folder , filename ) , contents ) ; } await callback ( ( filename ) => join ( folder , filename ) , folder ) ; } finally { await remove ( folder ) . catch ( ( error ) => { console . warn ( error ) ; if ( isWindows && error . code !== 'EBUSY' ) { throw error ; } } ) ; } } export async function mockedStdin ( callback ) { const mock = stdin ( ) ; isTestEnvAndShouldNotPrompt ( false ) ; process . stdin . isTTY = true ; process . stdout . isTTY = true ; const origWrite = process . stdout . write ; const mockWrite = jest . fn ( ) ; process . stdout . write = mockWrite ; try { const send = ( text ) => new Promise < void > ( ( resolve ) => { setTimeout ( ( ) => { mock . send ( text ) ; mock . send ( '\n' ) ; resolve ( ) ; } , 0 ) ; } ) ; await callback ( send , ( ) => mock . end ( ) ) ; } finally { isTestEnvAndShouldNotPrompt ( true ) ; process . stdin . isTTY = false ; process . stdout . isTTY = false ; process . stdout . write = origWrite ; mock . restore ( ) ; } }