import Path from 'path' ; import globby from 'globby' ; import normalize from 'normalize-path' ; import { parseEntries , dependenciesParseStrategy } from '@kbn/babel-code-parser' ; async function getDependencies ( cwd , entries ) { return Object . keys ( await parseEntries ( cwd , entries , dependenciesParseStrategy , { } ) ) ; } export async function findUsedDependencies ( listedPkgDependencies , baseDir ) { const mainCodeEntries = await globby ( normalize ( Path . resolve ( baseDir , `src/cli*/dist.js` ) ) ) ; const discoveredPluginEntries = await globby ( [ normalize ( Path . resolve ( baseDir , `src/plugins/**/server/index.js` ) ) , `!${ normalize ( Path . resolve ( baseDir , `/src/plugins/**/public` ) ) }` , normalize ( Path . resolve ( baseDir , `x-pack/plugins/**/server/index.js` ) ) , `!${ normalize ( Path . resolve ( baseDir , `/x-pack/plugins/**/public` ) ) }` , ] ) ; const dynamicRequiredEntries = await globby ( [ normalize ( Path . resolve ( baseDir , 'src/plugins/vis_types/timelion/server/**/*.js' ) ) , ] ) ; const serverEntries = [ ... mainCodeEntries , ... discoveredPluginEntries , ... dynamicRequiredEntries ] ; const serverDependencies = await getDependencies ( baseDir , serverEntries ) ; const hardCodedDependencies = [ '@kbn/ui-framework' ] ; const whiteListedModules = [ ... serverDependencies , ... hardCodedDependencies ] ; const listedDependencies = Object . keys ( listedPkgDependencies ) ; const filteredListedDependencies = listedDependencies . filter ( ( entry ) => { return whiteListedModules . some ( ( nonEntry ) => entry . includes ( nonEntry ) ) ; } ) ; return filteredListedDependencies . reduce ( ( foundUsedDeps , usedDep ) => { if ( listedPkgDependencies [ usedDep ] ) { foundUsedDeps [ usedDep ] = listedPkgDependencies [ usedDep ] ; } return foundUsedDeps ; } , { } ) ; }