import { load } from 'js-yaml' ; import { DartDatasource } from '../../datasource/dart' ; import { logger } from '../../logger' ; import type { PackageDependency , PackageFile } from '../types' ; function getDeps ( depsObj , preset ) { if ( ! depsObj ) { return [ ] ; } return Object . keys ( depsObj ) . reduce ( ( acc , depName ) => { if ( depName === 'meta' ) { return acc ; } const section = depsObj [ depName ] ; let currentValue = null ; if ( section ?. version ) { currentValue = section . version . toString ( ) ; } else if ( section ) { if ( typeof section === 'string' ) { currentValue = section ; } if ( typeof section === 'number' ) { currentValue = section . toString ( ) ; } } const dep = { ... preset , depName , currentValue } ; return [ ... acc , dep ] ; } , [ ] ) ; } export function extractPackageFile ( content , packageFile ) { try { const doc = load ( content , { json : true } ) as any ; const deps = [ ... getDeps ( doc . dependencies , { depType : 'dependencies' , } ) , ... getDeps ( doc . dev_dependencies , { depType : 'dev_dependencies' , } ) , ] ; if ( deps . length ) { return { packageFile , datasource : DartDatasource . id , deps , } ; } } catch ( e ) { logger . debug ( { packageFile } , 'Can not parse dependency' ) ; } return null ; }