refactor watch

This commit is contained in:
Iuri Matias 2016-08-21 11:24:57 -04:00
parent eb95ae7fd6
commit f6c8b7a301
3 changed files with 40 additions and 30 deletions

1
.gitignore vendored
View File

@ -2,4 +2,5 @@ node_modules
TODO TODO
.node-xmlhttprequest-sync-* .node-xmlhttprequest-sync-*
demo/dist/ demo/dist/
demo/.embark/development/
boilerplate/dist/ boilerplate/dist/

View File

@ -13,6 +13,8 @@ var ContractsManager = require('./contracts.js');
var ABIGenerator = require('./abi.js'); var ABIGenerator = require('./abi.js');
var TemplateGenerator = require('./template_generator.js'); var TemplateGenerator = require('./template_generator.js');
var Blockchain = require('./blockchain.js'); var Blockchain = require('./blockchain.js');
var Server = require('./server.js');
var Watch = require('./watch.js');
var Embark = { var Embark = {
@ -35,9 +37,10 @@ var Embark = {
run: function(env) { run: function(env) {
Embark.deploy(function(abi) { Embark.deploy(function(abi) {
Embark.buildAssets(abi); Embark.buildAssets(abi);
var server = new Server(); var server = new Server({});
server.start(function() { server.start(function() {
Embark.watch(); var watch = new Watch();
watch.start();
}); });
}); });
}, },
@ -114,34 +117,6 @@ var Embark = {
//console.log(content); //console.log(content);
fs.writeFileSync("dist/" + targetFile, content); fs.writeFileSync("dist/" + targetFile, content);
} }
},
server: function(callback) {
},
watch: function() {
var embarkConfig = JSON.parse(fs.readFileSync("embark.json"));
var appConfig = embarkConfig.app;
var filesToWatch = [];
for(var targetFile in appConfig) {
filesToWatch.push(appConfig[targetFile]);
}
console.log(filesToWatch);
var watcher = chokidar.watch(filesToWatch, {
ignored: /[\/\\]\./,
persistent: true,
ignoreInitial: true,
followSymlinks: true
});
watcher
.on('add', path => console.log(`File ${path} has been added`))
.on('change', path => console.log(`File ${path} has been changed`))
.on('unlink', path => console.log(`File ${path} has been removed`))
.on('ready', () => console.log('ready to watch changes'));
console.log("done!");
} }
}; };

34
lib/watch.js Normal file
View File

@ -0,0 +1,34 @@
var fs = require('fs');
var chokidar = require('chokidar');
var Watch = function(options) {
this.options = options;
};
Watch.prototype.start = function() {
var embarkConfig = JSON.parse(fs.readFileSync("embark.json"));
var appConfig = embarkConfig.app;
var filesToWatch = [];
for(var targetFile in appConfig) {
filesToWatch.push(appConfig[targetFile]);
}
// TODO: add callback to ready
console.log(filesToWatch);
var watcher = chokidar.watch(filesToWatch, {
ignored: /[\/\\]\./,
persistent: true,
ignoreInitial: true,
followSymlinks: true
});
watcher
.on('add', path => console.log(`File ${path} has been added`))
.on('change', path => console.log(`File ${path} has been changed`))
.on('unlink', path => console.log(`File ${path} has been removed`))
.on('ready', () => console.log('ready to watch changes'));
console.log("done!");
};
module.exports = Watch;