-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
89 lines (76 loc) · 2.59 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
/*!
* base-watch <https://github.com/base/base-watch>
*
* Copyright (c) 2016, Brian Woodward.
* Licensed under the MIT License.
*/
'use strict';
var utils = require('./utils');
/**
* Adds a [watch](#watch) method to your app that takes a file, directory, or glob pattern and watches for changes
* to those files. When a change occurs, the specified task(s) or function will execute.
*
* If no task(s) or function is specified, only the instance of `FSWatcher` is returned and can be used directly.
* See [chokidar.watch](https://github.com/paulmillr/chokidar#api) for more information.
*
* ```js
* app.use(watch());
* ```
* @return {Function} Returns the plugin function to be used in a [base][] application.
* @api public
*/
module.exports = function(config) {
return function(app) {
if (this.isRegistered('base-watch')) {
return;
}
/**
* Watch a file, directory, or glob pattern for changes and build a task
* or list of tasks when changes are made. Watch is powered by [chokidar][]
* so arguments can be anything supported by [chokidar.watch](https://github.com/paulmillr/chokidar#api).
*
* ```js
* var watcher = app.watch('templates/pages/*.hbs', ['site']);
* ```
* @name watch
* @param {String|Array} `glob` Filename, Directory name, or glob pattern to watch
* @param {Object} `options` Additional options to be passed to [chokidar][]
* @param {String|Array|Function} `tasks` Tasks that are passed to `.build` when files in the glob are changed.
* @return {Object} Returns an instance of `FSWatcher` from [chokidar][]
* @api public
*/
this.define('watch', function(glob, options/*, fns/tasks */) {
var self = this;
var len = arguments.length - 1, i = 0;
var args = new Array(len + 1);
while (len--) args[i] = arguments[++i];
args[i] = cb;
var opts = {};
if (typeof options === 'object' && !Array.isArray(options)) {
args.shift();
opts = utils.extend({}, options);
}
opts = utils.extend({}, config, opts);
var building = true;
function cb(err) {
building = false;
if (err) console.error(err);
}
var watch = utils.chokidar.watch(glob, opts);
// only contains our `cb` function
if (args.length === 1) {
return watch;
}
watch
.on('ready', function() {
building = false;
})
.on('all', function() {
if (building) return;
building = true;
self.build.apply(self, args);
});
return watch;
});
};
};