1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | var logger_diagnostics_1 = require("../logger/logger-diagnostics");
|
4 | var path = require("path");
|
5 | var tinylr = require("tiny-lr");
|
6 | var events = require("../util/events");
|
7 | function createLiveReloadServer(config) {
|
8 | var liveReloadServer = tinylr();
|
9 | liveReloadServer.listen(config.liveReloadPort, config.host);
|
10 | function fileChange(changedFiles) {
|
11 |
|
12 |
|
13 | if (!logger_diagnostics_1.hasDiagnostics(config.buildDir)) {
|
14 | liveReloadServer.changed({
|
15 | body: {
|
16 | files: changedFiles.map(function (changedFile) { return '/' + path.relative(config.wwwDir, changedFile.filePath); })
|
17 | }
|
18 | });
|
19 | }
|
20 | }
|
21 | events.on(events.EventType.FileChange, fileChange);
|
22 | events.on(events.EventType.ReloadApp, function () {
|
23 | fileChange([{ event: 'change', ext: '.html', filePath: 'index.html' }]);
|
24 | });
|
25 | }
|
26 | exports.createLiveReloadServer = createLiveReloadServer;
|
27 | function injectLiveReloadScript(content, host, port) {
|
28 | var contentStr = content.toString();
|
29 | var liveReloadScript = getLiveReloadScript(host, port);
|
30 | if (contentStr.indexOf('/livereload.js') > -1) {
|
31 |
|
32 | return content;
|
33 | }
|
34 | var match = contentStr.match(/<\/body>(?![\s\S]*<\/body>)/i);
|
35 | if (!match) {
|
36 | match = contentStr.match(/<\/html>(?![\s\S]*<\/html>)/i);
|
37 | }
|
38 | if (match) {
|
39 | contentStr = contentStr.replace(match[0], liveReloadScript + "\n" + match[0]);
|
40 | }
|
41 | else {
|
42 | contentStr += liveReloadScript;
|
43 | }
|
44 | return contentStr;
|
45 | }
|
46 | exports.injectLiveReloadScript = injectLiveReloadScript;
|
47 | function getLiveReloadScript(host, port) {
|
48 | var src = "//" + host + ":" + port + "/livereload.js?snipver=1";
|
49 | return " <!-- Ionic Dev Server: Injected LiveReload Script -->\n <script src=\"" + src + "\" async=\"\" defer=\"\"></script>";
|
50 | }
|