import fs from 'fs-extra' ; import { showGenerate , showCreate , showUpdate , showError } from '../../utils/logger.util' ; import { checkIfDirExistElseMakeDir , checkExistence , fileAlreadyExist } from '../../utils/checker.util' ; import { overwriteFileQuestion } from '../../questions' ; import { Answer } from '../../models/choice' ; export function defaultTemplate ( fileNameWithExt , fileContent , hasPath = false , filePath = '' ) { showGenerate ( fileNameWithExt ) ; checkIfDirExistElseMakeDir ( hasPath , filePath ) ; const fileExists = checkExistence ( `${ filePath }/${ fileNameWithExt }` ) ; if ( ! fileExists ) return createFile ( filePath , fileNameWithExt , fileContent ) ; return overwriteFileOrThrowError ( filePath , fileNameWithExt , fileContent ) ; } function createFile ( filePath , fileName , fileContent , fileAlreadyExists = false ) { const filepath = process . cwd ( ) + `${ filePath }/${ fileName }` ; fs . writeFile ( filepath , fileContent , ( error ) => { if ( ! error && ! fileAlreadyExists ) return showCreate ( fileName , filePath ) ; if ( ! error && fileAlreadyExists ) return showUpdate ( fileName , filePath ) ; return showError ( error ) ; } ) ; } async function overwriteFileOrThrowError ( filePath , fileNameWithExt , fileContent ) { const overwriteAnswer = await overwriteFileQuestion ( ) ; if ( overwriteAnswer . overwrite === true ) return createFile ( filePath , fileNameWithExt , fileContent , true ) ; return fileAlreadyExist ( fileNameWithExt ) ; }