import get from 'lodash.get' ; import { table , TableUserConfig } from 'table' ; import { SecurityReportHeader , ExceptionReportHeader } from 'src/types' ; const SECURITY_REPORT_HEADER = [ 'ID' , 'Module' , 'Title' , 'Paths' , 'Sev.' , 'URL' , 'Ex.' ] ; const EXCEPTION_REPORT_HEADER = [ 'ID' , 'Status' , 'Expiry' , 'Notes' ] ; param { Array } tableData param { Number } columnIndex param { Number } maxWidth param { Number } minWidth return { Number } export function getColumnWidth ( tableData , columnIndex , maxWidth = 50 , minWidth = 15 ) { const contentLength = tableData . reduce ( ( max , cur ) => { let content = JSON . stringify ( get ( cur , columnIndex , '' ) ) ; content = content . replace ( /\\x1b\[\d{1,2}m/g , '' ) ; content = content . replace ( /\\u001b\[\d{1,2}m/g , '' ) ; content = content . replace ( /"/g , '' ) ; return content . length > max ? content . length : max ; } , minWidth ) ; return Math . min ( contentLength , maxWidth ) ; } param { Array } data return { undefined } export function printSecurityReport ( data ) { const configs = { singleLine : true , header : { alignment : 'center' , content : '=== npm audit security report ===\n' , } , columns : { 2 : { width : getColumnWidth ( data , 2 ) , wrapWord : true , } , 3 : { width : getColumnWidth ( data , 3 ) , wrapWord : true , } , } , } ; console . info ( table ( [ SECURITY_REPORT_HEADER , ... data ] , configs ) ) ; } param { Array } data return { undefined } export function printExceptionReport ( data ) { const configs = { singleLine : true , header : { alignment : 'center' , content : '=== list of exceptions ===\n' , } , } ; console . info ( table ( [ EXCEPTION_REPORT_HEADER , ... data ] , configs ) ) ; }