-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathwatcher.js
57 lines (48 loc) · 1.42 KB
/
watcher.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
var gather = require('../lib/gather')
var structure = require('../lib/structure')
var viewParams = require('../lib/view-params')
var Doclet = require('../lib/models/doclet')
var _ = require('underscore')
var jade = require('jade')
var chokidar = require('chokidar')
var path = require('path')
var branch = require('git-branch')
var express = require('express')
var io = require('socket.io')
var sys = require('sys')
var http = require('http')
var argv = require('optimist').argv
if (argv.h) {
console.log('-p 3000 -d [dir]')
sys.exit(0)
}
var app = express()
var server = http.Server(app)
var socket = io(server)
var port = argv.p || 3000
var repoPath = path.resolve(argv.d)
var dbEntry = gather.gatherDocletsAndMeta(repoPath, false, branch.sync(repoPath))
if (dbEntry.error) {
console.log(dbEntry)
sys.exit(0)
}
server.listen(port)
var template = jade.compileFile('./views/api.jade')
app.get('/', function (req, res) {
var doclet = new Doclet({
data: {
hot: 'http://localhost:' + port,
doclets: gather.createDoclets(dbEntry.config, repoPath),
articles: dbEntry.articles
}
})
res.send(template(_.extend({
moment: require('moment')
}, viewParams.getApiParams(doclet), dbEntry, structure, viewParams)))
})
app.use(express.static('assets'))
var watch = path.join(repoPath, dbEntry.config.dir)
console.log('Watching ', watch)
chokidar.watch(watch).on('all', function () {
socket.emit('change')
})