embark/lib/core/engine.js

253 lines
8.0 KiB
JavaScript
Raw Normal View History

2018-05-18 21:15:49 +00:00
const async = require('async');
const utils = require('../utils/utils');
2018-06-04 19:36:43 +00:00
const IPC = require('./ipc');
2018-05-22 19:46:02 +00:00
2017-03-30 11:12:39 +00:00
class Engine {
constructor(options) {
this.env = options.env;
this.client = options.client;
this.locale = options.locale;
2017-03-30 11:12:39 +00:00
this.embarkConfig = options.embarkConfig;
this.interceptLogs = options.interceptLogs;
2017-03-31 11:34:43 +00:00
this.version = options.version;
this.logFile = options.logFile;
this.logLevel = options.logLevel;
this.events = options.events;
this.context = options.context;
this.useDashboard = options.useDashboard;
this.webServerConfig = options.webServerConfig;
this.webpackConfigName = options.webpackConfigName;
this.ipcRole = options.ipcRole || 'client';
2017-03-30 11:12:39 +00:00
}
2018-09-05 09:39:09 +00:00
init(_options, callback) {
callback = callback || function() {};
const Events = require('./events.js');
const Logger = require('./logger.js');
const Config = require('./config.js');
2017-03-30 11:38:14 +00:00
let options = _options || {};
this.events = options.events || this.events || new Events();
this.logger = options.logger || new Logger({logLevel: options.logLevel || this.logLevel || 'debug', events: this.events, logFile: this.logFile});
this.config = new Config({env: this.env, logger: this.logger, events: this.events, context: this.context, webServerConfig: this.webServerConfig});
2017-03-30 11:38:14 +00:00
this.config.loadConfigFiles({embarkConfig: this.embarkConfig, interceptLogs: this.interceptLogs});
this.plugins = this.config.plugins;
2018-07-10 12:49:08 +00:00
this.isDev = this.config && this.config.blockchainConfig && (this.config.blockchainConfig.isDev || this.config.blockchainConfig.default);
2017-03-30 11:38:14 +00:00
WIP to merge in other swarm changes Adding swarm to embarkjs. WIP. Add 'auto' setting for geth CORS and websockets origin * 'auto' now supported for `rpcCorsDomain` and `wsOrigins` in the blockchain config. * 'auto' set to the default value in blockchain config for test and demo apps. test add config and contract and add test addFileToPipeline test and registerBeforeDeploy with new arg add more registers but generation one fails in run WIP commit Undo changes to test config. Merge pull request #381 from embark-framework/features/cors-auto Add 'auto' setting for geth CORS and websockets origin fix a bug where upload cmd used plugin name don't error if it's an empty dapp with no contracts yet Merge pull request #383 from embark-framework/no_contracts don't error if it's an empty dapp with no contracts yet remove duplicated entry force zepplein version for travis Merge pull request #384 from embark-framework/chores/test-allpligin-apis Small fixes for plugin APIs intercept logs in the app itself - stopgap fix Merge pull request #385 from embark-framework/console_logs_fix intercept logs in the app itself - stopgap fix * removed unneeded provider property. * add 'swarm' as a provider in the storage.config * update method for swarm service check Merge branch 'develop' into features/add-swarm-to-embarkjs More work to add swarm to embarkjs * added eth-lib to parse result of swarm text * changed "currentStorage" and "currentMessages" to "currentProvider" for consistency. * added protocol to storage config * selectively starts storage service depending on which one is configured in the storage config * run service check for ipfs/swarm prior to uploaded * added swarm methods for embarkjs Updated code based on code review check if testrpc is installed and warn if not Merge pull request #386 from embark-framework/bug_fix/test-rpc-not-installed check if testrpc is installed and warn if not Removed timeout Removed spacer Merge pull request #382 from embark-framework/react-demo Updating embark demo to use react instead of jquery fix on contract add Merge pull request #387 from embark-framework/bug_fix/new-contract-in-empty-dapp Fix adding a contract redeploy with right config on config change fix tests reset watchers after build to make sure files remain watch Merge pull request #389 from embark-framework/bug_fix/file-changes-not-watched Fix files not being watched Merge pull request #388 from embark-framework/bug_fix/changing-contract-config Redeploy with right config on config change Added swarm support in embarkjs and isAvailable for messages/storage * reverted currentProvider back to currentStorage and currentMessages * added `EmbarkJS.Storage.isAvailable` and `EmbarkJS.Messages.isAvailable()` and underlying provider functions for Whisper, Orbit, IPFS, and Swarm * Finished swarm implementation in embarkjs plus cleanup * updated test app storage config to swarm to show swarm config option Merge branch 'develop' into features/add-swarm-to-embarkjs
2018-04-30 05:56:43 +00:00
if (this.interceptLogs || this.interceptLogs === undefined) {
2018-07-27 21:20:36 +00:00
utils.interceptLogs(console, this.logger);
WIP to merge in other swarm changes Adding swarm to embarkjs. WIP. Add 'auto' setting for geth CORS and websockets origin * 'auto' now supported for `rpcCorsDomain` and `wsOrigins` in the blockchain config. * 'auto' set to the default value in blockchain config for test and demo apps. test add config and contract and add test addFileToPipeline test and registerBeforeDeploy with new arg add more registers but generation one fails in run WIP commit Undo changes to test config. Merge pull request #381 from embark-framework/features/cors-auto Add 'auto' setting for geth CORS and websockets origin fix a bug where upload cmd used plugin name don't error if it's an empty dapp with no contracts yet Merge pull request #383 from embark-framework/no_contracts don't error if it's an empty dapp with no contracts yet remove duplicated entry force zepplein version for travis Merge pull request #384 from embark-framework/chores/test-allpligin-apis Small fixes for plugin APIs intercept logs in the app itself - stopgap fix Merge pull request #385 from embark-framework/console_logs_fix intercept logs in the app itself - stopgap fix * removed unneeded provider property. * add 'swarm' as a provider in the storage.config * update method for swarm service check Merge branch 'develop' into features/add-swarm-to-embarkjs More work to add swarm to embarkjs * added eth-lib to parse result of swarm text * changed "currentStorage" and "currentMessages" to "currentProvider" for consistency. * added protocol to storage config * selectively starts storage service depending on which one is configured in the storage config * run service check for ipfs/swarm prior to uploaded * added swarm methods for embarkjs Updated code based on code review check if testrpc is installed and warn if not Merge pull request #386 from embark-framework/bug_fix/test-rpc-not-installed check if testrpc is installed and warn if not Removed timeout Removed spacer Merge pull request #382 from embark-framework/react-demo Updating embark demo to use react instead of jquery fix on contract add Merge pull request #387 from embark-framework/bug_fix/new-contract-in-empty-dapp Fix adding a contract redeploy with right config on config change fix tests reset watchers after build to make sure files remain watch Merge pull request #389 from embark-framework/bug_fix/file-changes-not-watched Fix files not being watched Merge pull request #388 from embark-framework/bug_fix/changing-contract-config Redeploy with right config on config change Added swarm support in embarkjs and isAvailable for messages/storage * reverted currentProvider back to currentStorage and currentMessages * added `EmbarkJS.Storage.isAvailable` and `EmbarkJS.Messages.isAvailable()` and underlying provider functions for Whisper, Orbit, IPFS, and Swarm * Finished swarm implementation in embarkjs plus cleanup * updated test app storage config to swarm to show swarm config option Merge branch 'develop' into features/add-swarm-to-embarkjs
2018-04-30 05:56:43 +00:00
}
2018-08-08 12:42:45 +00:00
this.ipc = new IPC({logger: this.logger, ipcRole: this.ipcRole});
if (this.ipc.isClient()) {
return this.ipc.connect((_err) => {
callback();
});
} else if (this.ipc.isServer()) {
this.ipc.serve();
return callback();
}
callback();
WIP to merge in other swarm changes Adding swarm to embarkjs. WIP. Add 'auto' setting for geth CORS and websockets origin * 'auto' now supported for `rpcCorsDomain` and `wsOrigins` in the blockchain config. * 'auto' set to the default value in blockchain config for test and demo apps. test add config and contract and add test addFileToPipeline test and registerBeforeDeploy with new arg add more registers but generation one fails in run WIP commit Undo changes to test config. Merge pull request #381 from embark-framework/features/cors-auto Add 'auto' setting for geth CORS and websockets origin fix a bug where upload cmd used plugin name don't error if it's an empty dapp with no contracts yet Merge pull request #383 from embark-framework/no_contracts don't error if it's an empty dapp with no contracts yet remove duplicated entry force zepplein version for travis Merge pull request #384 from embark-framework/chores/test-allpligin-apis Small fixes for plugin APIs intercept logs in the app itself - stopgap fix Merge pull request #385 from embark-framework/console_logs_fix intercept logs in the app itself - stopgap fix * removed unneeded provider property. * add 'swarm' as a provider in the storage.config * update method for swarm service check Merge branch 'develop' into features/add-swarm-to-embarkjs More work to add swarm to embarkjs * added eth-lib to parse result of swarm text * changed "currentStorage" and "currentMessages" to "currentProvider" for consistency. * added protocol to storage config * selectively starts storage service depending on which one is configured in the storage config * run service check for ipfs/swarm prior to uploaded * added swarm methods for embarkjs Updated code based on code review check if testrpc is installed and warn if not Merge pull request #386 from embark-framework/bug_fix/test-rpc-not-installed check if testrpc is installed and warn if not Removed timeout Removed spacer Merge pull request #382 from embark-framework/react-demo Updating embark demo to use react instead of jquery fix on contract add Merge pull request #387 from embark-framework/bug_fix/new-contract-in-empty-dapp Fix adding a contract redeploy with right config on config change fix tests reset watchers after build to make sure files remain watch Merge pull request #389 from embark-framework/bug_fix/file-changes-not-watched Fix files not being watched Merge pull request #388 from embark-framework/bug_fix/changing-contract-config Redeploy with right config on config change Added swarm support in embarkjs and isAvailable for messages/storage * reverted currentProvider back to currentStorage and currentMessages * added `EmbarkJS.Storage.isAvailable` and `EmbarkJS.Messages.isAvailable()` and underlying provider functions for Whisper, Orbit, IPFS, and Swarm * Finished swarm implementation in embarkjs plus cleanup * updated test app storage config to swarm to show swarm config option Merge branch 'develop' into features/add-swarm-to-embarkjs
2018-04-30 05:56:43 +00:00
}
2017-12-16 20:39:30 +00:00
registerModule(moduleName, options) {
2018-05-30 16:26:49 +00:00
this.plugins.loadInternalPlugin(moduleName, options || {});
2017-12-16 20:39:30 +00:00
}
2017-03-30 11:38:14 +00:00
startService(serviceName, _options) {
let options = _options || {};
let services = {
2018-06-01 16:27:12 +00:00
"serviceMonitor": this.serviceMonitor,
2017-03-30 11:38:14 +00:00
"pipeline": this.pipelineService,
2018-05-23 15:16:56 +00:00
"codeRunner": this.codeRunnerService,
"codeGenerator": this.codeGeneratorService,
2017-03-30 11:38:14 +00:00
"deployment": this.deploymentService,
"fileWatcher": this.fileWatchService,
"webServer": this.webServerService,
"namingSystem": this.namingSystem,
2018-08-31 08:24:45 +00:00
"console": this.console,
"web3": this.web3Service,
"libraryManager": this.libraryManagerService,
"processManager": this.processManagerService,
2018-07-20 17:03:55 +00:00
"storage": this.storageService,
2018-08-01 16:08:47 +00:00
"graph": this.graphService,
2018-09-09 17:37:55 +00:00
"codeCoverage": this.codeCoverageService,
"pluginCommand": this.pluginCommandService
2017-03-30 11:38:14 +00:00
};
let service = services[serviceName];
if (!service) {
throw new Error("unknown service: " + serviceName);
}
// need to be careful with circular references due to passing the web3 object
//this.logger.trace("calling: " + serviceName + "(" + JSON.stringify(options) + ")");
return service.apply(this, [options]);
}
processManagerService(_options) {
2018-07-27 21:33:50 +00:00
const ProcessManager = require('./processes/processManager.js');
2018-07-26 16:37:33 +00:00
this.processManager = new ProcessManager({
events: this.events,
logger: this.logger,
plugins: this.plugins
});
}
2018-07-20 17:03:55 +00:00
graphService(_options) {
2018-07-30 18:31:24 +00:00
this.registerModule('graph');
2018-07-20 17:03:55 +00:00
}
2017-10-14 14:13:30 +00:00
pipelineService(_options) {
const self = this;
this.registerModule('pipeline', {
webpackConfigName: this.webpackConfigName
});
this.events.on('code-generator-ready', function () {
self.events.request('code', function (abi, contractsJSON) {
self.events.request('pipeline:build', {abi, contractsJSON}, () => {
self.events.emit('outputDone');
});
});
2017-03-30 11:38:14 +00:00
});
}
2017-03-30 11:38:14 +00:00
2018-06-01 16:27:12 +00:00
serviceMonitor() {
const self = this;
const ServicesMonitor = require('./services_monitor.js');
2018-06-01 16:27:12 +00:00
this.servicesMonitor = new ServicesMonitor({events: this.events, logger: this.logger, plugins: this.plugins});
this.servicesMonitor.addCheck('embarkVersion', function (cb) {
return cb({name: 'Embark ' + self.version, status: 'on'});
}, 0);
this.servicesMonitor.startMonitor();
}
2018-09-09 17:37:55 +00:00
pluginCommandService() {
2018-09-10 17:08:17 +00:00
this.registerModule('plugin_cmd', {embarkConfigFile: this.embarkConfig, embarkConfig: this.config.embarkConfig, packageFile: 'package.json'});
2018-09-09 17:37:55 +00:00
}
2018-05-28 15:10:09 +00:00
namingSystem(_options) {
2018-05-30 16:26:49 +00:00
this.registerModule('ens');
2018-05-28 15:10:09 +00:00
}
2018-08-31 08:24:45 +00:00
console(_options) {
this.registerModule('console', {
events: this.events,
plugins: this.plugins,
version: this.version,
ipc: this.ipc,
logger: this.logger,
config: this.config
});
}
2018-05-23 15:16:56 +00:00
codeRunnerService(_options) {
2018-07-20 16:37:50 +00:00
const CodeRunner = require('./modules/coderunner/codeRunner.js');
2018-05-21 21:22:19 +00:00
this.codeRunner = new CodeRunner({
2018-08-28 13:43:52 +00:00
config: this.config,
2018-05-23 15:16:56 +00:00
plugins: this.plugins,
events: this.events,
2018-08-08 12:42:45 +00:00
logger: this.logger,
ipc: this.ipc
2018-05-21 21:22:19 +00:00
});
2018-05-23 15:16:56 +00:00
}
codeGeneratorService(_options) {
let self = this;
2018-05-21 21:22:19 +00:00
2018-07-27 18:54:25 +00:00
this.registerModule('code_generator', {plugins: self.plugins, env: self.env});
2018-05-23 15:16:13 +00:00
const generateCode = function () {
2018-07-27 18:54:25 +00:00
self.events.request("code-generator:embarkjs:build", () => {
2018-01-10 15:43:25 +00:00
self.events.emit('code-generator-ready');
});
2017-03-30 11:38:14 +00:00
};
const cargo = async.cargo((_tasks, callback) => {
generateCode();
2018-05-15 18:42:06 +00:00
self.events.once('outputDone', callback);
});
2018-05-30 10:56:51 +00:00
const addToCargo = function () {
cargo.push({});
2018-05-15 18:42:06 +00:00
};
this.events.on('contractsDeployed', addToCargo);
this.events.on('blockchainDisabled', addToCargo);
this.events.on('asset-changed', addToCargo);
}
2017-03-04 02:48:32 +00:00
2017-03-30 11:38:14 +00:00
deploymentService(options) {
let self = this;
2017-12-16 20:39:30 +00:00
2018-08-30 20:11:34 +00:00
this.registerModule('compiler', {plugins: self.plugins, disableOptimizations: options.disableOptimizations});
2018-08-08 12:42:45 +00:00
this.registerModule('solidity', {ipc: self.ipc, useDashboard: this.useDashboard});
2018-05-30 16:26:49 +00:00
this.registerModule('vyper');
this.registerModule('profiler');
2018-08-24 13:25:47 +00:00
this.registerModule('deploytracker', {trackContracts: options.trackContracts});
2018-05-30 16:26:49 +00:00
this.registerModule('specialconfigs');
2018-08-08 12:42:45 +00:00
this.registerModule('console_listener', {ipc: self.ipc});
this.registerModule('contracts_manager', {compileOnceOnly: options.compileOnceOnly});
2018-07-27 20:03:15 +00:00
this.registerModule('deployment', {plugins: this.plugins, onlyCompile: options.onlyCompile});
2017-03-30 11:38:14 +00:00
WIP to merge in other swarm changes Adding swarm to embarkjs. WIP. Add 'auto' setting for geth CORS and websockets origin * 'auto' now supported for `rpcCorsDomain` and `wsOrigins` in the blockchain config. * 'auto' set to the default value in blockchain config for test and demo apps. test add config and contract and add test addFileToPipeline test and registerBeforeDeploy with new arg add more registers but generation one fails in run WIP commit Undo changes to test config. Merge pull request #381 from embark-framework/features/cors-auto Add 'auto' setting for geth CORS and websockets origin fix a bug where upload cmd used plugin name don't error if it's an empty dapp with no contracts yet Merge pull request #383 from embark-framework/no_contracts don't error if it's an empty dapp with no contracts yet remove duplicated entry force zepplein version for travis Merge pull request #384 from embark-framework/chores/test-allpligin-apis Small fixes for plugin APIs intercept logs in the app itself - stopgap fix Merge pull request #385 from embark-framework/console_logs_fix intercept logs in the app itself - stopgap fix * removed unneeded provider property. * add 'swarm' as a provider in the storage.config * update method for swarm service check Merge branch 'develop' into features/add-swarm-to-embarkjs More work to add swarm to embarkjs * added eth-lib to parse result of swarm text * changed "currentStorage" and "currentMessages" to "currentProvider" for consistency. * added protocol to storage config * selectively starts storage service depending on which one is configured in the storage config * run service check for ipfs/swarm prior to uploaded * added swarm methods for embarkjs Updated code based on code review check if testrpc is installed and warn if not Merge pull request #386 from embark-framework/bug_fix/test-rpc-not-installed check if testrpc is installed and warn if not Removed timeout Removed spacer Merge pull request #382 from embark-framework/react-demo Updating embark demo to use react instead of jquery fix on contract add Merge pull request #387 from embark-framework/bug_fix/new-contract-in-empty-dapp Fix adding a contract redeploy with right config on config change fix tests reset watchers after build to make sure files remain watch Merge pull request #389 from embark-framework/bug_fix/file-changes-not-watched Fix files not being watched Merge pull request #388 from embark-framework/bug_fix/changing-contract-config Redeploy with right config on config change Added swarm support in embarkjs and isAvailable for messages/storage * reverted currentProvider back to currentStorage and currentMessages * added `EmbarkJS.Storage.isAvailable` and `EmbarkJS.Messages.isAvailable()` and underlying provider functions for Whisper, Orbit, IPFS, and Swarm * Finished swarm implementation in embarkjs plus cleanup * updated test app storage config to swarm to show swarm config option Merge branch 'develop' into features/add-swarm-to-embarkjs
2018-04-30 05:56:43 +00:00
this.events.on('file-event', function (fileType) {
2018-05-15 19:41:24 +00:00
clearTimeout(self.fileTimeout);
self.fileTimeout = setTimeout(() => {
// TODO: still need to redeploy contracts because the original contracts
// config is being corrupted
self.config.reloadConfig();
2018-05-15 19:41:24 +00:00
if (fileType === 'asset') {
// Throttle file changes so we re-write only once for all files
2018-05-15 18:42:06 +00:00
self.events.emit('asset-changed', self.contractsManager);
2018-05-15 19:41:24 +00:00
}
// TODO: for now need to deploy on asset changes as well
// because the contractsManager config is corrupted after a deploy
if (fileType === 'contract' || fileType === 'config') {
self.events.request('deploy:contracts', () => {});
2018-05-15 19:41:24 +00:00
}
}, 50);
2017-03-30 11:12:39 +00:00
});
}
fileWatchService() {
this.registerModule('watcher');
this.events.request('watcher:start');
2017-03-30 11:38:14 +00:00
}
webServerService() {
this.registerModule('webserver');
2017-03-30 11:38:14 +00:00
}
storageService(_options) {
2018-07-08 20:41:37 +00:00
this.registerModule('storage', {plugins: this.plugins});
this.registerModule('ipfs');
this.registerModule('swarm');
}
2017-03-30 11:38:14 +00:00
web3Service(options) {
2018-07-20 12:21:23 +00:00
this.registerModule('blockchain_process', {
locale: this.locale,
2018-08-23 16:54:43 +00:00
isDev: this.isDev,
ipc: this.ipc
2018-07-20 12:21:23 +00:00
});
this.registerModule('blockchain_connector', {
2018-05-18 18:58:05 +00:00
isDev: this.isDev,
2018-05-18 22:31:47 +00:00
web3: options.web3
});
this.registerModule('whisper');
2017-03-30 11:38:14 +00:00
}
2017-12-30 22:07:13 +00:00
libraryManagerService(_options) {
this.registerModule('library_manager');
}
2018-08-01 16:08:47 +00:00
codeCoverageService(_options) {
this.registerModule('coverage');
}
2017-03-30 11:38:14 +00:00
}
module.exports = Engine;