import * as nodeSloc from "node-sloc" ; import { resolve } from "path" ; import { Options , Path } from "./types" ; import { buildDebugger , withDuration } from "../utils" ; type ComplexityEntry = { path : Path ; complexity : number ; } ; const internal = { debug : buildDebugger ( "complexity" ) } ; export default { compute : ( ... args ) : Promise < Map < Path , number > > => withDuration ( compute , args , internal . debug ) } ; async function compute ( paths , options ) { internal . debug ( `${ paths . length } files to compute complexity on` ) ; const entries = await Promise . all ( paths . map ( async ( path ) => { const complexity = await getComplexity ( path , options ) ; return { path , complexity } ; } ) ) ; const complexityPerPath = entries . reduce ( ( map , entry ) => { map . set ( entry . path , entry . complexity ) ; return map ; } , new Map ( ) ) ; return complexityPerPath ; } async function getComplexity ( path , options ) { const absolutePath = resolve ( options . directory , path ) ; const result = await nodeSloc ( { path : absolutePath } ) ; return result . sloc . sloc || 1 ; }