embark/lib/index.js

212 lines
6.0 KiB
JavaScript
Raw Normal View History

2016-08-14 16:26:49 +00:00
/*jshint esversion: 6 */
let async = require('async');
2017-03-12 15:21:19 +00:00
// require("./utils/debug_util.js")(__filename, async);
let colors = require('colors');
2016-08-14 12:04:34 +00:00
2017-03-30 13:16:46 +00:00
let Engine = require('./core/engine.js');
let IPFS = require('./upload/ipfs.js');
let Swarm = require('./upload/swarm.js');
2017-03-30 13:16:46 +00:00
let version = require('../package.json').version;
2017-03-31 11:34:43 +00:00
class Embark {
2017-03-30 13:16:46 +00:00
constructor (options) {
this.version = version;
this.options = options || {};
}
2017-03-30 11:12:39 +00:00
2017-03-30 13:16:46 +00:00
initConfig(env, options) {
let Events = require('./core/events.js');
let Logger = require('./core/logger.js');
let Config = require('./core/config.js');
2017-03-30 11:12:39 +00:00
2017-03-30 13:16:46 +00:00
this.events = new Events();
this.logger = new Logger({logLevel: 'debug'});
this.config = new Config({env: env, logger: this.logger, events: this.events});
2017-03-30 11:12:39 +00:00
this.config.loadConfigFiles(options);
this.plugins = this.config.plugins;
2017-03-30 13:16:46 +00:00
}
2017-03-30 11:12:39 +00:00
2017-03-30 13:16:46 +00:00
blockchain(env, client) {
return require('./cmds/blockchain/blockchain.js')(this.config.blockchainConfig, client, env).run();
}
2017-02-25 03:49:34 +00:00
2017-03-30 13:16:46 +00:00
simulator(options) {
let Simulator = require('./cmds/simulator.js');
let simulator = new Simulator({blockchainConfig: this.config.blockchainConfig});
simulator.run(options);
}
generateTemplate(templateName, destinationFolder, name) {
2017-03-30 13:16:46 +00:00
let TemplateGenerator = require('./cmds/template_generator.js');
let templateGenerator = new TemplateGenerator(templateName);
templateGenerator.generate(destinationFolder, name);
}
2017-02-25 03:49:34 +00:00
2017-03-30 13:16:46 +00:00
run(options) {
2017-03-31 11:34:43 +00:00
let self = this;
2017-03-30 13:16:46 +00:00
let Dashboard = require('./dashboard/dashboard.js');
2017-03-30 13:16:46 +00:00
let env = options.env;
2017-03-30 13:16:46 +00:00
let engine = new Engine({
env: options.env,
2017-03-31 11:34:43 +00:00
version: this.version,
2017-03-30 13:16:46 +00:00
embarkConfig: options.embarkConfig || 'embark.json'
});
engine.init();
if (!options.useDashboard) {
console.log('========================'.bold.green);
2017-03-31 11:34:43 +00:00
console.log(('Welcome to Embark ' + this.version).yellow.bold);
2017-03-30 13:16:46 +00:00
console.log('========================'.bold.green);
}
async.parallel([
function startDashboard(callback) {
if (!options.useDashboard) {
return callback();
}
let dashboard = new Dashboard({
logger: engine.logger,
plugins: engine.plugins,
2017-03-31 11:34:43 +00:00
version: self.version,
2017-03-30 13:16:46 +00:00
env: engine.env
});
dashboard.start(function () {
engine.events.on('abi-vanila', function (abi) {
dashboard.console.runCode(abi);
2017-03-30 11:12:39 +00:00
});
2017-03-30 13:16:46 +00:00
engine.logger.info('dashboard start');
engine.events.on('servicesState', function (servicesState) {
dashboard.monitor.availableServices(servicesState);
});
2017-03-30 11:12:39 +00:00
callback();
2017-03-30 13:16:46 +00:00
});
},
function (callback) {
let pluginList = engine.plugins.listPlugins();
if (pluginList.length > 0) {
engine.logger.info("loaded plugins: " + pluginList.join(", "));
2017-03-30 11:12:39 +00:00
}
2017-03-30 13:16:46 +00:00
engine.startMonitor();
engine.startService("web3");
engine.startService("pipeline");
engine.startService("abi");
engine.startService("deployment");
engine.startService("ipfs");
engine.events.on('check:backOnline:Ethereum', function () {
engine.logger.info('Ethereum node detected..');
engine.config.reloadConfig();
engine.deployManager.deployContracts(function () {
engine.logger.info('Deployment Done');
});
2016-11-28 20:14:39 +00:00
});
2017-03-30 13:16:46 +00:00
engine.deployManager.deployContracts(function (err) {
engine.startService("fileWatcher");
if (options.runWebserver) {
engine.startService("webServer", {
host: options.serverHost,
port: options.serverPort
});
}
callback(err);
2017-03-30 11:12:39 +00:00
});
2017-03-30 13:16:46 +00:00
}
], function (err, result) {
if (err) {
engine.logger.error(err.message);
engine.logger.info(err.stack);
2016-10-31 02:04:25 +00:00
} else {
2017-03-30 13:16:46 +00:00
engine.logger.setStatus("Ready".green);
engine.logger.info("Looking for documentation? you can find it at ".cyan + "http://embark.readthedocs.io/".green.underline);
engine.logger.info("Ready".underline);
engine.events.emit('firstDeploymentDone');
2016-10-31 02:04:25 +00:00
}
2017-03-30 13:16:46 +00:00
});
}
build(options) {
let engine = new Engine({
env: options.env,
2017-03-31 11:34:43 +00:00
version: this.version,
2017-03-30 13:16:46 +00:00
embarkConfig: 'embark.json',
interceptLogs: false
});
engine.init();
2016-10-02 21:26:57 +00:00
2017-03-30 13:16:46 +00:00
async.waterfall([
function startServices(callback) {
let pluginList = engine.plugins.listPlugins();
if (pluginList.length > 0) {
engine.logger.info("loaded plugins: " + pluginList.join(", "));
}
engine.startService("web3");
engine.startService("pipeline");
engine.startService("abi");
engine.startService("deployment");
callback();
},
function deploy(callback) {
engine.deployManager.deployContracts(function (err) {
callback(err);
});
}
], function (err, result) {
if (err) {
engine.logger.error(err.message);
engine.logger.debug(err.stack);
} else {
engine.logger.info("finished building".underline);
}
// needed due to child processes
process.exit();
});
}
initTests(options) {
2017-03-30 13:16:46 +00:00
let Test = require('./core/test.js');
return new Test(options);
}
// TODO: should deploy if it hasn't already
upload(platform) {
if (platform === 'ipfs') {
let ipfs = new IPFS({buildDir: 'dist/', plugins: this.plugins, storageConfig: this.config.storageConfig});
ipfs.deploy();
} else if (platform === 'swarm') {
let swarm = new Swarm({buildDir: 'dist/', plugins: this.plugins, storageConfig: this.config.storageConfig});
swarm.deploy();
} else {
console.log(("unknown platform: " + platform).red);
console.log('try "embark upload ipfs" or "embark upload swarm"'.green);
2017-01-08 01:37:48 +00:00
}
}
runTests(file) {
2017-07-02 04:27:14 +00:00
let RunTests = require('./core/run_tests.js');
RunTests.run(file);
}
2017-03-30 13:16:46 +00:00
}
2017-06-26 13:01:54 +00:00
// temporary until next refactor
Embark.initTests = function(options) {
let Test = require('./core/test.js');
return new Test(options);
2017-06-26 13:02:58 +00:00
};
2017-06-26 13:01:54 +00:00
2016-08-18 00:29:41 +00:00
module.exports = Embark;