UNPKG

880 BPlain TextView Raw
1/**
2 * Finds the host app under which an addon is running.
3 *
4 * This allows us to easily look up the host app from a nested addon (an addon
5 * running under another addon).
6 */
7// eslint-disable-next-line @typescript-eslint/no-explicit-any
8function findHost(addon: any): any {
9 // If the addon has the _findHost() method (in ember-cli >= 2.7.0), we'll just
10 // use that.
11 if (typeof addon._findHost === 'function') {
12 return addon._findHost();
13 }
14
15 // Otherwise, we'll use this implementation borrowed from the _findHost()
16 // method in ember-cli.
17 let current = addon;
18 let app;
19
20 // Keep iterating upward until we don't have a grandparent.
21 // Has to do this grandparent check because at some point we hit the project.
22 do {
23 app = current.app || app;
24 } while (current.parent.parent && (current = current.parent));
25
26 return app;
27}
28
29export default findHost;