parent
b955996654
commit
30f30f5083
@ -0,0 +1,47 @@
|
||||
/**
|
||||
* @module libflitter/app/Analyzer
|
||||
*/
|
||||
|
||||
const color = require('colors/safe')
|
||||
|
||||
/**
|
||||
* Analyzes various aspects of the application and its dependencies.
|
||||
*/
|
||||
class Analyzer {
|
||||
constructor(app, units) {
|
||||
/**
|
||||
* The application in question.
|
||||
* @type {module:libflitter/app/FlitterApp~FlitterApp}
|
||||
*/
|
||||
this.app = app
|
||||
|
||||
/**
|
||||
* Collection of unit definitions used by the application.
|
||||
* @type {object}
|
||||
*/
|
||||
this.units = units
|
||||
}
|
||||
|
||||
/**
|
||||
* Check the services and dependencies of each unit and ensure that none request
|
||||
* a service that has not been provided by the time the unit starts.
|
||||
*/
|
||||
check_dependencies() {
|
||||
const available_services = ['app']
|
||||
for ( const UnitClass of Object.values(this.units) ) {
|
||||
for ( const service of UnitClass.services ) {
|
||||
if ( !available_services.includes(service) ) {
|
||||
const message = ` ${color.red('UNIT DEPENDENCY ERROR:')} ${UnitClass.name} depends on ${service}, but it is not registered when the unit tries to start.`
|
||||
console.log(message)
|
||||
console.log(`If the unit depends on a service provided by a later unit, try moving ${UnitClass.name} below that unit.`)
|
||||
throw new Error(message)
|
||||
}
|
||||
}
|
||||
|
||||
available_services.push(UnitClass.name)
|
||||
if ( UnitClass.provides ) UnitClass.provides.forEach(s => available_services.push(s))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = exports = Analyzer
|
Loading…
Reference in new issue