summaryrefslogtreecommitdiffstats
path: root/lib/cli/watch.js
blob: 14434ab33e246690727da7e6407677ce380574f2 (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
41
42
43
44
45
46
var path = require('path');
var chokidar = require('chokidar');

var Promise = require('../utils/promise');
var parsers = require('../parsers');

/**
    Watch a folder and resolve promise once a file is modified

    @param {String} dir
    @return {Promise}
*/
function watch(dir) {
    var d = Promise.defer();
    dir = path.resolve(dir);

    var toWatch = [
        'book.json', 'book.js', '_layouts/**'
    ];

    // Watch all parsable files
    parsers.extensions.forEach(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;