forked from emberjs/ember-inspector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeneral-debug.js
129 lines (118 loc) · 3.14 KB
/
general-debug.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/* eslint no-empty:0 */
import DebugPort from './debug-port';
import Ember from 'ember-debug/utils/ember';
/**
* Class that handles gathering general information of the inspected app.
* ex:
* - Determines if the app was booted
* - Gathers the libraries. Found in the info tab of the inspector.
* - Gathers ember-cli configuration information from the meta tags.
*
* @module ember-debug/general-debug
*/
export default class extends DebugPort {
/**
* Fetches the ember-cli configuration info and sets them on
* the `emberCliConfig` property.
*/
getAppConfig() {
let found = findMetaTag('name', /environment$/);
if (found) {
try {
return JSON.parse(unescape(found.getAttribute('content')));
} catch (e) {}
}
}
/**
* Passed on creation.
*
* @type {EmberDebug}
*/
/**
* Set on creation.
* Contains ember-cli configuration info.
*
* Info used to determine the file paths of an ember-cli app.
*
* @return {Object}
* {String} environment ex: 'development'
* {String} modulePrefix ex: 'my-app'
* {String} podModulePrefix ex: 'my-app/pods'
* {Boolean} usePodsByDefault
*/
emberCliConfig = this.getAppConfig();
/**
* Sends a reply back indicating if the app has been booted.
*
* `__inspector__booted` is a property set on the application instance
* when the ember-debug is inserted into the target app.
* see: startup-wrapper.
*/
sendBooted() {
this.sendMessage('applicationBooted', {
booted: this.namespace.owner.__inspector__booted,
});
}
/**
* Sends a reply back indicating that ember-debug has been reset.
* We need to reset ember-debug to remove state between tests.
*/
sendReset() {
this.sendMessage('reset');
}
static {
/**
* Used by the DebugPort
*
* @type {String}
*/
this.prototype.portNamespace = 'general';
this.prototype.messages = {
/**
* Called from the inspector to check if the inspected app has been booted.
*/
applicationBooted() {
this.sendBooted();
},
/**
* Called from the inspector to fetch the libraries that are displayed in
* the info tab.
*/
getLibraries() {
this.sendMessage('libraries', {
libraries: Ember.libraries?._registry,
});
},
getEmberCliConfig() {
this.sendMessage('emberCliConfig', {
emberCliConfig: this.emberCliConfig,
});
},
/**
* Called from the inspector to refresh the inspected app.
* Used in case the inspector was opened late and therefore missed capturing
* all info.
*/
refresh() {
window.location.reload();
},
};
}
}
/**
* Finds a meta tag by searching through a certain meta attribute.
*
* @param {String} attribute
* @param {RegExp} regExp
* @return {Element}
*/
function findMetaTag(attribute, regExp = /.*/) {
let metas = document.querySelectorAll(`meta[${attribute}]`);
for (let i = 0; i < metas.length; i++) {
let match = metas[i].getAttribute(attribute).match(regExp);
if (match) {
return metas[i];
}
}
return null;
}