2016-08-14 16:26:49 +00:00
|
|
|
/*jshint esversion: 6 */
|
2016-08-13 14:48:00 +00:00
|
|
|
var async = require('async');
|
2016-08-14 12:04:34 +00:00
|
|
|
var Web3 = require('web3');
|
2016-08-14 14:34:42 +00:00
|
|
|
var fs = require('fs');
|
|
|
|
var grunt = require('grunt');
|
|
|
|
var mkdirp = require('mkdirp');
|
2016-08-14 15:10:34 +00:00
|
|
|
var colors = require('colors');
|
2016-08-14 16:26:49 +00:00
|
|
|
var chokidar = require('chokidar');
|
2016-08-14 12:04:34 +00:00
|
|
|
|
2016-08-18 00:29:41 +00:00
|
|
|
var Cmd = require('./cmd.js');
|
2016-08-14 12:04:34 +00:00
|
|
|
var Deploy = require('./deploy.js');
|
|
|
|
var ContractsManager = require('./contracts.js');
|
|
|
|
var ABIGenerator = require('./abi.js');
|
2016-08-21 14:42:42 +00:00
|
|
|
var TemplateGenerator = require('./template_generator.js');
|
|
|
|
var Blockchain = require('./blockchain.js');
|
2016-08-21 15:24:57 +00:00
|
|
|
var Server = require('./server.js');
|
|
|
|
var Watch = require('./watch.js');
|
2015-06-15 10:07:40 +00:00
|
|
|
|
2016-08-13 14:48:00 +00:00
|
|
|
var Embark = {
|
2016-08-18 00:29:41 +00:00
|
|
|
|
|
|
|
process: function(args) {
|
|
|
|
var cmd = new Cmd(Embark);
|
|
|
|
cmd.process(args);
|
|
|
|
},
|
|
|
|
|
2016-08-21 14:42:42 +00:00
|
|
|
generateTemplate: function(templateName, destinationFolder, name) {
|
|
|
|
var templateGenerator = new TemplateGenerator(templateName);
|
|
|
|
templateGenerator.generate(destinationFolder, name);
|
|
|
|
},
|
|
|
|
|
2016-08-14 12:04:34 +00:00
|
|
|
initConfig: function(configDir, files, env) {
|
|
|
|
this.contractsManager = new ContractsManager(configDir, files, env);
|
|
|
|
this.contractsManager.init();
|
|
|
|
return this.contractsManager;
|
|
|
|
},
|
2016-08-14 14:34:42 +00:00
|
|
|
|
2016-08-21 15:02:50 +00:00
|
|
|
run: function(env) {
|
|
|
|
Embark.deploy(function(abi) {
|
|
|
|
Embark.buildAssets(abi);
|
2016-08-21 15:24:57 +00:00
|
|
|
var server = new Server({});
|
2016-08-21 15:02:50 +00:00
|
|
|
server.start(function() {
|
2016-08-21 15:24:57 +00:00
|
|
|
var watch = new Watch();
|
|
|
|
watch.start();
|
2016-08-21 15:02:50 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
build: function(env) {
|
|
|
|
Embark.deploy(function(abi) {
|
|
|
|
Embark.buildAssets(abi);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
blockchain: function(env, client) {
|
|
|
|
var blockchain = Blockchain(client);
|
|
|
|
blockchain.run({env: env});
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-08-18 00:29:41 +00:00
|
|
|
deploy: function(done) {
|
2016-08-14 14:34:42 +00:00
|
|
|
async.waterfall([
|
|
|
|
function loadConfig(callback) {
|
|
|
|
var contractsManager = Embark.initConfig('config/', 'app/contracts/**/*.sol', 'development');
|
|
|
|
callback(null, contractsManager);
|
|
|
|
},
|
|
|
|
function buildContracts(contractsManager, callback) {
|
|
|
|
contractsManager.build();
|
2016-08-14 12:04:34 +00:00
|
|
|
callback(null, contractsManager);
|
2016-08-14 14:34:42 +00:00
|
|
|
},
|
|
|
|
function deployContracts(contractsManager, callback) {
|
|
|
|
var web3 = new Web3();
|
2016-08-18 00:29:41 +00:00
|
|
|
web3.setProvider(new web3.providers.HttpProvider('http://localhost:8545'));
|
2016-08-14 14:34:42 +00:00
|
|
|
var deploy = new Deploy(web3, contractsManager);
|
|
|
|
deploy.deployAll(function() {
|
|
|
|
callback(null, contractsManager);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function generateABI(contractsManager, callback) {
|
|
|
|
var abiGenerator = new ABIGenerator(contractsManager);
|
2016-08-18 00:29:41 +00:00
|
|
|
callback(null, abiGenerator.generateABI());
|
2016-08-14 14:34:42 +00:00
|
|
|
},
|
|
|
|
], function(err, result) {
|
2016-08-18 00:29:41 +00:00
|
|
|
done(result);
|
2016-08-14 12:04:34 +00:00
|
|
|
});
|
|
|
|
},
|
2016-08-14 14:34:42 +00:00
|
|
|
|
2016-08-18 00:29:41 +00:00
|
|
|
buildAssets: function(abi) {
|
2016-08-14 14:34:42 +00:00
|
|
|
var embarkConfig = JSON.parse(fs.readFileSync("embark.json"));
|
|
|
|
|
|
|
|
var appConfig = embarkConfig.app;
|
|
|
|
|
|
|
|
for(var targetFile in appConfig) {
|
2016-08-18 00:29:41 +00:00
|
|
|
var originalFiles = grunt.file.expand({nonull: true}, appConfig[targetFile]);
|
2016-08-14 14:34:42 +00:00
|
|
|
console.log(originalFiles);
|
|
|
|
// remove duplicates
|
|
|
|
|
|
|
|
var content = originalFiles.filter(function(file) {
|
|
|
|
return file.indexOf('.') >= 0;
|
|
|
|
}).map(function(file) {
|
|
|
|
console.log("reading " + file);
|
2016-08-18 00:29:41 +00:00
|
|
|
if (file === 'embark.js') {
|
2016-08-18 11:45:08 +00:00
|
|
|
return fs.readFileSync("../js/bluebird.js") + fs.readFileSync("../js/web3.js") + fs.readFileSync("../js/embark.js") + "\n" + abi;
|
2016-08-18 00:29:41 +00:00
|
|
|
} else {
|
|
|
|
return fs.readFileSync(file);
|
|
|
|
}
|
2016-08-14 14:34:42 +00:00
|
|
|
}).join("\n");
|
|
|
|
|
|
|
|
var dir = targetFile.split('/').slice(0, -1).join('/');
|
|
|
|
console.log("creating dir " + "dist/" + dir);
|
|
|
|
mkdirp.sync("dist/" + dir);
|
|
|
|
|
|
|
|
//console.log(content);
|
|
|
|
fs.writeFileSync("dist/" + targetFile, content);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-18 00:29:41 +00:00
|
|
|
};
|
2016-08-14 12:04:34 +00:00
|
|
|
|
2016-08-18 00:29:41 +00:00
|
|
|
module.exports = Embark;
|
2016-08-14 15:10:34 +00:00
|
|
|
|