-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
69 lines (60 loc) · 1.61 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
'use strict';
/**
* Watch out for dependent files,
* if they change - touch the file that includes them,
* or any other file you need
*
* @module gulp-watch-and-touch
* @author Oleg Dutchenko <dutchenko.o.wezom@gmail.com>
* @version 1.2.0
*/
const watch = require('gulp-watch');
const fs = require('fs');
/**
* Wrapper function
* @param {Gulp} gulp
* @return {function}
*/
module.exports = function (gulp) {
const watchers = {};
const cache = {};
/**
* @param {string} uniqueKey
* @param {string|Array} touchSrc
* @param {string|Array} watchingSrc
* @return {boolean}
*/
return function (uniqueKey, touchSrc, watchingSrc) {
let current = JSON.stringify(watchingSrc);
let cached = cache[uniqueKey] || JSON.stringify([]);
// if they are equal, then nothing has changed
// return false value
if (current === cached) {
return false;
}
// cache new sources
cache[uniqueKey] = current;
// new watcher name
let watcherName = `watcher:${uniqueKey}`;
if (watchers[watcherName]) {
// If it is already running - close it
watchers[watcherName].close();
}
// start new one
watchers[watcherName] = watch(watchingSrc, {read: false}, function () {
// touch the file on changes
return gulp.src(touchSrc, {buffer: false, read: false})
.on('data', function (file) {
let time = new Date();
fs.utimes(file.path, time, time, err => {
if (err) {
this.emit('end');
}
});
});
});
// when changing the cache and starting a new watcher - return the true value.
// this can be used for any of your actions, conditions, etc.
return true;
};
};