summaryrefslogtreecommitdiffstats
path: root/lib/utils/watch.js
blob: 4d1a752d41a5fb779301e53716a507d0dc371ab3 (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;