├── .npmignore ├── test └── indexTests.js ├── .gitignore ├── .travis.yml ├── package.json ├── LICENSE ├── README.md └── index.js /.npmignore: -------------------------------------------------------------------------------- 1 | *.tgz -------------------------------------------------------------------------------- /test/indexTests.js: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | 3 | *.tgz 4 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - "iojs" 4 | script: 5 | - npm test -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "electron-plugins", 3 | "version": "0.1.0", 4 | "description": "Plugin loader for electron applications.", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "mocha" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "https://github.com/evolvelabs/electron-plugins.git" 12 | }, 13 | "keywords": [ 14 | "electron", 15 | "plugins" 16 | ], 17 | "author": "Evolve LLC", 18 | "license": "MIT", 19 | "bugs": { 20 | "url": "https://github.com/evolvelabs/electron-plugins/issues" 21 | }, 22 | "homepage": "https://github.com/evolvelabs/electron-plugins", 23 | "dependencies": { 24 | "appdirectory": "^0.1.0", 25 | "async": "^0.9.0" 26 | }, 27 | "devDependencies": { 28 | "chai": "^2.3.0", 29 | "mocha": "^2.2.4", 30 | "proxyquire": "^1.4.0", 31 | "sinon": "^1.14.1" 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # electron-plugins 2 | Plugin loader for electron applications. 3 | 4 | ## Installation 5 | ``` 6 | npm install electron-plugins --save 7 | ``` 8 | 9 | ## Usage 10 | In your electron render process you can load your plugins like so: 11 | ``` 12 | var plugins = require('electron-plugins'); 13 | 14 | document.addEventListener('DOMContentLoaded', function () { 15 | var context = { document: document }; 16 | plugins.load(context, function (err, loaded) { 17 | if(err) return console.error(err); 18 | console.log('Plugins loaded successfully.'); 19 | }); 20 | }); 21 | ``` 22 | 23 | Your plugin should export a constructor function, which is passed the context object upon instantiation. You can put whatever you want onto the context object. 24 | ``` 25 | function Plugin(context) { 26 | var d = context.document 27 | var ul = d.getElementById('plugins') 28 | var li = d.createElement('li') 29 | li.innerHTML = 'electron-updater-sample-plugin' 30 | ul.appendChild(li) 31 | } 32 | 33 | module.exports = Plugin 34 | ``` 35 | 36 | ## Examples 37 | * [electron-updater-sample](https://github.com/EvolveLabs/electron-updater-sample) 38 | * [electron-updater-sample-plugin](https://github.com/EvolveLabs/electron-updater-sample-plugin) 39 | 40 | ## Related 41 | * [electron-updater](https://github.com/EvolveLabs/electron-updater) 42 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var path = require('path'), 2 | fs = require('fs'), 3 | async = require('async'), 4 | AppDirectory = require('appdirectory') 5 | 6 | function getPlugins(plugins) { 7 | var mapped = [] 8 | Object.getOwnPropertyNames(plugins).forEach(function (name) { 9 | mapped.push({ 10 | name: name, 11 | version: plugins[name] 12 | }) 13 | }) 14 | 15 | return mapped 16 | } 17 | 18 | function getPluginPackage(plugin, callback) { 19 | var context = this 20 | var name = plugin.name 21 | var version = plugin.version 22 | // Load the package.json in either the linked dev directory or from the downloaded plugin 23 | async.map( 24 | ['link', version], 25 | function (version, callback) { 26 | var packagePath = path.join(context.pluginsDir, name, version, 'package.json') 27 | fs.readFile(packagePath, {encoding:'utf8'}, function (err, result) { 28 | if(err) return callback() 29 | callback(null, { 30 | name: name, 31 | version: version, 32 | config: JSON.parse(result) 33 | }) 34 | }) 35 | }, 36 | function (err, results) { 37 | // If neither file is found, or there was an unexpected error then fail 38 | var result = results[0] || results[1] 39 | if (err || !result) return callback(err || 'ENOENT') 40 | callback(null, result) 41 | }) 42 | } 43 | 44 | function loadPlugin(context, results, callback) { 45 | var modules = [] 46 | var dependencies = [] 47 | try { 48 | for(var i = 0, n = results.length; i < n; i++) { 49 | var plugin = results[i] 50 | var main = plugin.config.main 51 | var name = plugin.name 52 | var version = plugin.version 53 | var depName = name.replace(/-/g, '.') 54 | var file = path.resolve(path.join(context.pluginsDir, name, version), main) 55 | var Plugin = require(file) 56 | var mod = new Plugin(context.appContext) 57 | modules.push(mod) 58 | dependencies.push(depName) 59 | } 60 | } catch (err) { 61 | return callback(err) 62 | } 63 | 64 | callback(null, dependencies, modules) 65 | } 66 | 67 | function load(appContext, callback) { 68 | var appDir = path.dirname(process.mainModule.filename) 69 | var packagePath = path.join(appDir, 'package.json') 70 | fs.readFile(packagePath, {encoding: 'utf8'}, function (err, contents) { 71 | if(err) return callback(err); 72 | var config = JSON.parse(contents) 73 | var dirs = new AppDirectory({ 74 | appName: config.name, 75 | appAuthor: config.publisher 76 | }) 77 | var appData = dirs.userData() 78 | console.log('appData: ' + appData) 79 | var currentPath = path.join(appData, '.current') 80 | fs.readFile(currentPath, {encoding: 'utf8'}, function (err, contents) { 81 | var plugins = (!err ? JSON.parse(contents) : config.plugins) || {} 82 | var context = { 83 | plugins: plugins, 84 | pluginsDir: path.join(appData, 'plugins'), 85 | appContext: appContext 86 | } 87 | async.map( 88 | getPlugins(context.plugins), 89 | getPluginPackage.bind(context), 90 | function (err, results) { 91 | if(err) return callback(err) 92 | loadPlugin(context, results, callback) 93 | }) 94 | }) 95 | }) 96 | } 97 | 98 | module.exports = { 99 | load: load 100 | } --------------------------------------------------------------------------------