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 15:10:34 +00:00
|
|
|
var colors = require('colors');
|
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');
|
2016-08-21 16:02:02 +00:00
|
|
|
var Pipeline = require('./pipeline.js');
|
2016-08-21 22:05:35 +00:00
|
|
|
var Test = require('./test.js');
|
2016-08-22 03:40:05 +00:00
|
|
|
var Logger = require('./logger.js');
|
|
|
|
var Config = require('./config.js');
|
2016-09-17 03:56:25 +00:00
|
|
|
var Monitor = require('./monitor.js');
|
2016-09-22 22:24:01 +00:00
|
|
|
var ServicesMonitor = require('./services.js');
|
2016-09-23 04:31:09 +00:00
|
|
|
var Console = require('./console.js');
|
2016-10-02 21:26:57 +00:00
|
|
|
var IPFS = require('./ipfs.js');
|
2017-01-07 21:52:29 +00:00
|
|
|
var Swarm = require('./swarm.js');
|
2017-02-18 13:01:03 +00:00
|
|
|
var Simulator = require('./simulator.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
|
|
|
|
2017-02-08 11:38:26 +00:00
|
|
|
version: '2.3.0',
|
2016-10-31 02:37:06 +00:00
|
|
|
|
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-22 03:40:05 +00:00
|
|
|
initConfig: function(env, options) {
|
2016-09-22 22:24:01 +00:00
|
|
|
this.logger = new Logger({logLevel: 'debug'});
|
2017-01-26 11:34:00 +00:00
|
|
|
|
|
|
|
this.config = new Config({env: env, logger: this.logger});
|
|
|
|
this.config.loadConfigFiles(options);
|
|
|
|
this.plugins = this.config.plugins;
|
|
|
|
|
2016-08-14 12:04:34 +00:00
|
|
|
},
|
2016-08-14 14:34:42 +00:00
|
|
|
|
2016-09-23 08:37:15 +00:00
|
|
|
redeploy: function(env) {
|
|
|
|
var self = this;
|
|
|
|
async.waterfall([
|
|
|
|
function reloadFiles(callback) {
|
|
|
|
self.config.reloadConfig();
|
|
|
|
callback();
|
|
|
|
},
|
2016-10-29 14:33:54 +00:00
|
|
|
self.buildDeployGenerate.bind(self)
|
2016-09-23 08:37:15 +00:00
|
|
|
], function(err, result) {
|
2016-10-31 02:04:25 +00:00
|
|
|
if (err) {
|
|
|
|
self.logger.error(err.message);
|
|
|
|
} else {
|
|
|
|
self.logger.trace("finished".underline);
|
|
|
|
}
|
2016-09-23 08:37:15 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2016-10-25 23:33:24 +00:00
|
|
|
run: function(options) {
|
2016-08-22 03:40:05 +00:00
|
|
|
var self = this;
|
2016-10-25 23:33:24 +00:00
|
|
|
var env = options.env;
|
2016-08-21 16:02:02 +00:00
|
|
|
async.waterfall([
|
2016-10-30 02:30:41 +00:00
|
|
|
function welcome(callback) {
|
|
|
|
if (!options.useDashboard) {
|
|
|
|
console.log('========================'.bold.green);
|
2016-10-31 02:37:06 +00:00
|
|
|
console.log(('Welcome to Embark ' + Embark.version).yellow.bold);
|
2016-10-30 02:30:41 +00:00
|
|
|
console.log('========================'.bold.green);
|
|
|
|
}
|
|
|
|
callback();
|
|
|
|
},
|
2016-09-23 04:31:09 +00:00
|
|
|
function startConsole(callback) {
|
2017-02-16 01:56:18 +00:00
|
|
|
Embark.console = new Console({plugins: self.plugins, version: self.version});
|
2016-09-23 04:31:09 +00:00
|
|
|
callback();
|
|
|
|
},
|
2016-09-17 03:56:25 +00:00
|
|
|
function startMonitor(callback) {
|
2016-10-30 02:30:41 +00:00
|
|
|
if (!options.useDashboard) {
|
|
|
|
return callback();
|
|
|
|
}
|
2016-09-23 04:31:09 +00:00
|
|
|
Embark.monitor = new Monitor({env: env, console: Embark.console});
|
2016-09-22 22:24:01 +00:00
|
|
|
self.logger.logFunction = Embark.monitor.logEntry;
|
|
|
|
self.logger.contractsState = Embark.monitor.setContracts;
|
|
|
|
self.logger.availableServices = Embark.monitor.availableServices;
|
2016-10-30 02:30:41 +00:00
|
|
|
self.logger.setStatus = Embark.monitor.setStatus.bind(Embark.monitor);
|
2017-01-15 23:46:40 +00:00
|
|
|
|
|
|
|
if (options.useDashboard) {
|
|
|
|
self.logger.info('========================'.bold.green);
|
|
|
|
self.logger.info(('Welcome to Embark ' + Embark.version).yellow.bold);
|
|
|
|
self.logger.info('========================'.bold.green);
|
|
|
|
}
|
|
|
|
|
2016-09-26 00:51:00 +00:00
|
|
|
// TODO: do this after monitor is rendered
|
2016-09-22 22:24:01 +00:00
|
|
|
callback();
|
|
|
|
},
|
2017-02-16 01:56:18 +00:00
|
|
|
function displayLoadedPlugins(callback) {
|
|
|
|
var pluginList = self.plugins.listPlugins();
|
|
|
|
if (pluginList.length > 0) {
|
|
|
|
self.logger.info("loaded plugins: " + pluginList.join(", "));
|
|
|
|
}
|
|
|
|
callback();
|
|
|
|
},
|
2016-09-22 22:24:01 +00:00
|
|
|
function monitorServices(callback) {
|
2016-10-30 02:30:41 +00:00
|
|
|
if (!options.useDashboard) {
|
|
|
|
return callback();
|
|
|
|
}
|
2016-09-22 22:24:01 +00:00
|
|
|
Embark.servicesMonitor = new ServicesMonitor({
|
|
|
|
logger: Embark.logger,
|
2016-10-25 23:33:24 +00:00
|
|
|
config: Embark.config,
|
2016-10-30 01:41:50 +00:00
|
|
|
serverHost: options.serverHost,
|
2016-10-30 02:00:54 +00:00
|
|
|
serverPort: options.serverPort,
|
2016-10-31 02:37:06 +00:00
|
|
|
runWebserver: options.runWebserver,
|
|
|
|
version: Embark.version
|
2016-09-22 22:24:01 +00:00
|
|
|
});
|
|
|
|
Embark.servicesMonitor.startMonitor();
|
2016-09-17 03:56:25 +00:00
|
|
|
callback();
|
|
|
|
},
|
2016-10-29 14:33:54 +00:00
|
|
|
self.buildDeployGenerate.bind(self),
|
2016-08-21 16:02:02 +00:00
|
|
|
function watchFilesForChanges(callback) {
|
2016-10-30 02:30:41 +00:00
|
|
|
self.logger.setStatus("Watching for changes");
|
2016-09-17 03:56:25 +00:00
|
|
|
var watch = new Watch({logger: self.logger});
|
2016-08-21 15:24:57 +00:00
|
|
|
watch.start();
|
2016-09-23 08:37:15 +00:00
|
|
|
watch.on('redeploy', function() {
|
|
|
|
self.logger.info("received redeploy event");
|
|
|
|
Embark.redeploy();
|
|
|
|
});
|
|
|
|
watch.on('rebuildAssets', function() {
|
|
|
|
self.logger.info("received rebuildAssets event");
|
|
|
|
// TODO: make this more efficient
|
|
|
|
Embark.redeploy();
|
|
|
|
});
|
2016-08-21 16:02:02 +00:00
|
|
|
callback();
|
2016-12-07 02:33:31 +00:00
|
|
|
},
|
2017-02-16 01:56:18 +00:00
|
|
|
function startAssetServer(callback) {
|
|
|
|
if (!options.runWebserver) {
|
|
|
|
return callback();
|
2016-12-07 02:33:31 +00:00
|
|
|
}
|
2017-02-16 01:56:18 +00:00
|
|
|
self.logger.setStatus("Starting Server");
|
|
|
|
var server = new Server({
|
|
|
|
logger: self.logger,
|
|
|
|
host: options.serverHost,
|
|
|
|
port: options.serverPort
|
|
|
|
});
|
|
|
|
server.start(callback);
|
2016-08-21 16:02:02 +00:00
|
|
|
}
|
|
|
|
], function(err, result) {
|
2016-10-31 02:04:25 +00:00
|
|
|
if (err) {
|
|
|
|
self.logger.error(err.message);
|
|
|
|
} else {
|
|
|
|
self.logger.setStatus("Ready".green);
|
2017-02-16 01:56:18 +00:00
|
|
|
self.logger.info("Looking for documentation? you can find it at ".cyan + "http://embark.readthedocs.io/".green.underline);
|
|
|
|
self.logger.info("Ready".underline);
|
2016-10-31 02:04:25 +00:00
|
|
|
}
|
2016-08-21 15:02:50 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
build: function(env) {
|
2016-10-31 02:04:25 +00:00
|
|
|
var self = this;
|
2016-08-21 16:02:02 +00:00
|
|
|
async.waterfall([
|
|
|
|
function deployAndGenerateABI(callback) {
|
|
|
|
Embark.deploy(function(abi) {
|
|
|
|
callback(null, abi);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function buildPipeline(abi, callback) {
|
2016-11-28 20:14:39 +00:00
|
|
|
self.logger.trace("Building Assets");
|
|
|
|
var pipeline = new Pipeline({
|
|
|
|
buildDir: self.config.buildDir,
|
|
|
|
contractsFiles: self.config.contractsFiles,
|
|
|
|
assetFiles: self.config.assetFiles,
|
2016-12-10 15:20:04 +00:00
|
|
|
logger: self.logger,
|
|
|
|
plugins: self.plugins
|
2016-11-28 20:14:39 +00:00
|
|
|
});
|
2016-08-21 16:02:02 +00:00
|
|
|
pipeline.build(abi);
|
|
|
|
callback();
|
|
|
|
}
|
|
|
|
], function(err, result) {
|
2016-10-31 02:04:25 +00:00
|
|
|
if (err) {
|
|
|
|
self.logger.error(err.message);
|
|
|
|
} else {
|
|
|
|
self.logger.trace("finished".underline);
|
|
|
|
}
|
2016-08-21 15:02:50 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
blockchain: function(env, client) {
|
2016-08-26 11:51:58 +00:00
|
|
|
var blockchain = Blockchain(this.config.blockchainConfig, client);
|
2016-08-21 15:02:50 +00:00
|
|
|
blockchain.run({env: env});
|
|
|
|
},
|
|
|
|
|
2016-09-23 04:31:09 +00:00
|
|
|
buildAndDeploy: function(done) {
|
2016-08-22 03:40:05 +00:00
|
|
|
var self = this;
|
2016-08-14 14:34:42 +00:00
|
|
|
async.waterfall([
|
2016-08-22 03:40:05 +00:00
|
|
|
function buildContracts(callback) {
|
|
|
|
var contractsManager = new ContractsManager({
|
|
|
|
contractFiles: self.config.contractsFiles,
|
2016-09-22 22:24:01 +00:00
|
|
|
contractsConfig: self.config.contractsConfig,
|
2017-01-29 02:31:09 +00:00
|
|
|
logger: Embark.logger,
|
|
|
|
plugins: self.plugins
|
2016-08-22 03:40:05 +00:00
|
|
|
});
|
2016-10-29 16:02:07 +00:00
|
|
|
contractsManager.build(callback);
|
2016-08-21 16:02:02 +00:00
|
|
|
},
|
|
|
|
function deployContracts(contractsManager, callback) {
|
2016-08-26 11:51:58 +00:00
|
|
|
|
|
|
|
//TODO: figure out where to put this since the web3 can be passed along if needed
|
|
|
|
// perhaps it should go into the deploy object itself
|
2016-09-26 00:51:00 +00:00
|
|
|
// TODO: should come from the config object
|
2016-08-21 16:02:02 +00:00
|
|
|
var web3 = new Web3();
|
2016-08-26 11:51:58 +00:00
|
|
|
var web3Endpoint = 'http://' + self.config.blockchainConfig.rpcHost + ':' + self.config.blockchainConfig.rpcPort;
|
|
|
|
web3.setProvider(new web3.providers.HttpProvider(web3Endpoint));
|
|
|
|
|
2016-09-23 04:43:07 +00:00
|
|
|
if (!web3.isConnected()) {
|
|
|
|
console.log(("Couldn't connect to " + web3Endpoint.underline + " are you sure it's on?").red);
|
|
|
|
console.log("make sure you have an ethereum node or simulator running. e.g 'embark blockchain'".magenta);
|
|
|
|
exit();
|
|
|
|
}
|
|
|
|
|
2016-10-02 21:57:33 +00:00
|
|
|
web3.eth.getAccounts(function(err, accounts) {
|
2016-10-31 01:56:19 +00:00
|
|
|
if (err) {
|
|
|
|
return callback(new Error(err));
|
|
|
|
}
|
2016-10-02 21:57:33 +00:00
|
|
|
web3.eth.defaultAccount = accounts[0];
|
|
|
|
|
|
|
|
var deploy = new Deploy({
|
|
|
|
web3: web3,
|
|
|
|
contractsManager: contractsManager,
|
|
|
|
logger: Embark.logger,
|
|
|
|
chainConfig: self.config.chainTracker,
|
|
|
|
env: self.config.env
|
|
|
|
});
|
|
|
|
deploy.deployAll(function() {
|
|
|
|
callback(null, contractsManager);
|
|
|
|
});
|
2016-08-21 16:02:02 +00:00
|
|
|
});
|
2016-10-02 21:57:33 +00:00
|
|
|
|
2016-09-23 04:31:09 +00:00
|
|
|
}
|
|
|
|
], function(err, result) {
|
2016-10-29 15:08:10 +00:00
|
|
|
if (err) {
|
|
|
|
done(err, null);
|
|
|
|
} else {
|
|
|
|
done(null, result);
|
|
|
|
}
|
2016-09-23 04:31:09 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
deploy: function(done) {
|
|
|
|
var self = this;
|
|
|
|
async.waterfall([
|
|
|
|
function buildAndDeploy(callback) {
|
2016-11-27 16:59:54 +00:00
|
|
|
Embark.buildAndDeploy(function(err, contractsManager) {
|
|
|
|
callback(err, contractsManager);
|
2016-09-23 04:31:09 +00:00
|
|
|
});
|
2016-08-21 16:02:02 +00:00
|
|
|
},
|
|
|
|
function generateABI(contractsManager, callback) {
|
2017-02-10 12:44:06 +00:00
|
|
|
var abiGenerator = new ABIGenerator({blockchainConfig: self.config.blockchainConfig, contractsManager: contractsManager, plugins: self.plugins, storageConfig: self.config.storageConfig});
|
2016-09-23 04:31:09 +00:00
|
|
|
callback(null, abiGenerator.generateABI({useEmbarkJS: true}));
|
2016-10-23 00:10:25 +00:00
|
|
|
}
|
2016-08-14 14:34:42 +00:00
|
|
|
], function(err, result) {
|
2016-10-31 02:04:25 +00:00
|
|
|
if (err) {
|
|
|
|
self.logger.error(err.message);
|
|
|
|
}
|
2016-08-18 00:29:41 +00:00
|
|
|
done(result);
|
2016-08-14 12:04:34 +00:00
|
|
|
});
|
2016-08-21 22:05:35 +00:00
|
|
|
},
|
|
|
|
|
2016-10-29 14:33:54 +00:00
|
|
|
|
|
|
|
buildDeployGenerate: function(done) {
|
|
|
|
var self = this;
|
|
|
|
|
2016-10-30 02:30:41 +00:00
|
|
|
self.logger.setStatus("Deploying...".magenta.underline);
|
2016-10-29 14:33:54 +00:00
|
|
|
async.waterfall([
|
|
|
|
function deployAndBuildContractsManager(callback) {
|
2016-10-29 15:08:10 +00:00
|
|
|
Embark.buildAndDeploy(function(err, contractsManager) {
|
|
|
|
if (err) {
|
2016-10-31 01:52:58 +00:00
|
|
|
return callback(err);
|
2016-10-29 15:08:10 +00:00
|
|
|
}
|
2016-10-31 01:52:58 +00:00
|
|
|
return callback(null, contractsManager);
|
2016-10-29 14:33:54 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
function generateConsoleABI(contractsManager, callback) {
|
2017-02-10 12:44:06 +00:00
|
|
|
var abiGenerator = new ABIGenerator({blockchainConfig: self.config.blockchainConfig, contractsManager: contractsManager, storageConfig: self.config.storageConfig});
|
2016-10-29 14:33:54 +00:00
|
|
|
var consoleABI = abiGenerator.generateABI({useEmbarkJS: false});
|
|
|
|
Embark.console.runCode(consoleABI);
|
|
|
|
callback(null, contractsManager);
|
|
|
|
},
|
|
|
|
function generateABI(contractsManager, callback) {
|
2017-02-10 12:44:06 +00:00
|
|
|
var abiGenerator = new ABIGenerator({blockchainConfig: self.config.blockchainConfig, contractsManager: contractsManager, plugins: self.plugins, storageConfig: self.config.storageConfig});
|
2016-10-29 14:33:54 +00:00
|
|
|
callback(null, abiGenerator.generateABI({useEmbarkJS: true}));
|
|
|
|
},
|
|
|
|
function buildPipeline(abi, callback) {
|
2016-10-30 02:30:41 +00:00
|
|
|
self.logger.setStatus("Building Assets");
|
2016-10-29 14:33:54 +00:00
|
|
|
var pipeline = new Pipeline({
|
|
|
|
buildDir: self.config.buildDir,
|
|
|
|
contractsFiles: self.config.contractsFiles,
|
|
|
|
assetFiles: self.config.assetFiles,
|
2016-12-10 15:20:04 +00:00
|
|
|
logger: self.logger,
|
|
|
|
plugins: self.plugins
|
2016-10-29 14:33:54 +00:00
|
|
|
});
|
|
|
|
pipeline.build(abi);
|
|
|
|
callback();
|
|
|
|
}
|
|
|
|
], function(err, result) {
|
2016-10-29 15:08:10 +00:00
|
|
|
if (err) {
|
|
|
|
self.logger.error("error deploying");
|
|
|
|
self.logger.error(err.message);
|
2016-10-30 02:30:41 +00:00
|
|
|
self.logger.setStatus("Deployment Error".red);
|
2016-10-29 15:08:10 +00:00
|
|
|
} else {
|
2016-10-30 02:30:41 +00:00
|
|
|
self.logger.setStatus("Ready".green);
|
2016-10-29 15:08:10 +00:00
|
|
|
}
|
2016-10-29 14:33:54 +00:00
|
|
|
done(result);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2016-08-21 22:05:35 +00:00
|
|
|
initTests: function(options) {
|
|
|
|
return new Test(options);
|
2016-10-02 21:26:57 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
// TODO: should deploy if it hasn't already
|
2017-01-08 01:37:48 +00:00
|
|
|
upload: function(platform) {
|
|
|
|
if (platform === 'ipfs') {
|
2017-02-10 12:44:06 +00:00
|
|
|
var ipfs = new IPFS({buildDir: 'dist/', plugins: this.plugins, storageConfig: this.config.storageConfig});
|
2017-01-08 01:37:48 +00:00
|
|
|
ipfs.deploy();
|
|
|
|
} else if (platform === 'swarm') {
|
2017-02-10 12:44:06 +00:00
|
|
|
var swarm = new Swarm({buildDir: 'dist/', plugins: this.plugins, storageConfig: this.config.storageConfig});
|
2017-01-08 01:37:48 +00:00
|
|
|
swarm.deploy();
|
|
|
|
} else {
|
|
|
|
console.log(("unknown platform: " + platform).red);
|
|
|
|
console.log('try "embark upload ipfs" or "embark upload swarm"'.green);
|
|
|
|
}
|
2017-01-07 21:52:29 +00:00
|
|
|
},
|
|
|
|
|
2017-02-18 13:01:03 +00:00
|
|
|
simulator: function(options) {
|
|
|
|
var simulator = new Simulator({blockchainConfig: this.config.blockchainConfig});
|
|
|
|
simulator.run(options);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|