import { Command } from 'commander' ; import { exec } from 'child_process' ; import { AuditLevel , CommandOptions } from 'src/types' ; import handleInput from './src/handlers/handleInput' ; import handleFinish from './src/handlers/handleFinish' ; import packageJson from './package.json' ; const MAX_BUFFER_SIZE = 1024 * 1000 * 50 ; const program = new Command ( ) ; param { String } auditCommand param { String } auditLevel param { Array } exceptionIds export function callback ( auditCommand , auditLevel , exceptionIds ) { const audit = exec ( `${ auditCommand } --json` , { maxBuffer : MAX_BUFFER_SIZE } ) ; let jsonBuffer = '' ; if ( audit . stdout ) { audit . stdout . on ( 'data' , ( data ) => ( jsonBuffer += data ) ) ; } if ( audit . stderr ) { audit . stderr . on ( 'close' , ( ) => handleFinish ( jsonBuffer , auditLevel , exceptionIds ) ) ; audit . stderr . on ( 'data' , console . error ) ; } } program . name ( packageJson . name ) . version ( packageJson . version ) ; program . command ( 'audit' ) . description ( 'execute npm audit' ) . option ( '-x, --exclude <ids>' , 'Exceptions or the vulnerabilities ID(s) to exclude.' ) . option ( '-l, --level <auditLevel>' , 'The minimum audit level to validate.' ) . option ( '-p, --production' , 'Skip checking the devDependencies.' ) . option ( '-r, --registry <url>' , 'The npm registry url to use.' ) . action ( ( options ) => handleInput ( options , callback ) ) ; program . parse ( process . argv ) ;