Created
January 14, 2020 20:03
-
-
Save akatakritos/c2b5371bfec542d42668753dee2f41fc to your computer and use it in GitHub Desktop.
angular.js injectables and dependencies
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function getInjectables(appName) { | |
const queue = (angular.module(appName) as any)._invokeQueue; | |
const injectables = []; | |
function injectable(type, name, deps) { | |
return { name, type, deps, depsCount: deps.length, depsJoin: deps.join() }; | |
} | |
queue.forEach(q => { | |
if (q[0] === '$controllerProvider' && q[1] === 'register') { | |
const name = q[2][0]; | |
const deps = q[2][1].$inject || []; | |
injectables.push(injectable('controller', name, deps)); | |
} else if (q[0] === '$compileProvider' && q[1] === 'component') { | |
const name = q[2][0]; | |
const deps = q[2][1].controller.$inject || []; | |
injectables.push(injectable('component', name, deps)); | |
} else if (q[1] === 'factory' || q[1] === 'service') { | |
const name = q[2][0]; | |
const deps = q[2][1].$inject || []; | |
injectables.push(injectable(q[1], name, deps)); | |
} | |
}); | |
return injectables; | |
} | |
const injectables = getInjectables('primaryCareApp'); | |
console.table(injectables); | |
const csv = injectables.map(s => `"${s.name}","${s.type}","${s.depsCount}","${s.depsJoin}"\r\n`).join(''); | |
console.log(csv); | |
let graph = 'digraph { node [shape = doublecircle]; q http timeout rootScope location;\r\nnode [shape = circle];\r\n'; | |
function filterName(name) { | |
return name.replace('$', ''); | |
} | |
injectables.forEach(s => { | |
s.deps.forEach(d => { | |
graph += `${filterName(s.name)} -> ${filterName(d)}; \r\n`; | |
}); | |
}); | |
graph += '}'; | |
console.log(graph); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment