2018-04-24 18:42:56 +00:00
|
|
|
const fs = require('./fs.js');
|
|
|
|
const utils = require('../utils/utils.js');
|
|
|
|
const constants = require('../constants');
|
2017-03-31 11:34:43 +00:00
|
|
|
|
|
|
|
// TODO: pass other params like blockchainConfig, contract files, etc..
|
|
|
|
var Plugin = function(options) {
|
|
|
|
this.name = options.name;
|
2017-12-16 20:39:30 +00:00
|
|
|
this.isInternal = options.isInternal;
|
2017-03-31 11:34:43 +00:00
|
|
|
this.pluginModule = options.pluginModule;
|
|
|
|
this.pluginPath = options.pluginPath;
|
|
|
|
this.pluginConfig = options.pluginConfig;
|
|
|
|
this.shouldInterceptLogs = options.interceptLogs;
|
|
|
|
this.clientWeb3Providers = [];
|
2018-01-17 23:04:19 +00:00
|
|
|
this.beforeDeploy = [];
|
2017-03-31 11:34:43 +00:00
|
|
|
this.contractsGenerators = [];
|
|
|
|
this.pipeline = [];
|
|
|
|
this.pipelineFiles = [];
|
|
|
|
this.console = [];
|
|
|
|
this.contractsConfigs = [];
|
|
|
|
this.contractsFiles = [];
|
|
|
|
this.compilers = [];
|
|
|
|
this.serviceChecks = [];
|
2018-05-11 19:49:06 +00:00
|
|
|
this.dappGenerators = [];
|
2017-03-31 11:34:43 +00:00
|
|
|
this.pluginTypes = [];
|
2018-07-08 17:40:06 +00:00
|
|
|
this.uploadCmds = [];
|
2018-03-13 10:23:12 +00:00
|
|
|
this.apiCalls = [];
|
2018-01-10 16:15:32 +00:00
|
|
|
this.imports = [];
|
2017-12-28 17:16:50 +00:00
|
|
|
this.embarkjs_code = [];
|
2017-12-28 22:42:25 +00:00
|
|
|
this.embarkjs_init_code = {};
|
2018-08-28 13:43:52 +00:00
|
|
|
this.embarkjs_init_console_code = {};
|
2018-05-20 14:46:36 +00:00
|
|
|
this.afterContractsDeployActions = [];
|
2018-05-20 23:29:26 +00:00
|
|
|
this.onDeployActions = [];
|
2018-05-28 22:49:18 +00:00
|
|
|
this.eventActions = {};
|
2018-06-19 18:47:50 +00:00
|
|
|
this._loggerObject = options.logger;
|
|
|
|
this.logger = this._loggerObject; // Might get changed if we do intercept
|
2017-03-31 11:34:43 +00:00
|
|
|
this.events = options.events;
|
|
|
|
this.config = options.config;
|
2018-03-13 10:23:12 +00:00
|
|
|
this.plugins = options.plugins;
|
2018-05-19 02:40:47 +00:00
|
|
|
this.env = options.env;
|
2018-04-24 19:53:19 +00:00
|
|
|
this.loaded = false;
|
2018-04-25 14:34:17 +00:00
|
|
|
this.currentContext = options.context;
|
|
|
|
this.acceptedContext = options.pluginConfig.context || [constants.contexts.any];
|
|
|
|
|
|
|
|
if (!Array.isArray(this.currentContext)) {
|
|
|
|
this.currentContext = [this.currentContext];
|
|
|
|
}
|
|
|
|
if (!Array.isArray(this.acceptedContext)) {
|
|
|
|
this.acceptedContext = [this.acceptedContext];
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-06-19 18:47:50 +00:00
|
|
|
Plugin.prototype._log = function(type) {
|
|
|
|
this._loggerObject[type](this.name + ':', ...[].slice.call(arguments, 1));
|
|
|
|
};
|
|
|
|
|
|
|
|
Plugin.prototype.setUpLogger = function () {
|
|
|
|
this.logger = {
|
|
|
|
log: this._log.bind(this, 'log'),
|
|
|
|
warn: this._log.bind(this, 'warn'),
|
|
|
|
error: this._log.bind(this, 'error'),
|
|
|
|
info: this._log.bind(this, 'info'),
|
|
|
|
debug: this._log.bind(this, 'debug'),
|
|
|
|
trace: this._log.bind(this, 'trace'),
|
|
|
|
dir: this._log.bind(this, 'dir')
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2018-04-25 14:34:17 +00:00
|
|
|
Plugin.prototype.isContextValid = function() {
|
|
|
|
if (this.currentContext.includes(constants.contexts.any) || this.acceptedContext.includes(constants.contexts.any)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return this.acceptedContext.some(context => {
|
|
|
|
return this.currentContext.includes(context);
|
|
|
|
});
|
2017-03-31 11:34:43 +00:00
|
|
|
};
|
|
|
|
|
2018-04-25 14:57:23 +00:00
|
|
|
Plugin.prototype.hasContext = function(context) {
|
|
|
|
return this.currentContext.includes(context);
|
|
|
|
};
|
|
|
|
|
2018-04-25 14:34:17 +00:00
|
|
|
Plugin.prototype.loadPlugin = function() {
|
|
|
|
if (!this.isContextValid()) {
|
2018-10-19 00:21:04 +00:00
|
|
|
this.logger.warn(__('Plugin {{name}} can only be loaded in the context of "{{contexts}}"', {name: this.name, contexts: this.acceptedContext.join(', ')}));
|
2018-04-25 14:34:17 +00:00
|
|
|
return false;
|
2018-04-24 18:42:56 +00:00
|
|
|
}
|
2018-04-24 19:53:19 +00:00
|
|
|
this.loaded = true;
|
2017-02-06 11:42:58 +00:00
|
|
|
if (this.shouldInterceptLogs) {
|
2018-06-19 18:47:50 +00:00
|
|
|
this.setUpLogger();
|
2017-02-06 11:42:58 +00:00
|
|
|
}
|
2018-10-19 00:21:04 +00:00
|
|
|
if (typeof this.pluginModule === 'object' && typeof this.pluginModule.default === 'function' && this.pluginModule.__esModule){
|
|
|
|
this.pluginModule = this.pluginModule.default;
|
|
|
|
return new this.pluginModule(this);
|
|
|
|
}
|
2017-03-31 11:34:43 +00:00
|
|
|
(this.pluginModule.call(this, this));
|
2016-12-07 02:33:31 +00:00
|
|
|
};
|
|
|
|
|
2017-12-16 20:39:30 +00:00
|
|
|
Plugin.prototype.loadInternalPlugin = function() {
|
2017-12-16 21:05:46 +00:00
|
|
|
new this.pluginModule(this, this.pluginConfig); /*eslint no-new: "off"*/
|
2017-12-16 20:39:30 +00:00
|
|
|
};
|
|
|
|
|
2017-03-31 11:34:43 +00:00
|
|
|
Plugin.prototype.loadPluginFile = function(filename) {
|
2017-01-26 11:34:00 +00:00
|
|
|
return fs.readFileSync(this.pathToFile(filename)).toString();
|
|
|
|
};
|
|
|
|
|
2017-03-31 11:34:43 +00:00
|
|
|
Plugin.prototype.pathToFile = function(filename) {
|
2017-12-30 20:52:51 +00:00
|
|
|
if (!this.pluginPath) {
|
|
|
|
throw new Error('pluginPath not defined for plugin: ' + this.name);
|
|
|
|
}
|
2017-02-18 19:10:01 +00:00
|
|
|
return utils.joinPath(this.pluginPath, filename);
|
2017-01-26 11:34:00 +00:00
|
|
|
};
|
|
|
|
|
2016-12-07 02:33:31 +00:00
|
|
|
// TODO: add deploy provider
|
2017-03-31 11:34:43 +00:00
|
|
|
Plugin.prototype.registerClientWeb3Provider = function(cb) {
|
2016-12-07 02:33:31 +00:00
|
|
|
this.clientWeb3Providers.push(cb);
|
2018-05-31 17:12:56 +00:00
|
|
|
this.addPluginType('clientWeb3Provider');
|
2018-01-17 23:04:19 +00:00
|
|
|
};
|
|
|
|
|
2017-03-31 11:34:43 +00:00
|
|
|
Plugin.prototype.registerContractsGeneration = function(cb) {
|
2016-12-07 02:33:31 +00:00
|
|
|
this.contractsGenerators.push(cb);
|
2018-05-31 17:12:56 +00:00
|
|
|
this.addPluginType('contractGeneration');
|
2016-12-07 02:33:31 +00:00
|
|
|
};
|
|
|
|
|
2017-03-31 11:34:43 +00:00
|
|
|
Plugin.prototype.registerPipeline = function(matcthingFiles, cb) {
|
2016-12-10 15:20:04 +00:00
|
|
|
// TODO: generate error for more than one pipeline per plugin
|
2017-01-15 19:30:41 +00:00
|
|
|
this.pipeline.push({matcthingFiles: matcthingFiles, cb: cb});
|
2018-05-31 17:12:56 +00:00
|
|
|
this.addPluginType('pipeline');
|
2016-12-10 15:20:04 +00:00
|
|
|
};
|
|
|
|
|
2018-05-11 19:49:06 +00:00
|
|
|
Plugin.prototype.registerDappGenerator = function(framework, cb){
|
|
|
|
this.dappGenerators.push({framework: framework, cb: cb});
|
|
|
|
this.pluginTypes.push('dappGenerator');
|
2018-05-10 21:02:47 +00:00
|
|
|
};
|
2018-05-09 21:02:17 +00:00
|
|
|
|
2018-05-09 21:02:17 +00:00
|
|
|
Plugin.prototype.registerCustomType = function(type){
|
|
|
|
this.pluginTypes.push(type);
|
|
|
|
}
|
|
|
|
|
2017-03-31 11:34:43 +00:00
|
|
|
Plugin.prototype.addFileToPipeline = function(file, intendedPath, options) {
|
2017-02-03 11:30:08 +00:00
|
|
|
this.pipelineFiles.push({file: file, intendedPath: intendedPath, options: options});
|
2018-05-31 17:12:56 +00:00
|
|
|
this.addPluginType('pipelineFiles');
|
2017-01-26 11:34:00 +00:00
|
|
|
};
|
|
|
|
|
2017-03-31 11:34:43 +00:00
|
|
|
Plugin.prototype.addContractFile = function(file) {
|
2018-06-18 15:25:43 +00:00
|
|
|
if (this.isInternal) {
|
|
|
|
throw new Error("this API cannot work for internal modules. please use an event command instead: config:contractsFiles:add");
|
|
|
|
}
|
2017-01-26 11:34:00 +00:00
|
|
|
this.contractsFiles.push(file);
|
2018-05-31 17:12:56 +00:00
|
|
|
this.addPluginType('contractFiles');
|
2017-01-26 11:34:00 +00:00
|
|
|
};
|
|
|
|
|
2017-03-31 11:34:43 +00:00
|
|
|
Plugin.prototype.registerConsoleCommand = function(cb) {
|
2017-01-16 12:00:41 +00:00
|
|
|
this.console.push(cb);
|
2018-05-31 17:12:56 +00:00
|
|
|
this.addPluginType('console');
|
2017-01-16 12:00:41 +00:00
|
|
|
};
|
|
|
|
|
2017-12-18 14:37:16 +00:00
|
|
|
// TODO: this only works for services done on startup
|
2017-03-31 11:34:43 +00:00
|
|
|
Plugin.prototype.registerServiceCheck = function(checkName, checkFn, time) {
|
2017-03-16 11:31:52 +00:00
|
|
|
this.serviceChecks.push({checkName: checkName, checkFn: checkFn, time: time});
|
2018-05-31 17:12:56 +00:00
|
|
|
this.addPluginType('serviceChecks');
|
2017-03-16 11:31:52 +00:00
|
|
|
};
|
|
|
|
|
2017-03-31 11:34:43 +00:00
|
|
|
Plugin.prototype.has = function(pluginType) {
|
2016-12-07 02:33:31 +00:00
|
|
|
return this.pluginTypes.indexOf(pluginType) >= 0;
|
|
|
|
};
|
|
|
|
|
2018-05-31 17:12:56 +00:00
|
|
|
Plugin.prototype.addPluginType = function(pluginType) {
|
|
|
|
this.pluginTypes.push(pluginType);
|
2018-07-24 12:29:06 +00:00
|
|
|
this.pluginTypes = Array.from(new Set(this.pluginTypes));
|
2018-05-31 17:12:56 +00:00
|
|
|
};
|
|
|
|
|
2017-03-31 11:34:43 +00:00
|
|
|
Plugin.prototype.generateProvider = function(args) {
|
|
|
|
return this.clientWeb3Providers.map(function(cb) {
|
2016-12-07 02:33:31 +00:00
|
|
|
return cb.call(this, args);
|
|
|
|
}).join("\n");
|
|
|
|
};
|
|
|
|
|
2017-03-31 11:34:43 +00:00
|
|
|
Plugin.prototype.generateContracts = function(args) {
|
|
|
|
return this.contractsGenerators.map(function(cb) {
|
2016-12-07 02:33:31 +00:00
|
|
|
return cb.call(this, args);
|
|
|
|
}).join("\n");
|
|
|
|
};
|
|
|
|
|
2017-03-31 11:34:43 +00:00
|
|
|
Plugin.prototype.registerContractConfiguration = function(config) {
|
2017-01-26 11:34:00 +00:00
|
|
|
this.contractsConfigs.push(config);
|
2018-05-31 17:12:56 +00:00
|
|
|
this.addPluginType('contractsConfig');
|
2017-01-26 11:34:00 +00:00
|
|
|
};
|
|
|
|
|
2017-03-31 11:34:43 +00:00
|
|
|
Plugin.prototype.registerCompiler = function(extension, cb) {
|
2017-01-29 02:31:09 +00:00
|
|
|
this.compilers.push({extension: extension, cb: cb});
|
2018-05-31 17:12:56 +00:00
|
|
|
this.addPluginType('compilers');
|
2017-01-29 02:31:09 +00:00
|
|
|
};
|
|
|
|
|
2018-07-08 17:40:06 +00:00
|
|
|
Plugin.prototype.registerUploadCommand = function(cmd, cb) {
|
|
|
|
this.uploadCmds.push({cmd: cmd, cb: cb});
|
|
|
|
this.addPluginType('uploadCmds');
|
|
|
|
};
|
|
|
|
|
2017-12-28 17:16:50 +00:00
|
|
|
Plugin.prototype.addCodeToEmbarkJS = function(code) {
|
|
|
|
this.embarkjs_code.push(code);
|
2018-05-31 17:12:56 +00:00
|
|
|
this.addPluginType('embarkjsCode');
|
2017-12-28 17:16:50 +00:00
|
|
|
};
|
|
|
|
|
2017-12-28 22:42:25 +00:00
|
|
|
Plugin.prototype.addProviderInit = function(providerType, code, initCondition) {
|
|
|
|
this.embarkjs_init_code[providerType] = this.embarkjs_init_code[providerType] || [];
|
|
|
|
this.embarkjs_init_code[providerType].push([code, initCondition]);
|
2018-05-31 17:12:56 +00:00
|
|
|
this.addPluginType('initCode');
|
2017-12-28 22:42:25 +00:00
|
|
|
};
|
|
|
|
|
2018-08-28 13:43:52 +00:00
|
|
|
Plugin.prototype.addConsoleProviderInit = function(providerType, code, initCondition) {
|
|
|
|
this.embarkjs_init_console_code[providerType] = this.embarkjs_init_console_code[providerType] || [];
|
|
|
|
this.embarkjs_init_console_code[providerType].push([code, initCondition]);
|
|
|
|
this.addPluginType('initConsoleCode');
|
|
|
|
};
|
|
|
|
|
2018-01-10 16:15:32 +00:00
|
|
|
Plugin.prototype.registerImportFile = function(importName, importLocation) {
|
|
|
|
this.imports.push([importName, importLocation]);
|
2018-05-31 17:12:56 +00:00
|
|
|
this.addPluginType('imports');
|
2018-01-10 16:15:32 +00:00
|
|
|
};
|
|
|
|
|
2018-05-28 22:49:18 +00:00
|
|
|
Plugin.prototype.registerActionForEvent = function(eventName, cb) {
|
|
|
|
if (!this.eventActions[eventName]) {
|
|
|
|
this.eventActions[eventName] = [];
|
|
|
|
}
|
|
|
|
this.eventActions[eventName].push(cb);
|
2018-05-31 17:12:56 +00:00
|
|
|
this.addPluginType('eventActions');
|
2018-05-29 13:58:19 +00:00
|
|
|
};
|
2018-05-28 22:49:18 +00:00
|
|
|
|
2018-03-13 10:23:12 +00:00
|
|
|
Plugin.prototype.registerAPICall = function(method, endpoint, cb) {
|
2018-08-02 16:45:59 +00:00
|
|
|
this.apiCalls.push({method, endpoint, cb});
|
2018-08-01 15:14:02 +00:00
|
|
|
this.addPluginType('apiCalls');
|
2018-08-02 16:45:59 +00:00
|
|
|
this.events.emit('plugins:register:api', {method, endpoint, cb});
|
2018-08-01 15:14:02 +00:00
|
|
|
};
|
|
|
|
|
2017-03-31 11:34:43 +00:00
|
|
|
Plugin.prototype.runFilePipeline = function() {
|
|
|
|
var self = this;
|
2017-01-26 11:34:00 +00:00
|
|
|
|
2017-03-31 11:34:43 +00:00
|
|
|
return this.pipelineFiles.map(function(file) {
|
|
|
|
var obj = {};
|
|
|
|
obj.filename = file.file.replace('./','');
|
|
|
|
obj.content = self.loadPluginFile(file.file).toString();
|
|
|
|
obj.intendedPath = file.intendedPath;
|
|
|
|
obj.options = file.options;
|
|
|
|
obj.path = self.pathToFile(obj.filename);
|
2017-01-26 11:34:00 +00:00
|
|
|
|
2017-03-31 11:34:43 +00:00
|
|
|
return obj;
|
2017-01-26 11:34:00 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2017-03-31 11:34:43 +00:00
|
|
|
Plugin.prototype.runPipeline = function(args) {
|
2017-02-07 02:08:11 +00:00
|
|
|
// TODO: should iterate the pipelines
|
2017-03-31 11:34:43 +00:00
|
|
|
var pipeline = this.pipeline[0];
|
|
|
|
var shouldRunPipeline = utils.fileMatchesPattern(pipeline.matcthingFiles, args.targetFile);
|
2017-01-15 19:30:41 +00:00
|
|
|
if (shouldRunPipeline) {
|
|
|
|
return pipeline.cb.call(this, args);
|
|
|
|
}
|
2018-08-30 13:53:04 +00:00
|
|
|
return args.source;
|
2016-12-10 15:20:04 +00:00
|
|
|
};
|
|
|
|
|
2016-12-07 02:33:31 +00:00
|
|
|
module.exports = Plugin;
|