-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
173 lines (139 loc) · 4.04 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
var fs = require('fs');
var sane = require('sane');
var resolve = require('path').resolve;
var EventEmitter = require('events').EventEmitter;
var debug = require('debug')('component-watcher');
// default extensions to check
var EXTENSIONS = {
scripts: [
'js',
'json',
'html',
],
styles: [
'css',
],
};
// default fields to check
var FIELDS = {
scripts: [
'scripts',
'json',
'templates',
],
styles: [
'styles',
]
};
module.exports = function componentWatcher(options) {
options = options || {};
var root = resolve(options.root || process.cwd());
var filename = resolve(root, 'component.json');
// make sure `component.json` exists
try {
fs.statSync(filename);
} catch (err) {
throw new Error('"' + filename + '" does not exist!');
}
var extensions = options.extensions || EXTENSIONS;
var fields = options.fields || FIELDS;
var json;
var paths;
var globs;
var watcher;
var emitter = new EventEmitter();
emitter.restart =
emitter.start = function () {
if (watcher) watcher.close();
watch();
return emitter;
};
emitter.close =
emitter.stop = function () {
if (!watcher) return emitter;
watcher.close();
watcher = null;
return emitter;
};
watch();
return emitter;
function watch() {
try {
json = read(filename);
} catch (err) {
console.error('could not read "' + filename + '", not watching.');
return;
}
paths = json.paths || [];
globs = ['component.json'];
fields.scripts.forEach(addField);
fields.styles.forEach(addField);
paths.forEach(addPath);
debug('watching globs: ' + globs.join(', '));
watcher = sane(root, globs);
watcher.on('ready', onReady);
watcher.on('change', onUpdate);
watcher.on('add', onChange);
watcher.on('delete', onChange);
}
function onReady() {
// always resolve on ready
emitter.emit('resolve');
}
// when a file is updated
function onUpdate(file) {
debug('watcher emitted update: "%s"', file);
// the main `component.json` has changed,
// so we rewatch everything
if (file === filename) return emitter.restart();
// if a `component.json` is changed,
// emit a resolve event
if (/(\/)?component\.json$/.test(file)) return emitter.emit('resolve');
var ext = file.split('.').pop();
debug('watcher got extension: "%s"', ext);
if (~extensions.scripts.indexOf(ext)) return emitter.emit('scripts');
if (~extensions.styles.indexOf(ext)) return emitter.emit('styles');
}
// when a file is added or removed,
// resolve
function onChange(file) {
debug('watcher emitted change: "%s"', file);
// the main `component.json` has been added or removed,
// so we rewatch everything
if (file === filename) return emitter.restart();
// otherwise, resolve
emitter.emit('resolve');
}
function addField(field) {
(json[field] || []).forEach(function (path) {
if (path.slice(2) === '..') throw new Error('invalid path: ' + path);
if (path.slice(1) === '/') throw new Error('invalid path: ' + path);
// ./ paths
if (path[0] === '.') path = path.slice(2);
// only add globs if necessary
if (!~globs.indexOf(path)) globs.push(path);
});
// do the same for development if --dev was passed and a development property exists
if (options.development && json.development) {
(json.development[field] || []).forEach(function (path) {
if (path.slice(2) === '..') throw new Error('invalid path: ' + path);
if (path.slice(1) === '/') throw new Error('invalid path: ' + path);
// ./ paths
if (path[0] === '.') path = path.slice(2);
// only add globs if necessary
if (!~globs.indexOf(path)) globs.push(path);
});
}
}
function addPath(path) {
extensions.scripts.forEach(push);
extensions.styles.forEach(push);
function push(ext) {
globs.push(path + '/**/*.' + ext);
}
}
};
function read(filename) {
var json = fs.readFileSync(filename, 'utf8');
return JSON.parse(json);
}