import * as chalk from 'chalk' ; import * as pluralize from 'pluralize' ; import { render as inlineTableRenderer } from './inlinetable' ; import { isMatchingVersion } from '../deps' ; export const render = ( depMap , cliOpts ) => { const filteredMatching = { deps : filterMatchingDependencies ( depMap . deps ) , devDeps : filterMatchingDependencies ( depMap . devDeps ) } ; const filteredUnlocked = { deps : filterLockedDependencies ( depMap . deps ) , devDeps : filterLockedDependencies ( depMap . devDeps ) } ; const hasMismatches = filteredMatching . deps . length > 0 || filteredMatching . devDeps . length > 0 ; const hasUnlocked = filteredUnlocked . deps . length > 0 || filteredUnlocked . devDeps . length > 0 ; if ( ! cliOpts . strict && ! hasMismatches ) { sendSuccessResponse ( depMap , ' NPM Vet: No mismatched package versions ' ) ; } if ( cliOpts . strict && ! hasUnlocked ) { sendSuccessResponse ( depMap , ' NPM Vet: No unlocked packages or mismatched package versions ' ) ; } const mismatchingLength = filteredMatching . deps . length + filteredMatching . devDeps . length ; const unlockedLength = filteredUnlocked . deps . length + filteredUnlocked . devDeps . length ; const phrase = mismatchingLength > 1 ? pluralize ( 'package' ) : 'package' ; if ( mismatchingLength > 0 ) { sendErrorResponse ( filteredMatching , ` NPM Vet: You have ${ mismatchingLength } ${ phrase } ` + `with mismatched versions ` ) ; } sendErrorResponse ( filteredUnlocked , ` NPM Vet: You have ${ unlockedLength } ${ phrase } ` + `that are currently unlocked versions ` ) ; } ; param { IPackageDescriptorMap } depMap param { string } message let sendSuccessResponse = ( depMap , message ) => { console . log ( chalk . bold . bgGreen ( message ) ) ; inlineTableRenderer ( depMap ) ; process . exit ( 0 ) ; } ; param { IPackageDescriptorMap } filteredMatching param { string } message let sendErrorResponse = ( filteredMatching , message ) => { console . log ( chalk . bold . bgRed ( message ) ) ; inlineTableRenderer ( filteredMatching ) ; process . exit ( 1 ) ; } ; param { IPackageDescriptor [ ] } deps returns let filterMatchingDependencies = ( deps ) : IPackageDescriptor [ ] => deps . filter ( d => ! isMatchingVersion ( d . parsedDefinedVersion , d . installedVersion ) ) ; param { IPackageDescriptor [ ] } deps returns let filterLockedDependencies = ( deps ) : IPackageDescriptor [ ] => deps . filter ( d => ! d . locked ) ;