This repository was archived by the owner on May 1, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 311
Expand file tree
/
Copy pathinjector.ts
More file actions
44 lines (36 loc) · 1.47 KB
/
injector.ts
File metadata and controls
44 lines (36 loc) · 1.47 KB
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
import { getAppScriptsVersion, getSystemText } from '../util/helpers';
import { LOGGER_DIR } from './serve-config';
const LOGGER_HEADER = '<!-- Ionic Dev Server: Injected Logger Script -->';
export function injectNotificationScript(rootDir: string, content: any, notifyOnConsoleLog: boolean, notificationPort: Number): any {
let contentStr = content.toString();
const consoleLogScript = getDevLoggerScript(rootDir, notifyOnConsoleLog, notificationPort);
if (contentStr.indexOf(LOGGER_HEADER) > -1) {
// already added script somehow
return content;
}
let match = contentStr.match(/<head>(?![\s\S]*<head>)/i);
if (!match) {
match = contentStr.match(/<body>(?![\s\S]*<body>)/i);
}
if (match) {
contentStr = contentStr.replace(match[0], `${match[0]}\n${consoleLogScript}`);
} else {
contentStr = consoleLogScript + contentStr;
}
return contentStr;
}
function getDevLoggerScript(rootDir: string, notifyOnConsoleLog: boolean, notificationPort: Number) {
const appScriptsVersion = getAppScriptsVersion();
const ionDevServer = JSON.stringify({
sendConsoleLogs: notifyOnConsoleLog,
wsPort: notificationPort,
appScriptsVersion: appScriptsVersion,
systemInfo: getSystemText(rootDir)
});
return `
${LOGGER_HEADER}
<script>var IonicDevServerConfig=${ionDevServer};</script>
<link href="/${LOGGER_DIR}/ion-dev.css?v=${appScriptsVersion}" rel="stylesheet">
<script src="/${LOGGER_DIR}/ion-dev.js?v=${appScriptsVersion}"></script>
`;
}