import * as fs from 'fs' ; import filenamifyCombined from 'filenamify' ; import { injectable } from 'inversify' ; import normalizePath from 'normalize-path' ; import { ensureDirectory } from 'common' ; const path = require ( 'path' ) ; const filenamifyUrl = require ( 'filenamify-url' ) ; @ injectable ( ) export class OutputFileWriter { constructor ( private readonly fileSystemObj = fs , private readonly pathObj = path , private readonly ensureDirectoryFunc = ensureDirectory ) { } public writeToDirectory ( directory , fileBaseName , fileExtension , content ) { let fileName ; try { fileName = `${ filenamifyUrl ( fileBaseName , { replacement : '_' } ) }.${ fileExtension }` ; } catch { fileName = `${ filenamifyCombined ( fileBaseName , { replacement : '_' } ) }.${ fileExtension }` ; } return this . writeToDirectoryWithPreSanitizedFilename ( directory , fileName , content ) ; } public writeToDirectoryWithOriginalFilename ( directory , originalFilePath , content ) { const originalFileName = this . pathObj . basename ( originalFilePath ) ; return this . writeToDirectoryWithPreSanitizedFilename ( directory , originalFileName , content ) ; } public writeToFile ( filePath , content ) { const normalizedPath = normalizePath ( this . pathObj . resolve ( filePath ) ) ; const dirName = this . pathObj . dirname ( normalizedPath ) ; this . ensureDirectoryFunc ( dirName ) ; this . fileSystemObj . writeFileSync ( normalizedPath , content ) ; return normalizedPath ; } private writeToDirectoryWithPreSanitizedFilename ( directory , fileName , content ) { const normalizedDirectory = this . ensureDirectoryFunc ( directory ) ; const filePath = normalizePath ( this . pathObj . resolve ( normalizedDirectory , fileName ) ) ; this . fileSystemObj . writeFileSync ( filePath , content ) ; return filePath ; } }