summaryrefslogtreecommitdiffstats
path: root/lib/utils/watch.js
blob: 3e73e473aef5bb2015e29c85c30e037e6de56aa2 (plain)
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
var Q = require('q');
var _ = require('lodash');
var path = require('path');
var chokidar = require('chokidar');

var parsers = require('gitbook-parsers')

function watch(dir) {
    var d = Q.defer();
    dir = path.resolve(dir);

    var toWatch = [
        "book.json", "book.js"
    ];

    _.each(parsers.extensions, function(ext) {
        toWatch.push("**/*"+ext);
    });

    var watcher = chokidar.watch(toWatch, {
        cwd: dir,
        ignored: '_book/**',
        ignoreInitial: true
    });

    watcher.once("all", function(e, filepath) {
        watcher.close();

        d.resolve(filepath);
    });
    watcher.once("error", function(err) {
        watcher.close();

        d.reject(err);
    });

    return d.promise;
}

module.exports = watch;