Skip to content

Commit 02ff6c3

Browse files
authored
Merge pull request #389 from embark-framework/bug_fix/file-changes-not-watched
Fix files not being watched
2 parents 3f3bba3 + 0dd2938 commit 02ff6c3

File tree

4 files changed

+385
-28
lines changed

4 files changed

+385
-28
lines changed

lib/core/engine.js

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -146,17 +146,11 @@ class Engine {
146146
self.currentAbi = abi;
147147
self.contractsJSON = contractsJSON;
148148
pipeline.build(abi, contractsJSON, null, function() {
149+
self.watch.restart(); // Necessary because changing a file while it is writing can stop it from being watched
149150
self.events.emit('outputDone');
150151
});
151152
});
152153
});
153-
// TODO: still need to redeploy contracts because the original contracts
154-
// config is being corrupted
155-
this.events.on('file-event', function(fileType, _path) {
156-
if (fileType === 'asset') {
157-
self.events.emit('asset-changed', self.contractsManager);
158-
}
159-
});
160154
}
161155

162156
codeGeneratorService(_options) {
@@ -213,6 +207,11 @@ class Engine {
213207
});
214208

215209
this.events.on('file-event', function (fileType) {
210+
// TODO: still need to redeploy contracts because the original contracts
211+
// config is being corrupted
212+
if (fileType === 'asset') {
213+
self.events.emit('asset-changed', self.contractsManager);
214+
}
216215
// TODO: for now need to deploy on asset chanes as well
217216
// because the contractsManager config is corrupted after a deploy
218217
if (fileType === 'contract' || fileType === 'config') {
@@ -225,8 +224,8 @@ class Engine {
225224

226225
fileWatchService(_options) {
227226
this.events.emit("status", "Watching for changes");
228-
let watch = new Watch({logger: this.logger, events: this.events});
229-
watch.start();
227+
this.watch = new Watch({logger: this.logger, events: this.events});
228+
this.watch.start();
230229
}
231230

232231
webServerService() {

lib/pipeline/watch.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ class Watch {
99
constructor(options) {
1010
this.logger = options.logger;
1111
this.events = options.events;
12+
this.fileWatchers = [];
1213
}
1314

1415
start() {
@@ -32,6 +33,19 @@ class Watch {
3233
this.logger.info("ready to watch file changes");
3334
}
3435

36+
restart() {
37+
this.stop();
38+
this.start();
39+
}
40+
41+
stop() {
42+
this.fileWatchers.forEach(fileWatcher => {
43+
fileWatcher.close();
44+
fileWatcher = null;
45+
});
46+
this.fileWatchers = [];
47+
}
48+
3549
watchAssets(embarkConfig, callback) {
3650
let self = this;
3751
let appConfig = embarkConfig.app;
@@ -102,6 +116,7 @@ class Watch {
102116
let configWatcher = chokidar.watch(files, {
103117
ignored: /[\/\\]\./, persistent: true, ignoreInitial: true, followSymlinks: true
104118
});
119+
this.fileWatchers.push(configWatcher);
105120

106121
configWatcher
107122
.on('add', path => changeCallback('add', path))

0 commit comments

Comments
 (0)