🌳
pt0/deployF/ptDeployActions/reduceRelMadgeHF.mts
2import * as _ from 'lodash-es'
4type MadgeH = Record<string, string[]>
6export const getDirectImports = ({madgeH, relPath}: {madgeH: MadgeH, relPath: string}) => {
7 const ret = new Set()
8 _.each(madgeH, (childPathA, parPath) => {
9 if (!_.includes(childPathA, relPath)) return
10 ret.add(parPath)
11 })
12 return ret
15export const reduceMadgeRelH = ({madgeH, relPath, importDepth=Infinity}: {madgeH: MadgeH, relPath: string, importDepth?: number}) => {
16 const visited = new Set()
17 const relevant = new Set()
19 const findImporters = (path: string, depth: number) => {
20 if (visited.has(path) || depth > importDepth) return
21 visited.add(path)
23 _.forEach(madgeH, (imports, importer) => {
24 if (_.includes(imports, path)) {
25 relevant.add(importer)
26 findImporters(importer, depth + 1)
27 }
28 })
29 }
31 findImporters(relPath, 0)
33 return _.mapValues(
34 _.pickBy(madgeH, (imports, path) => relevant.has(path)),
35 imports => _.filter(imports, imp => relevant.has(imp) || imp === relPath)
36 )