-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrefresh.js
183 lines (159 loc) · 4.55 KB
/
refresh.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/**
* Main RefreshJS Node server. Includes - Watchr/Router.
* @author Prajwalit Bhopale <[email protected]>
* @created Dec 1, 2012
* @module refresh
* @requires connect
*/
var http = require ("http");
var fs = require ("fs");
// There's small a problem with watchr. So copying it for now.
var chokidar = require ("chokidar");
var app = require ("connect") ();
var qs = require ("querystring");
var connection, watcher, settings, domains;
// Routes
app.use ("/static/", function (request, response) {
var url = request.url.split ("?") [0];
var type = url.split (".");
type = type [type.length - 1];
switch (type) {
case "css":
response.setHeader ("Content-Type", "text/css");
break;
case "js":
response.setHeader ("Content-Type", "application/javascript");
break;
case "png":
case "jpg":
case "jpeg":
case "gif":
response.setHeader ("Content-Type", "image/" + type);
break;
}
response.end (fs.readFileSync ("resources/static" + url));
});
// SSE path
app.use ("/watch/", function (request, response) {
response.writeHead (200, {
"content-type" : "text/event-stream",
"cache-control": "no-cache",
"connection" : "keep-alive"
});
// @TODO Handle multiple connections
connection = response;
request.on ("close", function () {
connection = null;
});
});
// iframe in background page of chrome-extension
app.use ("/background/", function(request, response) {
response.setHeader ("Content-Type", "text/html");
response.end (fs.readFileSync ("templates/background.html"));
});
// Settings XHR
app.use ("/settings/", function(request, response) {
if (request.method === "GET") {
response.setHeader ("Content-Type", "application/json");
response.end (fs.readFileSync ("settings.json"));
} else if (request.method === "POST") {
response.setHeader ("Content-Type", "application/json");
response.end ("{success:true}");
var body = "";
request.on ("data", function (data) {
body += data;
});
request.on ("end", function () {
fs.writeFile ("settings.json", qs.parse (body).settings);
settings = JSON.parse (qs.parse (body).settings);
updateWatcher (true);
});
}
});
// Configurator home
app.use ("/", function(request, response) {
response.setHeader ("Content-Type", "text/html");
response.end (fs.readFileSync ("templates/index.html"));
});
function fileUpdateHandler (filePath) {
var fileType = filePath.split (".") [1];
var fileName = filePath.split ("/");
fileName = fileName [fileName.length - 1];
// console.log ("File changed: " + fileName);
var message = JSON.stringify ({
type: fileType,
name: fileName
});
sendUpdate ("file-update", message);
};
// File Watcher
function updateWatcher (updateDomains) {
var i;
if (watcher) {
watcher.close ();
}
var paths = [], projs = settings.projects;
for (i=0; i<projs.length; i+=1) {
var p = projs [i];
if (p.css) {
paths.push (p.css);
}
if (p.images) {
paths.push (p.images);
}
if (p.js) {
paths.push (p.js);
}
if (p.fonts) {
paths.push (p.fonts);
}
}
if (paths.length) {
console.log ("Watching:\n" + paths.join (",\n"));
watcher = chokidar.watch (paths, {ignored: /^\./, persistent: true});
watcher.on ("add", fileUpdateHandler);
watcher.on ("change", fileUpdateHandler);
}
domains = [];
for (i=0; i<settings.projects.length; i+=1) {
domains = domains.concat (settings.projects [i].domains);
}
if (updateDomains) {
sendUpdate ("domains-update", JSON.stringify (domains));
}
}
// Create Settings File if not already present
if (!fs.existsSync ("settings.json")) {
fs.readFile ("defaultSettings.json", function (err, content) {
fs.writeFile ("settings.json", content, function () {
console.log ("Settings file updated");
settings = JSON.parse (fs.readFileSync ("settings.json"));
updateWatcher ();
});
});
} else {
settings = JSON.parse (fs.readFileSync ("settings.json"));
updateWatcher ();
}
// Send Server Side Event (EventSource)
function sendUpdate (event, message) {
if (!connection) {
return;
}
var data = "";
if (event) {
data += "event: " + event + "\n";
}
if (message) {
data += "data: " + message + "\n";
}
data += "\n"; // final part of message
connection.write (data);
}
// After server starts, tell all content-scripts which domains to watch.
setTimeout (function () {
sendUpdate ("domains-update", JSON.stringify (domains));
}, 2500);
// Lets Go!
app.listen (7725);
console.log ("Cool! Now go to - http://localhost:7725");