-
Notifications
You must be signed in to change notification settings - Fork 0
/
diffwatch.js
443 lines (376 loc) · 12.1 KB
/
diffwatch.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
var util = require('util'),
chokidar = require('chokidar'),
colors = require('colors'),
fs = require('fs'),
path = require('path'),
util = require('util'),
/* classes / static imports */
exec = require('child-process-promise').exec,
spawn = require('child-process-promise').spawn,
EventEmitter = require('events').EventEmitter;
var dw = log = require('./modules/utils'),
localdiff = require('./modules/localdiff');
// local diff module
var diff = localdiff.diff;
// an eventemitter, diffwatch is the module containing the logic for updating the mongodb-compatible tingodb setup by index.js
// and creating the relevant collections for the path.
function diffwatch(opts)
{
this.opts = opts;
this.path = opts.path;
this.paths = this.opts['paths'];
this.db = opts.db;
this.storage = opts.storageDir.toLowerCase();
this.dbPath = opts.dbPath.toLowerCase();
this.project = opts.project;
this.revHistory = opts.revHistory;
this.dbHistory = opts.dbHistory;
// default file ignores
this.ignored = opts.ignored || /[\/\\]\./;
// ready / scanned working dir
this.ready = false;
// current list of revisions made this session
this.revisions = [];
dw.currentDiffwatch = this;
// temp dirs
localdiff.tempDir = opts.tempPath;
localdiff.getDiffPath(function(){
this.start();
}.bind(this));
}
diffwatch.prototype.__proto__ = EventEmitter.prototype;
// important - the class gets exposed here
dw.DiffWatcher = diffwatch;
// start watching
diffwatch.prototype.start = function() {
var watcher = this.watcher = new chokidar.FSWatcher({ignored: this.ignored, persistent: true});
if (this.paths && util.isArray(this.paths))
{
this.paths.forEach(function(path, index){
watcher.add(path);
log.debug('added: '+path+' to watch.');
});
} else if (this.path && this.path.length > 2) {
watcher.add(this.path);
log.debug('added: '+this.path+' to watch.');
}
watcher.on('ready', function() {
util.log('Scanned working directory. ready for changes..');
this.ready = true;
this.check();
this.updateRevisions();
}.bind(this));
}
diffwatch.prototype.getFiles = function(msg, send)
{
send({ msg: 'incoming_files' });
var project = this.project;
project.find({type: 'file'}).toArray(function (err, result) {
send({
msg: 'files',
files: result
})
});
}
diffwatch.prototype.getFile = function(msg, send)
{
send({ msg: 'incoming_file' });
var project = this.project;
project.findOne({ type: 'file', key: msg.key }, function (err, result) {
send({
msg: 'file',
file: result
})
});
}
diffwatch.prototype.getRecent = function(msg, send)
{
send({ msg: 'incoming_recent' });
var project = this.project;
this.updateRevisions();
send({
msg: 'recent',
files: this.revisions2 || this.revisions.slice(-100)
});
}
// command from the client, socket rpc
diffwatch.prototype.clientCmd = function(msg, send)
{
// send the most recent changes, up to limit or default 25
if (msg.cmd == "recent")
{
this.getRecent(msg, send);
}
if (msg.cmd == "file")
{
this.getFile(msg, send);
}
if (msg.cmd == "files")
{
this.getFiles(msg, send);
}
if (msg.cmd == "ping")
send({msg: 'pong'});
}
// update the revisions -- try to get the list and save
diffwatch.prototype.updateRevisions = function(record, cb) {
var project = this.project;
var scope = this;
if (!this._revisionsId)
{
project.findOne({ 'type': 'revisions' }, function(err, item){
if (item)
{
this._revisionsId = item._id;
dw.debug('loaded revisions list: '+this._revisionsId+' count: '+item.recent.length);
this.updateRevisions();
return;
} else if (!item) {
dw.warn('had no revisions list');
if (!record) return;
this.project.insert(
{
'type': 'revisions',
'recent': [ record ]
});
// seems to work
dw.debug('added first revisions record');
} else if (err) {
dw.error('error loading revisions list:', err);
}
}.bind(this));
}
else
{
if (!record)
{
project.findOne({ 'type': 'revisions' }, function(err, item){
if (item && item.recent.length > 1)
{
// test / delete later
dw.debug('loaded revisions list: '+item.recent.length+' entries');
scope.revisions2 = item.recent;
}
if (cb)
cb(item);
}.bind(this));
return;
}
dw.debug('updating revisions list '+this._revisionsId);
project.update({ _id: this._revisionsId }, {
"$set": {
'lastUpdate': new Date().getTime()
}
});
project.update({ _id: this._revisionsId },
{
'$push': { 'recent': { $each: [ record ], $slice: -100 } }
}, function(err, result) {
if (err != null)
{
dw.error('error saving revisions list:', err);
}
});
}
}
var originals = {};
// update block to be called after saving other info
diffwatch.prototype.update = function update(obj, record, oneId) {
// test and again so
dw.debug('updating id '+oneId, 'type of ',typeof(oneId));
dw.debug('update diff length: ', obj.diff.length);
dw.debug('record diff:'+record.diff);
if (record.diff.trim() == '')
{
dw.info('skipping because chokidar doesn\'t work on mac, whole file will be saved as revision.',obj.key);
//record.diff = fs.readFileSync(obj.key);
return;
}
var project = this.project,
scope = this;
// try and update this with set first
project.update({ _id: oneId }, {
"$set": {
previous: obj.text.toString('base64'),
lastUpdate: new Date().getTime()
}
});
// see how that is
project.update(
{ _id: oneId },
{
key: obj.key,
$push: { revisions: record }
},
function(err, result) {
if (err != null) {
dw.error('error saving to db', err);
dw.error(err.stack);
}
// add the last revision onto the session stack.
var revRec = record;
revRec.file = obj.key;
// todo: rewriting
scope.revisions.push(revRec);
this.updateRevisions(revRec);
// check to make sure the revision saved correctly (ensure-write)
project.findOne({ _id: oneId }, function(err, entry){
// check for error
if (err != null)
{
dw.error(err);
dw.error(err.stack);
return;
}
// make sure entry is there / updated test
if (entry != null && entry.revisions != null)
{
// show entry info / test debug
var lastDiff = entry.revisions[entry.revisions.length-1].diff || null;
dw.info("revision history for "+oneId+" contains "+entry.revisions.length+" entries.");
dw.debug('updated diff length: ', lastDiff.length);
dw.debug('last diff: ', lastDiff);
}
else if (entry != null)
{
dw.error('revision history for '+oneId+' null');
}
else
{
dw.error('revision history / entry for '+oneId+' null');
}
}.bind(this));
}.bind(this)); // update callback (first)
}
// start listening to change events (called after ready) ink my whole body i dont give a mothafuck
diffwatch.prototype.check = function() {
// closure / this scope
var scope = this;
var db = scope.db;
this.watcher.on('change', function(file, stats) {
if (file.indexOf('temp') != -1) return;
var name = file.toLowerCase();
if ((name.indexOf('.localdiff') != -1) || (file.indexOf('db') != -1) || (file.indexOf('storage') != -1) || (name.indexOf(this.storage) != -1) || (file.indexOf(name.dbPath) != -1)) {
dw.debug('file updated in internal dirs. ignoring');
return;
}
if (!stats) {
console.log('Error checking file / stat changes: '+file);
}
// auto reload watcher (test)
setTimeout(function() { if (this.wss) this.wss.broadcast({msg: 'refresh-watcher'}); }.bind(this), 1200);
var key = (file+'').toLowerCase().replace(/\W/g, '').toString();
var project = this.project;
// find existing record by key in db
this.project.findOne({ key: key, type: 'file' }, function(err, item){
if (err)
{
dw.error('db', err);
dw.error('stack', err.stack);
dw.info('key:', key);
return;
}
if (item == null) {
dw.debug('no item found for key:', key);
if (originals[key])
{
// if we have the original state of the file
// and it's changed, diff from that
diff(file, originals[key], function(success, obj){
if (success)
{
// create the mew revision record
var record = { timestamp: new Date().getTime(), diff: obj.diff };
project.insert({
key: obj.key,
type: 'file',
path: file,
lastUpdate: new Date().getTime(),
revisions: [ record ],
previous: obj.text.toString('base64'),
});
dw.success('tracking new file '+file);
}
else
{
dw.warn('original diff failed for '+file+" info: "+JSON.stringify(obj));
}
});
return;
}
else
{
dw.info('will track '+file+' on next change (waiting for new version)');
originals[key] = fs.readFileSync(file);
return;
}
dw.error('diff could not find item w/ key:'+key+' for file:'+file+' / did nothing about it.');
return;
}
// updated
if (item != null && item.previous)
{
var oneId = item._id;
dw.debug('existing item key: ', item.key, ' id:', oneId);
//dw.debug('item id:', oneId);
//dw.debug('running diff for '+file+' / key '+item.key);
// file has a latest version stored in the db, so diff against that and save result
// at this point, latest should be the last revision
diff(file, new Buffer(item.previous, 'base64'), function(success, obj){
// update
if (success)
{
// wtf, why wouldn't item b here
dw.debug('got new diff for '+file+' with id '+oneId);
var record = { timestamp: new Date().getTime(), diff: obj.diff };
if (scope.dbHistory)
{
dw.debug('saving history in DB for', oneId)
record.text = obj.text;
}
// if we are using revision history, move the tmp file to storage - then call update, or call update when
// finished.
if (scope.revHistory)
{
// base path (ie folder name without extension)
record.saved_at = path.join(scope.storage, path.basename(file, path.extname(file))+'-revs');
// create the revisions folder
fs.mkdir(record.saved_at,function(e){
if(!e || (e && e.code === 'EEXIST')) {
record.saved_at = path.join(record.saved_at, path.basename(file, path.extname(file))+'('+record.timestamp+')'+path.extname(file));
try {
fs.renameSync(obj.tmp, record.saved_at);
dw.success('saved revision file '+record.saved_at);
scope.update(obj, record, oneId);
} catch (ex) {
dw.error(ex, 'while saving revision file');
// scope.update(obj, record, oneId);
}
}
else
{
dw.error(e);
dw.warn('could not check / use '+record.saved_at+'.. did not save revision file!');
// scope.update(obj, record, oneId);
}
})
}
else
{
// no file based revision history
scope.update(obj, record, oneId);
}
}
else
{
// diff was not successful - why
dw.error('diff unsuccessful - from diff engine: ');
dw.warn(JSON.stringify(obj));
}
}.bind(this));
}
}.bind(this)); /* findOne */
}.bind(this)); /* change */
dw.info('started checking');
}
module.exports = dw;