summaryrefslogtreecommitdiffstats
path: root/lib/generate/plugin.js
diff options
context:
space:
mode:
authorSamy Pessé <samypesse@gmail.com>2014-04-19 17:28:44 +0200
committerSamy Pessé <samypesse@gmail.com>2014-04-19 17:28:44 +0200
commit8be8b825eebc95926e7d7c2596f8b789dba0f799 (patch)
tree7077d1363a95a9e8eb3b00f5a87420a2ce9dcab2 /lib/generate/plugin.js
parent56eb4a2897ae78be35db33172376f9901e23546e (diff)
downloadgitbook-8be8b825eebc95926e7d7c2596f8b789dba0f799.zip
gitbook-8be8b825eebc95926e7d7c2596f8b789dba0f799.tar.gz
gitbook-8be8b825eebc95926e7d7c2596f8b789dba0f799.tar.bz2
Add base loading of default addons
Add mixpanel as default addon
Diffstat (limited to 'lib/generate/plugin.js')
-rw-r--r--lib/generate/plugin.js70
1 files changed, 69 insertions, 1 deletions
diff --git a/lib/generate/plugin.js b/lib/generate/plugin.js
index bc131c6..a39fc65 100644
--- a/lib/generate/plugin.js
+++ b/lib/generate/plugin.js
@@ -1,3 +1,5 @@
+var _ = require("lodash");
+var Q = require("q");
var semver = require("semver");
var fs = require("./fs");
@@ -6,14 +8,38 @@ var pkg = require("../../package.json");
var Plugin = function(name) {
this.name = name;
this.packageInfos = {};
+ this.infos = {};
+ _.each([
+ name,
+ "gitbook-plugin-"+name,
+ "gitbook-theme-"+name
+ ], function(_name) {
+ if (this.load(_name)) return false;
+ }.bind(this));
+};
+
+// Load from a name
+Plugin.prototype.load = function(name) {
try {
this.packageInfos = require(name+"/package.json");
+ this.infos = require(name);
+ this.name = name;
+
+ return true;
} catch (e) {
- this.packageInfos = {};
+ return false;
}
};
+// Return resources
+Plugin.prototype.getResources = function(resource) {
+ if (!this.infos.book || !this.infos.book[resource]) {
+ return [];
+ }
+ return this.infos.book[resource];
+};
+
// Test if it's a valid plugin
Plugin.prototype.isValid = function() {
return (
@@ -25,4 +51,46 @@ Plugin.prototype.isValid = function() {
);
};
+// Extract data from a list of plugin
+Plugin.fromList = function(names) {
+ var failed = [];
+
+ // Load plugins
+ var plugins = _.map(names, function(name) {
+ var plugin = new Plugin(name);
+ if (!plugin.isValid()) failed.push(name);
+ return plugin;
+ });
+
+ if (_.size(failed) > 0) return Q.reject(new Error("Error loading plugins: "+failed.join(":")));
+
+ // Get all resources
+ var resources = _.chain([
+ "js", "css"
+ ])
+ .map(function(resource) {
+ return [
+ resource,
+ _.chain(plugins)
+ .map(function(plugin) {
+ return plugin.getResources(resource);
+ })
+ .flatten()
+ .value()
+ ];
+ })
+ .object()
+ .value();
+
+ return Q({
+ 'plugins': plugins,
+ 'resources': resources
+ });
+};
+
+// Default plugins
+Plugin.defaults = [
+ "mixpanel"
+];
+
module.exports = Plugin; \ No newline at end of file