import { isAbsolute , dirname , resolve , extname } from 'path' ; import { Log } from 'sarif' ; import { existsSync , mkdirpSync , writeFileSync , writeJsonSync } from 'fs-extra' ; import { todayFormat } from '@checkup/core' ; const stripAnsi = require ( 'strip-ansi' ) ; export const DEFAULT_OUTPUT_FILENAME = `checkup-report-${ todayFormat ( ) }` ; param { ( Log | string ) } result param { string } cwd param { string } outputFile return { * } export function writeResultsToFile ( result , cwd , outputFile = DEFAULT_OUTPUT_FILENAME ) { let fileType = typeof result === 'string' ? 'txt' : 'sarif' ; let outputPath = getOutputPath ( cwd , outputFile , fileType ) ; if ( fileType === 'txt' ) { writeFileSync ( outputPath , stripAnsi ( result ) ) ; } else { writeJsonSync ( outputPath , result ) ; } return outputPath ; } export function getOutputPath ( cwd = '' , outputFile , fileType ) { let outputPath = isAbsolute ( outputFile ) ? outputFile : resolve ( cwd , outputFile ? extname ( outputFile ) ? outputFile : `${ outputFile }.${ fileType }` : `${ DEFAULT_OUTPUT_FILENAME }.${ fileType }` ) ; let dir = dirname ( outputPath ) ; if ( ! existsSync ( dir ) ) { mkdirpSync ( dir ) ; } return outputPath ; }