import * as commander from 'commander' ; import * as fs from 'fs' ; import readJson from './util/readJson' ; import { Stats } from './types/Stats' ; import { deriveBundleData , diff , generateReport , DiffResults } from './index' ; const packageVersion = require ( '../package' ) . version ; const program = commander . version ( packageVersion ) ; program . command ( 'data <stats>' ) . description ( 'derive bundle data from stats' ) . option ( '-o, --outFile <string>' , 'output file' ) . option ( '-v, --validate' , 'validate module graph' ) . action ( ( statsPath , options ) => { console . log ( 'Deriving bundle data from stats...' ) ; readJson ( statsPath ) . then ( ( stats ) => { const { validate } = options ; const bundleData = deriveBundleData ( stats , { validate } ) ; fs . writeFileSync ( options . outFile , JSON . stringify ( bundleData , null , 2 ) ) ; } ) . catch ( reportError ) ; } ) ; program . command ( 'diff <baseline> <comparison>' ) . description ( 'diff bundles' ) . option ( '-o, --outFile <string>' , 'output file' ) . action ( ( baselinePath , comparisonPath , options ) => { console . log ( 'Diffing bundles...' ) ; Promise . all ( [ readJson ( baselinePath ) , readJson ( comparisonPath ) ] ) . then ( data => { let result = diff ( data [ 0 ] , data [ 1 ] ) ; fs . writeFileSync ( options . outFile , JSON . stringify ( result , null , 2 ) ) ; } ) . catch ( reportError ) ; } ) ; program . command ( 'report <diff>' ) . description ( 'generate a markdown report from a diff' ) . option ( '-o, --outFile <string>' , 'output file' ) . action ( ( diffPath , options ) => { console . log ( 'Generating report...' ) ; readJson ( diffPath ) . then ( ( diff ) => { const markdown = generateReport ( diff ) ; fs . writeFileSync ( options . outFile , markdown ) ; } ) . catch ( reportError ) ; } ) ; program . parse ( process . argv ) ; function reportError ( error ) { console . error ( 'webpack-bundle-diff failed with an error:' , error ) ; }