Skip to content

Commit 6766eca

Browse files
authored
Merge pull request #388 from embark-framework/bug_fix/changing-contract-config
Redeploy with right config on config change
2 parents 5aa05c8 + 02ff6c3 commit 6766eca

File tree

8 files changed

+402
-34
lines changed

8 files changed

+402
-34
lines changed

lib/constants.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"any": "any"
1414
},
1515
"events": {
16-
"contractFilesChanged": "contractFilesChanged"
16+
"contractFilesChanged": "contractFilesChanged",
17+
"contractConfigChanged": "contractConfigChanged"
1718
}
1819
}

lib/contracts/contracts.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,11 @@ class ContractsManager {
1919
this.deployOnlyOnConfig = false;
2020
this.events = options.events;
2121

22-
this.events.on(constants.events.contractFilesChanged, (newContracts) => {
23-
this.contractFiles = newContracts;
22+
this.events.on(constants.events.contractFilesChanged, (newContractFiles) => {
23+
this.contractFiles = newContractFiles;
24+
});
25+
this.events.on(constants.events.contractConfigChanged, (newContracts) => {
26+
this.contractsConfig = newContracts;
2427
});
2528
}
2629

lib/core/config.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,12 @@ Config.prototype.loadContractsConfigFile = function() {
142142

143143
let configFilePath = this._getFileOrOject(this.configDir, 'contracts.json', 'contracts');
144144

145-
this.contractsConfig = this._mergeConfig(configFilePath, configObject, this.env);
145+
const newContractsConfig = this._mergeConfig(configFilePath, configObject, this.env);
146+
147+
if (!deepEqual(newContractsConfig, this.contractsConfig)) {
148+
this.events.emit(constants.events.contractConfigChanged, newContractsConfig);
149+
this.contractsConfig = newContractsConfig;
150+
}
146151
};
147152

148153
Config.prototype.loadExternalContractsFiles = function() {
@@ -220,7 +225,7 @@ Config.prototype.loadWebServerConfigFile = function() {
220225
Config.prototype.loadEmbarkConfigFile = function() {
221226
const contracts = this.embarkConfig.contracts;
222227
const newContractsFiles = this.loadFiles(contracts);
223-
if (newContractsFiles.length !== this.contractFiles || deepEqual(newContractsFiles, this.contractFiles)) {
228+
if (!this.contractFiles || newContractsFiles.length !== this.contractFiles.length || !deepEqual(newContractsFiles, this.contractFiles)) {
224229
this.events.emit(constants.events.contractFilesChanged, newContractsFiles);
225230
this.contractsFiles = newContractsFiles;
226231
}

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)