* Function is responsible for calling all registered Booter classes that * are bound to the Application instance. Each phase of an instance must * complete before the next phase is started. * * @param execOptions - Execution options for boot. These * determine the phases and booters t
(
execOptions?: BootExecutionOptions,
ctx?: Context,
)
| 66 | * may maintain state. |
| 67 | */ |
| 68 | async boot( |
| 69 | execOptions?: BootExecutionOptions, |
| 70 | ctx?: Context, |
| 71 | ): Promise<Context> { |
| 72 | const bootCtx = ctx ?? new Context(this.app); |
| 73 | |
| 74 | // Bind booters passed in as a part of BootOptions |
| 75 | // We use _bindBooter so this Class can be used without the Mixin |
| 76 | if (execOptions?.booters) { |
| 77 | execOptions.booters.forEach(booter => bindBooter(this.app, booter)); |
| 78 | } |
| 79 | |
| 80 | // Determine the phases to be run. If a user set a phases filter, those |
| 81 | // are selected otherwise we run the default phases (BOOTER_PHASES). |
| 82 | const phases = execOptions?.filter?.phases ?? BOOTER_PHASES; |
| 83 | |
| 84 | // Find booters registered to the BOOTERS_TAG by getting the bindings |
| 85 | const bindings = bootCtx.findByTag(BootTags.BOOTER); |
| 86 | |
| 87 | // Prefix length. +1 because of `.` => 'booters.' |
| 88 | const prefixLength = BootBindings.BOOTERS.length + 1; |
| 89 | |
| 90 | // Names of all registered booters. |
| 91 | const defaultBooterNames = bindings.map(binding => |
| 92 | binding.key.slice(prefixLength), |
| 93 | ); |
| 94 | |
| 95 | // Determining the booters to be run. If a user set a booters filter (class |
| 96 | // names of booters that should be run), that is the value, otherwise it |
| 97 | // is all the registered booters by default. |
| 98 | const names = execOptions |
| 99 | ? execOptions.filter?.booters |
| 100 | ? execOptions.filter.booters |
| 101 | : defaultBooterNames |
| 102 | : defaultBooterNames; |
| 103 | |
| 104 | // Filter bindings by names |
| 105 | const filteredBindings = bindings.filter(binding => |
| 106 | names.includes(binding.key.slice(prefixLength)), |
| 107 | ); |
| 108 | |
| 109 | // Resolve Booter Instances |
| 110 | const booterInsts = await resolveList(filteredBindings, binding => |
| 111 | // We cannot use Booter interface here because "filter.booters" |
| 112 | // allows arbitrary string values, not only the phases defined |
| 113 | // by Booter interface |
| 114 | bootCtx.get<{[phase: string]: () => Promise<void>}>(binding.key), |
| 115 | ); |
| 116 | |
| 117 | // Run phases of booters |
| 118 | for (const phase of phases) { |
| 119 | for (const inst of booterInsts) { |
| 120 | const instName = inst.constructor.name; |
| 121 | if (inst[phase]) { |
| 122 | debug(`${instName} phase: ${phase} starting.`); |
| 123 | await inst[phase](); |
| 124 | debug(`${instName} phase: ${phase} complete.`); |
| 125 | } else { |
nothing calls this directly
no test coverage detected