Skip to content

Commit c57f819

Browse files
committed
Added live reloading.
1 parent 67a7537 commit c57f819

2 files changed

Lines changed: 42 additions & 10 deletions

File tree

changes.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
- added: (IMPORTANT) bundles
44
- added: (IMPORTANT) Total.js components can have async delegate
5+
- added: `debugging` supports live reloading
56
- added: new schema operations: `schema.setInsert()` and `schema.setUpdate()`
67
- added: `RESTBuilder.patch([data])`
78
- added: `CONVERT(obj, schema)` for quick converting values like Schema (more in docs.)

debug.js

Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ module.exports = function(opt) {
4242
// options.inspector = 9229;
4343
// options.debugger = 40894;
4444
// options.watch = ['adminer'];
45+
// options.livereload = true;
4546
};
4647

4748
process.on('uncaughtException', e => e.toString().indexOf('ESRCH') == -1 && console.log(e));
@@ -79,13 +80,15 @@ function runwatching() {
7980
const FILENAME = U.getName(process.argv[1]);
8081
const directory = process.cwd();
8182
const VERSION = F.version_header;
82-
const SPEED = 1500;
8383
const REG_CONFIGS = /configs\//g;
8484
const REG_FILES = /config-debug|config-release|config|versions|workflows|sitemap|dependencies|\.js$|\.resource$/i;
8585
const REG_THEMES = /\/themes\//i;
8686
const REG_COMPONENTS = /components\/.*?\.html|\.package\/.*?$/i;
8787
const REG_THEMES_INDEX = /themes(\/|\\)?[a-z0-9_.-]+(\/|\\)?index\.js$/i;
8888
const REG_EXTENSION = /\.(js|resource|package|bundle)$/i;
89+
const REG_RELOAD = /\.(js|css|html|htm|jpg|png|gif|ico|svg|resource)$/i;
90+
const isRELOAD = !!options.livereload;
91+
const SPEED = isRELOAD ? 1000 : 1500;
8992

9093
function copyFile(oldname, newname, callback) {
9194
var writer = Fs.createWriteStream(newname);
@@ -138,35 +141,49 @@ function runwatching() {
138141
var isLoaded = false;
139142
var isSkip = false;
140143
var pidIncrease;
141-
var speed = 4000;
142144
var isBUNDLE = false;
143145
var blacklist = {};
144146
var counter = 0;
147+
var WS = null;
148+
var speed = isRELOAD ? 1000 : 4000;
145149

146150
blacklist['/debug.pid'] = 1;
147151
blacklist['/debug.js'] = 1;
148152
blacklist['/bundle.json'] = 1;
149153
blacklist['/package.json'] = 1;
150154
blacklist['/readme.md'] = 1;
151155

156+
if (isRELOAD) {
157+
F.console = NOOP;
158+
F.websocket('/', function() {
159+
var self = this;
160+
self.autodestroy(function() {
161+
WS = null;
162+
});
163+
WS = self;
164+
});
165+
F.http('release', { port: typeof(options.livereload) === 'number' ? options.livereload : 35729 });
166+
}
167+
152168
try {
153169
Fs.statSync(F.path.root(F.config['directory-bundles']));
154170
isBUNDLE = true;
155171
} catch(e) {}
156172

157-
if (isBUNDLE) {
173+
if (isBUNDLE || isRELOAD) {
158174
directories.push(U.combine(F.config['directory-public']));
159175
directories.push(U.combine(F.config['directory-views']));
160176
}
161177

162178
function onFilter(path, isDirectory) {
163179
if (isBUNDLE)
164180
return isDirectory ? SRC !== path : !blacklist[path.substring(directory.length)];
181+
if (isRELOAD)
182+
return isDirectory ? true : REG_RELOAD.test(path);
165183
return isDirectory && REG_THEMES.test(path) ? REG_THEMES_INDEX.test(path) : isDirectory ? true : REG_EXTENSION.test(path) || REG_COMPONENTS.test(path) || REG_CONFIGS.test(path);
166184
}
167185

168186
function onComplete(f) {
169-
170187
Fs.readdir(directory, function(err, arr) {
171188

172189
var length = arr.length;
@@ -187,9 +204,13 @@ function runwatching() {
187204
});
188205
}
189206

207+
function livereload() {
208+
isRELOAD && setTimeout2('livereload', () => WS && WS.send('reload'), 500);
209+
}
210+
190211
function isViewPublic(filename) {
191212

192-
if (!isBUNDLE)
213+
if (!isBUNDLE && !isRELOAD)
193214
return false;
194215

195216
var fn = filename.substring(directory.length);
@@ -205,6 +226,7 @@ function runwatching() {
205226
}
206227

207228
function refresh() {
229+
var reload = false;
208230
Object.keys(files).wait(function(filename, next) {
209231
Fs.stat(filename, function(err, stat) {
210232

@@ -215,8 +237,11 @@ function runwatching() {
215237
var tmp = isViewPublic(filename);
216238
var log = stamp.replace('#', 'REM') + prefix + normalize(filename.replace(directory, ''));
217239
if (tmp) {
218-
Fs.unlinkSync(Path.join(SRC, tmp));
219-
console.log(log);
240+
if (isBUNDLE) {
241+
Fs.unlinkSync(Path.join(SRC, tmp));
242+
console.log(log);
243+
}
244+
reload = true;
220245
} else {
221246
changes.push(log);
222247
force = true;
@@ -229,9 +254,12 @@ function runwatching() {
229254
if (files[filename]) {
230255
var tmp = isViewPublic(filename);
231256
if (tmp) {
232-
copyFile(filename, Path.join(SRC, tmp));
257+
if (isBUNDLE) {
258+
copyFile(filename, Path.join(SRC, tmp));
259+
console.log(log);
260+
}
233261
files[filename] = ticks;
234-
console.log(log);
262+
reload = true;
235263
next();
236264
return;
237265
}
@@ -249,9 +277,11 @@ function runwatching() {
249277

250278
isLoaded = true;
251279

280+
reload && livereload();
281+
252282
if (status !== 1 || !force) {
253283
if (counter % 150 === 0)
254-
speed = 6000;
284+
speed = isRELOAD ? 3000 : 6000;
255285
setTimeout(refresh_directory, speed);
256286
return;
257287
}
@@ -267,6 +297,7 @@ function runwatching() {
267297

268298
changes = [];
269299
force = false;
300+
livereload();
270301
}, 3);
271302
}
272303

0 commit comments

Comments
 (0)