import mm from "micromatch" ; import { analyze , getStats } from "../lib" ; import { vlidateStatsJson } from "../lib/validate" ; import { log , invalidStatsJson } from "../lib/console/messages" ; import { normalizeStats } from "../lib/normalize-stats" ; import { reporter as defaultReporter } from "../lib/reporter" ; import { createProgressBar } from "../lib/console/progress-bar" ; import { sortModules } from "./common/sort-modules" ; param { string [ ] } $inputs param { number } limit param { boolean } filesOnly param { boolean } modulesOnly param { boolean } directoryOnly param { boolean } transitiveOnly param { boolean } duplicatesOnly param { string } ignore param { string } sortBy usage example example example example export default async function defaultCommand ( $inputs , limit = 20 , filesOnly ? , modulesOnly ? , directOnly ? , transitiveOnly ? , duplicatesOnly ? , ignore ? , sortBy ? ) { const start = Date . now ( ) ; const [ statsFilePath , pattern ] = $inputs ; const updateProgressBar = createProgressBar ( ) ; const stats = normalizeStats ( await getStats ( statsFilePath ) ) ; if ( ! vlidateStatsJson ( stats . modules ) ) { log ( invalidStatsJson ( statsFilePath ) ) ; process . exit ( 1 ) ; } const ignorePatterns = ignore ? ignore . split ( "," ) : [ ] ; const report = analyze ( stats , ignorePatterns , updateProgressBar ) ; const format = ( str ) => str . replace ( /^\.\// , "" ) . replace ( / \+ \d+ modules$/ , "" ) ; const modules = report . modules . filter ( ( module ) => { if ( pattern && mm . isMatch ( module . name , pattern , { format } ) ) { return true ; } else if ( pattern ) { return false ; } if ( filesOnly && module . type !== "file" ) return false ; if ( modulesOnly && module . type !== "module" ) return false ; if ( directOnly && module . depsType !== "direct" ) return false ; if ( transitiveOnly && module . depsType !== "transitive" ) return false ; if ( duplicatesOnly && ( module . locations || [ ] ) . length < 2 ) return false ; return true ; } ) ; const updatedLimit = pattern ? 0 : limit >= 0 ? limit : 20 ; const [ sortKey , sortOrder ] = ( sortBy || "" ) . split ( ":" ) ; sortModules ( modules , sortKey , sortOrder ) ; defaultReporter . print ( modules , report . chunks , { } , updatedLimit ) ; const timing = ( Date . now ( ) - start ) / 1000 ; const rounded = Math . round ( timing * 100 ) / 100 ; console . log ( `🏁  Done in ${ rounded }s.` ) ; }