|
| 1 | +"use strict"; |
| 2 | + |
| 3 | +var plugin_manager = function (config, configPath) { |
| 4 | + var path = require('path'), |
| 5 | + fs = require('fs-extra'), |
| 6 | + util = require('./utilities'), |
| 7 | + paths = config.paths; |
| 8 | + |
| 9 | + function loadPlugin(pluginName) { |
| 10 | + var pluginInstance = require(path.join(process.cwd(), 'node_modules', pluginName)); |
| 11 | + return pluginInstance; |
| 12 | + } |
| 13 | + |
| 14 | + function installPlugin(pluginName) { |
| 15 | + try { |
| 16 | + var pluginPath = path.resolve( |
| 17 | + path.join(process.cwd(), 'node_modules', pluginName) |
| 18 | + ); |
| 19 | + console.log('Attempting to load plugin from', pluginPath); |
| 20 | + try { |
| 21 | + var pluginDirStats = fs.statSync(pluginPath); |
| 22 | + } catch (ex) { |
| 23 | + util.logRed(pluginName + ' not found, please use npm to install it first.'); |
| 24 | + util.logRed(pluginName + ' not loaded.'); |
| 25 | + return; |
| 26 | + } |
| 27 | + var pluginPathDirExists = pluginDirStats.isDirectory(); |
| 28 | + if (pluginPathDirExists) { |
| 29 | + |
| 30 | + //write config entry back |
| 31 | + var diskConfig = fs.readJSONSync(path.resolve(configPath), 'utf8'); |
| 32 | + diskConfig[pluginName] = true; |
| 33 | + fs.outputFileSync(path.resolve(configPath), JSON.stringify(diskConfig, null, 2)); |
| 34 | + } |
| 35 | + } catch (ex) { |
| 36 | + console.log(ex); |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + function detectPlugins() { |
| 41 | + var node_modules_path = path.join(process.cwd(), 'node_modules'); |
| 42 | + var npm_modules = fs.readdirSync(node_modules_path).filter(function (dir) { |
| 43 | + var module_path = path.join(process.cwd(), 'node_modules', dir); |
| 44 | + return fs.statSync(module_path).isDirectory() && dir.indexOf('plugin-node-') === 0; |
| 45 | + }); |
| 46 | + return npm_modules; |
| 47 | + } |
| 48 | + |
| 49 | + function disablePlugin(pluginName) { |
| 50 | + console.log('not implemented yet'); |
| 51 | + } |
| 52 | + |
| 53 | + function enablePlugin(pluginName) { |
| 54 | + console.log('not implemented yet'); |
| 55 | + } |
| 56 | + |
| 57 | + return { |
| 58 | + install_plugin: function(pluginName) { |
| 59 | + installPlugin(pluginName); |
| 60 | + }, |
| 61 | + load_plugin: function (pluginName) { |
| 62 | + loadPlugin(pluginName); |
| 63 | + }, |
| 64 | + detect_plugins: function () { |
| 65 | + return detectPlugins(); |
| 66 | + }, |
| 67 | + disable_plugin: function (pluginName) { |
| 68 | + disablePlugin(pluginName); |
| 69 | + }, |
| 70 | + enable_plugin: function (pluginName) { |
| 71 | + enablePlugin(pluginName); |
| 72 | + } |
| 73 | + }; |
| 74 | + |
| 75 | +}; |
| 76 | + |
| 77 | +module.exports = plugin_manager; |
0 commit comments