embark-area-51/lib/core/plugin.js

254 lines
8.0 KiB
JavaScript
Raw Normal View History

const fs = require('./fs.js');
const utils = require('../utils/utils.js');
const constants = require('../constants');
const _ = require('underscore')
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 = [];
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 = [];
this.pluginTypes = [];
2017-12-27 00:55:42 +00:00
this.uploadCmds = [];
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 = {};
this.afterContractsDeployActions = [];
this.onDeployActions = [];
this.eventActions = {};
2017-03-31 11:34:43 +00:00
this.logger = options.logger;
this.events = options.events;
this.config = options.config;
2018-05-19 02:40:47 +00:00
this.env = options.env;
this.loaded = false;
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];
}
};
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
};
Plugin.prototype.hasContext = function(context) {
return this.currentContext.includes(context);
};
Plugin.prototype.loadPlugin = function() {
if (!this.isContextValid()) {
console.log(this.acceptedContext);
2018-05-08 21:49:46 +00:00
this.logger.warn(__('Plugin {{name}} can only be loaded in the context of "{{contextes}}"', {name: this.name, contextes: this.acceptedContext.join(', ')}));
return false;
}
this.loaded = true;
2017-02-06 11:42:58 +00:00
if (this.shouldInterceptLogs) {
this.interceptLogs(this.pluginModule);
}
2017-03-31 11:34:43 +00:00
(this.pluginModule.call(this, this));
};
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) {
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
};
2017-03-31 11:34:43 +00:00
Plugin.prototype.interceptLogs = function(context) {
var self = this;
2017-02-06 11:42:58 +00:00
// TODO: this is a bit nasty, figure out a better way
context.console = context.console || console;
//context.console.error = function(txt) {
// // TODO: logger should support an array instead of a single argument
// //self.logger.error.apply(self.logger, arguments);
// self.logger.error(self.name + " > " + txt);
//};
2017-03-31 11:34:43 +00:00
context.console.log = function(txt) {
2017-01-16 12:00:41 +00:00
self.logger.info(self.name + " > " + txt);
};
2017-03-31 11:34:43 +00:00
context.console.warn = function(txt) {
2017-01-16 12:00:41 +00:00
self.logger.warn(self.name + " > " + txt);
};
2017-03-31 11:34:43 +00:00
context.console.info = function(txt) {
2017-01-16 12:00:41 +00:00
self.logger.info(self.name + " > " + txt);
};
2017-03-31 11:34:43 +00:00
context.console.debug = function(txt) {
2017-01-26 11:34:00 +00:00
// TODO: ue JSON.stringify
2017-01-16 12:00:41 +00:00
self.logger.debug(self.name + " > " + txt);
};
2017-03-31 11:34:43 +00:00
context.console.trace = function(txt) {
2017-01-16 12:00:41 +00:00
self.logger.trace(self.name + " > " + txt);
};
context.console.dir = function(txt) {
self.logger.dir(txt);
};
};
// TODO: add deploy provider
2017-03-31 11:34:43 +00:00
Plugin.prototype.registerClientWeb3Provider = function(cb) {
this.clientWeb3Providers.push(cb);
this.pluginTypes.push('clientWeb3Provider');
this.pluginTypes = _.uniq(this.pluginTypes);
};
2017-03-31 11:34:43 +00:00
Plugin.prototype.registerContractsGeneration = function(cb) {
this.contractsGenerators.push(cb);
this.pluginTypes.push('contractGeneration');
this.pluginTypes = _.uniq(this.pluginTypes);
};
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
this.pipeline.push({matcthingFiles: matcthingFiles, cb: cb});
2016-12-10 15:20:04 +00:00
this.pluginTypes.push('pipeline');
this.pluginTypes = _.uniq(this.pluginTypes);
2016-12-10 15:20:04 +00:00
};
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});
2017-01-26 11:34:00 +00:00
this.pluginTypes.push('pipelineFiles');
this.pluginTypes = _.uniq(this.pluginTypes);
2017-01-26 11:34:00 +00:00
};
2017-03-31 11:34:43 +00:00
Plugin.prototype.addContractFile = function(file) {
2017-01-26 11:34:00 +00:00
this.contractsFiles.push(file);
this.pluginTypes.push('contractFiles');
this.pluginTypes = _.uniq(this.pluginTypes);
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);
this.pluginTypes.push('console');
this.pluginTypes = _.uniq(this.pluginTypes);
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) {
this.serviceChecks.push({checkName: checkName, checkFn: checkFn, time: time});
this.pluginTypes.push('serviceChecks');
this.pluginTypes = _.uniq(this.pluginTypes);
};
2017-03-31 11:34:43 +00:00
Plugin.prototype.has = function(pluginType) {
return this.pluginTypes.indexOf(pluginType) >= 0;
};
2017-03-31 11:34:43 +00:00
Plugin.prototype.generateProvider = function(args) {
return this.clientWeb3Providers.map(function(cb) {
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) {
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);
this.pluginTypes.push('contractsConfig');
this.pluginTypes = _.uniq(this.pluginTypes);
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});
this.pluginTypes.push('compilers');
this.pluginTypes = _.uniq(this.pluginTypes);
2017-01-29 02:31:09 +00:00
};
2017-12-27 00:55:42 +00:00
Plugin.prototype.registerUploadCommand = function(cmd, cb) {
this.uploadCmds.push({cmd: cmd, cb: cb});
this.pluginTypes.push('uploadCmds');
this.pluginTypes = _.uniq(this.pluginTypes);
2017-12-27 00:55:42 +00:00
};
2017-12-28 17:16:50 +00:00
Plugin.prototype.addCodeToEmbarkJS = function(code) {
this.embarkjs_code.push(code);
this.pluginTypes.push('embarkjsCode');
this.pluginTypes = _.uniq(this.pluginTypes);
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]);
this.pluginTypes.push('initCode');
this.pluginTypes = _.uniq(this.pluginTypes);
2017-12-28 22:42:25 +00:00
};
2018-01-10 16:15:32 +00:00
Plugin.prototype.registerImportFile = function(importName, importLocation) {
this.imports.push([importName, importLocation]);
this.pluginTypes.push('imports');
this.pluginTypes = _.uniq(this.pluginTypes);
2018-01-10 16:15:32 +00:00
};
Plugin.prototype.registerActionForEvent = function(eventName, cb) {
if (!this.eventActions[eventName]) {
this.eventActions[eventName] = [];
}
this.eventActions[eventName].push(cb);
this.pluginTypes.push('eventActions');
this.pluginTypes = _.uniq(this.pluginTypes);
}
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);
if (shouldRunPipeline) {
return pipeline.cb.call(this, args);
} else {
return args.source;
}
2016-12-10 15:20:04 +00:00
};
module.exports = Plugin;