import { promises } from 'fs' ; import { join } from 'path' ; import { unixEOL , codeBlock , ErrorWithCause } from '@sap-cloud-sdk/util' ; import { getCopyrightHeader } from '@sap-cloud-sdk/generator-common/internal' ; const { writeFile } = promises ; param directoryPath param fileName param content param overwrite param withCopyright internal export async function createFile ( directoryPath , fileName , content , overwrite , withCopyright = true ) { try { return await writeFile ( join ( directoryPath , fileName ) , withCopyright ? wrapContent ( content ) : content , { encoding : 'utf8' , flag : overwrite ? 'w' : 'wx' } ) ; } catch ( err ) { const recommendation = err . code === 'EEXIST' && ! overwrite ? ' File already exists. If you want to allow overwriting files, enable the `overwrite` flag.' : '' ; throw new ErrorWithCause ( `Could not write file "${ fileName }".${ recommendation }` , err ) ; } } function wrapContent ( content ) { return ( codeBlock `
${ getCopyrightHeader ( ) }
${ content }
` + unixEOL ) ; }