injector.js 1.7KB

123456789101112131415161718192021222324252627282930313233343536
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. var helpers_1 = require("../util/helpers");
  4. var serve_config_1 = require("./serve-config");
  5. var LOGGER_HEADER = '<!-- Ionic Dev Server: Injected Logger Script -->';
  6. function injectNotificationScript(rootDir, content, notifyOnConsoleLog, notificationPort) {
  7. var contentStr = content.toString();
  8. var consoleLogScript = getDevLoggerScript(rootDir, notifyOnConsoleLog, notificationPort);
  9. if (contentStr.indexOf(LOGGER_HEADER) > -1) {
  10. // already added script somehow
  11. return content;
  12. }
  13. var match = contentStr.match(/<head>(?![\s\S]*<head>)/i);
  14. if (!match) {
  15. match = contentStr.match(/<body>(?![\s\S]*<body>)/i);
  16. }
  17. if (match) {
  18. contentStr = contentStr.replace(match[0], match[0] + "\n" + consoleLogScript);
  19. }
  20. else {
  21. contentStr = consoleLogScript + contentStr;
  22. }
  23. return contentStr;
  24. }
  25. exports.injectNotificationScript = injectNotificationScript;
  26. function getDevLoggerScript(rootDir, notifyOnConsoleLog, notificationPort) {
  27. var appScriptsVersion = helpers_1.getAppScriptsVersion();
  28. var ionDevServer = JSON.stringify({
  29. sendConsoleLogs: notifyOnConsoleLog,
  30. wsPort: notificationPort,
  31. appScriptsVersion: appScriptsVersion,
  32. systemInfo: helpers_1.getSystemText(rootDir)
  33. });
  34. return "\n " + LOGGER_HEADER + "\n <script>var IonicDevServerConfig=" + ionDevServer + ";</script>\n <link href=\"" + serve_config_1.LOGGER_DIR + "/ion-dev.css?v=" + appScriptsVersion + "\" rel=\"stylesheet\">\n <script src=\"" + serve_config_1.LOGGER_DIR + "/ion-dev.js?v=" + appScriptsVersion + "\"></script>\n ";
  35. }