embark-area-51/lib/index.js

461 lines
14 KiB
JavaScript
Raw Normal View History

let async = require('async');
const constants = require('./constants');
2017-12-05 23:14:46 +00:00
require('colors');
2016-08-14 12:04:34 +00:00
// Set PWD to CWD since Windows doesn't have a value for PWD
if (!process.env.PWD) {
process.env.PWD = process.cwd();
}
2018-04-09 20:14:07 +00:00
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) {
2017-03-30 13:16:46 +00:00
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();
2018-03-15 21:18:20 +00:00
this.logger = new Logger({logLevel: 'debug', events: this.events});
2017-03-30 13:16:46 +00:00
this.config = new Config({env: env, logger: this.logger, events: this.events, context: this.context});
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) {
this.context = [constants.contexts.blockchain];
return require('./modules/blockchain_process/blockchain.js')(this.config.blockchainConfig, client, env, this.isDev(env), () => {}).run();
2017-03-30 13:16:46 +00:00
}
2017-02-25 03:49:34 +00:00
2017-03-30 13:16:46 +00:00
simulator(options) {
this.context = options.context || [constants.contexts.simulator, constants.contexts.blockchain];
2017-03-30 13:16:46 +00:00
let Simulator = require('./cmds/simulator.js');
2018-06-07 16:50:22 +00:00
let simulator = new Simulator({
blockchainConfig: this.config.blockchainConfig,
2018-06-07 19:13:35 +00:00
logger: this.logger
2018-06-07 16:50:22 +00:00
});
2017-03-30 13:16:46 +00:00
simulator.run(options);
}
2018-07-06 08:38:09 +00:00
generateTemplate(templateName, destinationFolder, name, url) {
this.context = [constants.contexts.templateGeneration];
2017-03-30 13:16:46 +00:00
let TemplateGenerator = require('./cmds/template_generator.js');
let templateGenerator = new TemplateGenerator(templateName);
2018-07-06 08:38:09 +00:00
if (url) {
return templateGenerator.downloadAndGenerate(url, destinationFolder, name);
}
2017-03-30 13:16:46 +00:00
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;
self.context = options.context || [constants.contexts.run, constants.contexts.build];
2017-03-30 13:16:46 +00:00
let Dashboard = require('./dashboard/dashboard.js');
2018-07-23 09:36:53 +00:00
let webServerConfig = {
enabled: options.runWebserver
};
2018-07-23 09:36:53 +00:00
if (options.serverHost) {
webServerConfig.host = options.serverHost;
}
if (options.serverPort) {
webServerConfig.port = options.serverPort;
}
2018-06-02 14:06:58 +00:00
const Engine = require('./core/engine.js');
const engine = new Engine({
2017-03-30 13:16:46 +00:00
env: options.env,
client: options.client,
locale: options.locale,
2017-03-31 11:34:43 +00:00
version: this.version,
embarkConfig: options.embarkConfig || 'embark.json',
logFile: options.logFile,
logLevel: options.logLevel,
context: self.context,
useDashboard: options.useDashboard,
webServerConfig: webServerConfig
2017-03-30 13:16:46 +00:00
});
engine.init();
if (!options.useDashboard) {
engine.logger.info('========================'.bold.green);
2018-05-08 21:49:46 +00:00
engine.logger.info((__('Welcome to Embark') + ' ' + this.version).yellow.bold);
engine.logger.info('========================'.bold.green);
2017-03-30 13:16:46 +00:00
}
async.parallel([
function startDashboard(callback) {
if (!options.useDashboard) {
return callback();
}
let dashboard = new Dashboard({
2017-12-17 23:34:41 +00:00
events: engine.events,
2017-03-30 13:16:46 +00:00
logger: engine.logger,
plugins: engine.plugins,
2017-03-31 11:34:43 +00:00
version: self.version,
2018-07-07 13:34:56 +00:00
env: engine.env
2017-03-30 13:16:46 +00:00
});
dashboard.start(function () {
2018-05-08 21:49:46 +00:00
engine.logger.info(__('dashboard start'));
callback();
2017-03-30 13:16:46 +00:00
});
},
function (callback) {
let pluginList = engine.plugins.listPlugins();
if (pluginList.length > 0) {
2018-05-08 21:49:46 +00:00
engine.logger.info(__("loaded plugins") + ": " + pluginList.join(", "));
2017-03-30 11:12:39 +00:00
}
2017-03-30 13:16:46 +00:00
engine.startService("processManager");
2018-06-01 16:27:12 +00:00
engine.startService("serviceMonitor");
engine.startService("libraryManager");
2018-05-23 15:16:56 +00:00
engine.startService("codeRunner");
2017-03-30 13:16:46 +00:00
engine.startService("web3");
engine.startService("pipeline");
engine.startService("deployment");
engine.startService("storage");
2018-05-23 15:16:13 +00:00
engine.startService("codeGenerator");
engine.startService("namingSystem");
2017-03-30 13:16:46 +00:00
engine.events.on('check:backOnline:Ethereum', function () {
2018-05-08 21:49:46 +00:00
engine.logger.info(__('Ethereum node detected') + '..');
2017-03-30 13:16:46 +00:00
engine.config.reloadConfig();
engine.events.request('deploy:contracts', function (err) {
if (err) {
return;
}
2018-05-08 21:49:46 +00:00
engine.logger.info(__('Deployment Done'));
2017-03-30 13:16:46 +00:00
});
2016-11-28 20:14:39 +00:00
});
2017-03-30 13:16:46 +00:00
engine.events.on('outputDone', function () {
2018-06-27 14:09:21 +00:00
engine.logger.warn(__("Note: The console uses ").green + (__("web3.js 1.0").underline + " ").cyan + " the reference docs for web3.js can be found at http://web3js.readthedocs.io/en/1.0/".green);
2018-05-08 21:49:46 +00:00
engine.logger.info((__("Looking for documentation? You can find it at") + " ").cyan + "http://embark.status.im/docs/".green.underline + ".".cyan);
engine.logger.info(__("Ready").underline);
engine.events.emit("status", __("Ready").green);
});
if (options.runWebserver) {
engine.startService("webServer", {
host: options.serverHost,
port: options.serverPort
});
}
engine.startService("fileWatcher");
2018-07-25 08:31:35 +00:00
callback();
2017-03-30 13:16:46 +00:00
}
2017-12-05 23:14:46 +00:00
], function (err, _result) {
2017-03-30 13:16:46 +00:00
if (err) {
engine.logger.error(err.message);
engine.logger.info(err.stack);
2016-10-31 02:04:25 +00:00
} else {
2017-12-19 20:14:09 +00:00
engine.events.emit('firstDeploymentDone');
2016-10-31 02:04:25 +00:00
}
2017-03-30 13:16:46 +00:00
});
}
build(options) {
this.context = options.context || [constants.contexts.build];
2018-06-02 14:06:58 +00:00
const Engine = require('./core/engine.js');
const engine = new Engine({
env: options.env,
2018-05-22 19:46:02 +00:00
client: options.client,
locale: options.locale,
version: this.version,
embarkConfig: 'embark.json',
interceptLogs: false,
logFile: options.logFile,
logLevel: options.logLevel,
events: options.events,
logger: options.logger,
config: options.config,
plugins: options.plugins,
context: this.context
});
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) {
2018-05-08 21:49:46 +00:00
engine.logger.info(__("loaded plugins") + ": " + pluginList.join(", "));
2017-03-30 13:16:46 +00:00
}
engine.startService("processManager");
engine.startService("libraryManager");
2018-05-23 15:16:56 +00:00
engine.startService("codeRunner");
2017-03-30 13:16:46 +00:00
engine.startService("web3");
2018-06-22 15:12:34 +00:00
if (!options.onlyCompile) {
engine.startService("pipeline");
}
engine.startService("deployment", {onlyCompile: options.onlyCompile});
engine.startService("storage");
2018-05-23 15:16:13 +00:00
engine.startService("codeGenerator");
2018-07-23 09:36:53 +00:00
2017-03-30 13:16:46 +00:00
callback();
},
function deploy(callback) {
engine.events.request('deploy:contracts', function (err) {
2018-05-23 14:06:12 +00:00
callback(err);
2017-03-30 13:16:46 +00:00
});
2018-06-14 14:30:34 +00:00
},
function waitForWriteFinish(callback) {
2018-06-22 15:12:34 +00:00
if (options.onlyCompile) {
engine.logger.info("Finished compiling".underline);
return callback(null, true);
}
2018-06-14 14:30:34 +00:00
engine.logger.info("Finished deploying".underline);
2018-06-22 15:12:34 +00:00
engine.events.on('outputDone', (err) => {
engine.logger.info(__("finished building").underline);
callback(err, true);
});
2017-03-30 13:16:46 +00:00
}
2018-06-22 15:12:34 +00:00
], function (err, canExit) {
2017-03-30 13:16:46 +00:00
if (err) {
engine.logger.error(err.message);
engine.logger.debug(err.stack);
}
2018-06-22 15:12:34 +00:00
2018-07-07 13:59:00 +00:00
// TODO: this should be moved out and determined somewhere else
2018-06-22 15:12:34 +00:00
if (canExit || !engine.config.contractsConfig.afterDeploy || !engine.config.contractsConfig.afterDeploy.length) {
process.exit();
}
engine.logger.info(__('Waiting for after deploy to finish...'));
engine.logger.info(__('You can exit with CTRL+C when after deploy completes'));
2017-03-30 13:16:46 +00:00
});
}
2018-07-23 08:34:21 +00:00
console(options) {
2018-07-23 09:36:53 +00:00
this.context = options.context || [constants.contexts.run, constants.contexts.console];
2018-07-23 12:30:58 +00:00
const REPL = require('./dashboard/repl.js');
2018-07-23 09:36:53 +00:00
const Engine = require('./core/engine.js');
const engine = new Engine({
env: options.env,
client: options.client,
locale: options.locale,
version: this.version,
embarkConfig: options.embarkConfig || 'embark.json',
logFile: options.logFile,
logLevel: options.logLevel,
2018-07-23 12:30:58 +00:00
context: this.context
2018-07-23 09:36:53 +00:00
});
engine.init();
2018-07-23 12:30:58 +00:00
async.waterfall([
function startServices(callback) {
2018-07-23 09:36:53 +00:00
let pluginList = engine.plugins.listPlugins();
if (pluginList.length > 0) {
engine.logger.info(__("loaded plugins") + ": " + pluginList.join(", "));
}
engine.startService("processManager");
engine.startService("serviceMonitor");
2018-07-23 09:36:53 +00:00
engine.startService("libraryManager");
engine.startService("codeRunner");
engine.startService("web3");
2018-07-24 08:40:48 +00:00
engine.startService("pipeline");
2018-07-23 13:12:20 +00:00
engine.startService("deployment", {onlyCompile: false});
2018-07-23 09:36:53 +00:00
engine.startService("storage");
2018-07-23 13:12:20 +00:00
engine.startService("codeGenerator");
2018-07-24 15:46:52 +00:00
engine.startService("fileWatcher");
2018-07-23 08:34:21 +00:00
2018-07-23 12:30:58 +00:00
callback();
},
function deploy(callback) {
engine.events.request('deploy:contracts', function (err) {
2018-07-23 13:12:20 +00:00
callback(err);
});
},
function waitForWriteFinish(callback) {
engine.logger.info("Finished deploying".underline);
2018-07-24 15:46:52 +00:00
engine.events.once('outputDone', (err) => {
2018-07-24 08:40:48 +00:00
engine.logger.info(__("finished building").underline);
callback(err);
});
2018-07-23 12:30:58 +00:00
},
function startREPL(callback) {
2018-07-26 15:00:25 +00:00
let repl = new REPL({
env: engine.env,
plugins: engine.plugins,
version: engine.version,
events: engine.events
});
2018-07-24 15:46:52 +00:00
repl.start(callback);
2018-07-23 09:36:53 +00:00
}
], function (err, _result) {
if (err) {
engine.logger.error(err.message);
engine.logger.info(err.stack);
} else {
engine.events.emit('firstDeploymentDone');
}
});
2018-07-23 08:34:21 +00:00
}
graph(options) {
this.context = options.context || [constants.contexts.graph];
options.onlyCompile = true;
2018-06-02 14:06:58 +00:00
const Engine = require('./core/engine.js');
const engine = new Engine({
env: options.env,
version: this.version,
embarkConfig: options.embarkConfig || 'embark.json',
logFile: options.logFile,
context: this.context
});
engine.init();
async.waterfall([
function (callback) {
let pluginList = engine.plugins.listPlugins();
if (pluginList.length > 0) {
2018-05-08 21:49:46 +00:00
engine.logger.info(__("loaded plugins") + ": " + pluginList.join(", "));
}
engine.startService("processManager");
2018-06-01 16:27:12 +00:00
engine.startService("serviceMonitor");
engine.startService("libraryManager");
engine.startService("pipeline");
engine.startService("deployment", {onlyCompile: true});
engine.startService("web3");
2018-05-23 15:16:13 +00:00
engine.startService("codeGenerator");
2018-07-20 17:03:55 +00:00
engine.startService("graph");
engine.events.request('deploy:contracts', callback);
}
], (err) => {
if (err) {
engine.logger.error(err.message);
engine.logger.info(err.stack);
} else {
2018-07-20 17:03:55 +00:00
engine.events.request("graph:create", options, () => {
engine.logger.info(__("Done. %s generated", "./diagram.svg").underline);
});
}
2018-05-23 15:13:51 +00:00
process.exit();
});
}
2018-01-11 14:22:58 +00:00
reset() {
2018-07-31 10:46:19 +00:00
var fs = require('./core/fs.js');
2018-07-25 13:29:18 +00:00
fs.removeSync('./chains.json');
fs.removeSync('.embark/');
fs.removeSync('dist/');
console.log(__("reset done!").green);
2018-01-11 14:22:58 +00:00
}
upload(options) {
this.context = options.context || [constants.contexts.upload, constants.contexts.build];
2018-06-02 14:06:58 +00:00
const Engine = require('./core/engine.js');
const engine = new Engine({
env: options.env,
2018-05-22 19:46:58 +00:00
client: options.client,
locale: options.locale,
version: this.version,
embarkConfig: 'embark.json',
interceptLogs: false,
logFile: options.logFile,
logLevel: options.logLevel,
events: options.events,
logger: options.logger,
config: options.config,
plugins: options.plugins,
context: this.context
});
engine.init();
let platform = engine.config.storageConfig.upload.provider;
async.waterfall([
2018-05-08 21:49:46 +00:00
function startServices(callback) {
2018-05-08 21:49:46 +00:00
engine.startService("processManager");
2018-06-01 16:27:12 +00:00
engine.startService("serviceMonitor");
engine.startService("libraryManager");
2018-05-23 15:16:56 +00:00
engine.startService("codeRunner");
engine.startService("web3");
engine.startService("pipeline");
engine.startService("deployment");
engine.startService("storage");
2018-05-23 15:16:13 +00:00
engine.startService("codeGenerator");
2018-07-30 19:49:58 +00:00
if(options.ensDomain) {
engine.startService("namingSystem");
}
callback();
},
function listLoadedPlugin(callback) {
let pluginList = engine.plugins.listPlugins();
if (pluginList.length > 0) {
2018-05-08 21:49:46 +00:00
engine.logger.info(__("loaded plugins") + ": " + pluginList.join(", "));
}
2018-05-01 13:38:13 +00:00
callback();
},
function deploy(callback) {
engine.events.on('outputDone', function () {
2018-07-07 21:02:46 +00:00
engine.events.request("storage:upload", callback);
});
engine.events.on('check:backOnline:Ethereum', function () {
engine.logger.info(__('Ethereum node detected') + '..');
engine.config.reloadConfig();
engine.events.request('deploy:contracts', function (err) {
if (err) {
return;
}
engine.logger.info(__('Deployment Done'));
});
});
2018-07-30 15:57:00 +00:00
},
function associateToENS(hash, callback) {
if(!options.ensDomain) {
return callback(null, hash);
}
2018-07-31 14:33:10 +00:00
engine.events.request("storage:ens:associate",
2018-07-30 15:57:00 +00:00
{name: options.ensDomain, storageHash: hash}, (err) => {
if (err) {
return callback(err);
}
engine.logger.info(__('ENS association completed for {{hash}} at {{domain}}', {hash, domain: options.ensDomain}));
callback();
});
}
2018-07-30 15:57:00 +00:00
], function (err) {
if (err) {
2018-07-30 19:49:58 +00:00
if (err.message) {
engine.logger.error(err.message);
return engine.logger.debug(err.stack);
}
engine.locale.error(err);
} else {
2018-05-08 21:49:46 +00:00
engine.logger.info((__("finished building DApp and deploying to") + " " + platform).underline);
}
// needed due to child processes
process.exit();
});
}
runTests(options) {
this.context = [constants.contexts.test];
let RunTests = require('./tests/run_tests.js');
RunTests.run(options);
}
2017-03-30 13:16:46 +00:00
}
2016-08-18 00:29:41 +00:00
module.exports = Embark;