embark/lib/index.js

196 lines
5.8 KiB
JavaScript
Raw Normal View History

2016-08-14 12:26:49 -04:00
/*jshint esversion: 6 */
let async = require('async');
2017-03-13 00:21:19 +09:00
// require("./utils/debug_util.js")(__filename, async);
let colors = require('colors');
2016-08-14 08:04:34 -04:00
let IPFS = require('./upload/ipfs.js');
let Swarm = require('./upload/swarm.js');
let EventEmitter = require('events').EventEmitter;
let Config = require('./core/config');
/**
* Initialize a new `Embark` instance.
*
* @return {Embark}
* @api public
*/
2017-02-19 12:51:32 -05:00
let Embark = function (options) {
this.version = require('../package.json').version;
2015-06-15 06:07:40 -04:00
this.env = options.environment || options.env || "development";
2016-08-17 20:29:41 -04:00
this.config = new Config({env: this.env, logger: this.logger, events: this.events});
this.config.loadConfigFiles(options);
this.plugins = this.config.plugins;
2017-01-26 06:34:00 -05:00
this.blockchain = function (env, client) {
return require('./cmds/blockchain/blockchain.js')(Embark.prototype.config.blockchainConfig, client, env).run();
};
2017-02-19 13:18:43 -05:00
this.simulator = function (options) {
let Simulator = require('./cmds/simulator.js');
let simulator = new Simulator({blockchainConfig: Embark.prototype.config.blockchainConfig});
2017-02-19 13:18:43 -05:00
simulator.run(options);
};
2017-02-19 13:18:43 -05:00
this.generateTemplate = function (templateName, destinationFolder, name) {
let TemplateGenerator = require('./cmds/template_generator.js');
let templateGenerator = new TemplateGenerator(templateName);
2017-02-19 13:18:43 -05:00
templateGenerator.generate(destinationFolder, name);
};
2017-02-24 22:49:34 -05:00
this.run = function (options) {
let Dashboard = require('./dashboard/dashboard.js');
let Engine = require('./core/engine');
let engine = new Engine({
env: options.env,
embarkConfig: 'embark.json'
});
engine.init();
2017-02-24 22:49:34 -05:00
if (!options.useDashboard) {
console.log('========================'.bold.green);
console.log(('Welcome to Embark ' + Embark.version).yellow.bold);
console.log('========================'.bold.green);
}
async.parallel([
function startDashboard(callback) {
2016-10-29 22:30:41 -04:00
if (!options.useDashboard) {
return callback();
}
let dashboard = new Dashboard({
logger: engine.logger,
plugins: engine.plugins,
version: engine.version,
env: engine.env
});
dashboard.start(function () {
Embark.on('abi-vanila', function (abi) {
2017-02-24 22:49:34 -05:00
dashboard.console.runCode(abi);
});
engine.logger.info('dashboard start');
Embark.on('servicesState', function (servicesState) {
dashboard.monitor.availableServices(servicesState);
});
2017-02-24 22:49:34 -05:00
callback();
});
},
2017-02-24 22:49:34 -05:00
function (callback) {
let pluginList = engine.plugins.listPlugins();
if (pluginList.length > 0) {
engine.logger.info("loaded plugins: " + pluginList.join(", "));
}
engine.startMonitor();
engine.startService("web3");
engine.startService("pipeline");
engine.startService("abi");
engine.startService("deployment");
engine.startService("ipfs");
Embark.on('check:backOnline:Ethereum', function () {
engine.logger.info('Ethereum node detected..');
engine.config.reloadConfig();
engine.deployManager.deployContracts(function () {
engine.logger.info('Deployment Done');
});
});
engine.deployManager.deployContracts(function () {
engine.startService("fileWatcher");
if (options.runWebserver) {
engine.startService("webServer", {
host: options.serverHost,
port: options.serverPort
});
}
callback();
});
2016-08-21 12:02:02 -04:00
}
], function (err) {
2016-10-30 22:04:25 -04:00
if (err) {
engine.logger.error(err.message);
} else {
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);
Embark.emit('firstDeploymentDone');
2016-10-30 22:04:25 -04:00
}
2016-08-21 11:02:50 -04:00
});
};
2016-08-21 11:02:50 -04:00
this.build = function (options) {
let Engine = require('./core/engine');
let engine = new Engine({
env: options.env,
embarkConfig: 'embark.json',
interceptLogs: false
});
engine.init();
2016-08-21 12:02:02 -04:00
async.waterfall([
function startServices(callback) {
let pluginList = engine.plugins.listPlugins();
2017-02-25 20:45:40 -05:00
if (pluginList.length > 0) {
engine.logger.info("loaded plugins: " + pluginList.join(", "));
2017-02-25 20:45:40 -05:00
}
engine.startService("pipeline");
engine.startService("abi");
engine.startService("deployment");
callback();
2016-08-21 12:02:02 -04:00
},
function deploy(callback) {
engine.deployManager.deployContracts(function () {
callback();
2016-11-28 17:14:39 -03:00
});
2016-08-21 12:02:02 -04:00
}
], function (err) {
2016-10-30 22:04:25 -04:00
if (err) {
engine.logger.error(err.message);
2016-10-30 22:04:25 -04:00
} else {
engine.logger.info("finished building".underline);
2016-10-30 22:04:25 -04:00
}
// needed due to child processes
process.exit();
2016-08-21 11:02:50 -04:00
});
};
2016-08-21 11:02:50 -04:00
this.initTests = function (options) {
let Test = require('./core/test.js');
2016-08-21 18:05:35 -04:00
return new Test(options);
};
2016-10-02 17:26:57 -04:00
// TODO: should deploy if it hasn't already
this.upload = function (platform) {
2017-01-07 20:37:48 -05:00
if (platform === 'ipfs') {
let ipfs = new IPFS({buildDir: 'dist/', plugins: Embark.prototype.plugins, storageConfig: Embark.prototype.config.storageConfig});
2017-01-07 20:37:48 -05:00
ipfs.deploy();
} else if (platform === 'swarm') {
let swarm = new Swarm({buildDir: 'dist/', plugins: Embark.prototype.plugins, storageConfig: Embark.prototype.config.storageConfig});
2017-01-07 20:37:48 -05:00
swarm.deploy();
} else {
console.log(("unknown platform: " + platform).red);
console.log('try "embark upload ipfs" or "embark upload swarm"'.green);
}
};
if (!(this instanceof Embark)) {
return new Embark();
}
return this;
};
Embark.prototype = Object.create(EventEmitter.prototype);
2016-08-17 20:29:41 -04:00
module.exports = Embark;
2016-08-14 11:10:34 -04:00