-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
95 lines (83 loc) · 2.04 KB
/
index.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
'use strict';
//
// Require modules.
//
var fs = require('fs')
, path = require('path')
, notify = require('chokidar')
, Mongo = require('./lib/mongo')
, mongo = new Mongo
, content = path.join(__dirname, 'node_modules', 'ghost', 'content');
//
// Constructor for Ghost blog and syncing of files
//
function Ghost() {
var ghost = this;
//
// Setup and sync.
//
require('./lib/pre-startup').setup(function done(err) {
if (err) throw err;
//
// Start Ghost blog.
//
ghost.blog = require('ghost')();
//
// Post Startup
//
require('./lib/post-startup').setup(function done(err) {
//
// Wait two second, to prevent premature triggering, before listening
// for file changes in eiter sqlite or content, ignore journaling.
//
setTimeout(function defer() {
ghost.watcher = notify.watch(content, { ignored: /README\.md|\/themes\/|\.git|\.db-journal$/ })
.on('change', ghost.change)
.on('add', ghost.change)
.on('unlink', ghost.unlink)
.on('err', console.error);
}, 2000);
});
});
}
/**
* Catch changes in content directory.
*
* @param {String} full
* @api public
*/
Ghost.prototype.change = function change(full) {
var rel = relative(full);
fs.readFile(full, function read(err, content) {
if (err) return console.error('Could not read: ' + relative);
mongo.store(rel, content, function done(err) {
if (err) return console.log(err);
console.log('Sync complete: ' + rel.green + ' - added || changed');
});
});
};
/**
* Check for removal of content.
* TODO: not yet implemented in ghost.
*
* @param {String} full
* @api public
*/
Ghost.prototype.unlink = function unlink(full) {
var rel = relative(full);
console.log('Sync complete: ' + rel.red + ' - removed');
};
/**
* Create local path from full.
*
* @
* @returns {String} relative path
* @api private
*/
function relative(full) {
return full.replace(content, '');
}
//
// Expose constructed instance.
//
module.exports = new Ghost;