watcher.js
1.78 KB
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
90
91
92
93
var grapher = require('sass-graph'),
clonedeep = require('lodash/cloneDeep'),
path = require('path'),
config = {},
watcher = {},
graph = null;
watcher.reset = function(opts) {
config = clonedeep(opts || config || {});
var options = {
loadPaths: config.includePath,
extensions: ['scss', 'sass', 'css'],
follow: config.follow,
};
if (config.directory) {
graph = grapher.parseDir(config.directory, options);
} else {
graph = grapher.parseFile(config.src, options);
}
return Object.keys(graph.index);
};
watcher.changed = function(absolutePath) {
var files = {
added: [],
changed: [],
removed: [],
};
this.reset();
if (absolutePath && path.basename(absolutePath)[0] !== '_') {
files.changed.push(absolutePath);
}
graph.visitAncestors(absolutePath, function(parent) {
if (path.basename(parent)[0] !== '_') {
files.changed.push(parent);
}
});
graph.visitDescendents(absolutePath, function(child) {
files.added.push(child);
});
return files;
};
watcher.added = function(absolutePath) {
var files = {
added: [],
changed: [],
removed: [],
};
this.reset();
if (Object.keys(graph.index).indexOf(absolutePath) === -1) {
files.added.push(absolutePath);
}
graph.visitDescendents(absolutePath, function(child) {
files.added.push(child);
});
return files;
};
watcher.removed = function(absolutePath) {
var files = {
added: [],
changed: [],
removed: [],
};
graph.visitAncestors(absolutePath, function(parent) {
if (path.basename(parent)[0] !== '_') {
files.changed.push(parent);
}
});
if (Object.keys(graph.index).indexOf(absolutePath) !== -1) {
files.removed.push(absolutePath);
}
this.reset();
return files;
};
module.exports = watcher;