Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

[lib] carapace.load, load arbitrary plugins

  • Loading branch information...
commit 44060b0105966feed87665d997e45f3112ab70d2 1 parent f5c3113
@jamesonjlee jamesonjlee authored
Showing with 35 additions and 4 deletions.
  1. +1 −1  bin/carapace
  2. +34 −3 lib/carapace.js
View
2  bin/carapace
@@ -41,7 +41,7 @@ carapace.start(function () {
carapace.on('carapace::plugin::error', onPluginError);
var plugins = argv.plugin.map(function (plugin) {
- return carapace.plugins[plugin];
+ return carapace.load(plugin);
});
carapace.use(plugins, function () {
View
37 lib/carapace.js
@@ -35,9 +35,6 @@ require('./net');
// can be later required by `carapace.plugin()` or `carapace:plugin` events.
//
carapace.plugins = {};
-fs.readdirSync(path.join(__dirname, '../lib/plugins')).forEach(function (name) {
- carapace.plugins[name.replace(/\.js$/, '')] = path.join(__dirname, '../lib/plugins', name);
-});
//
// Internal state for managing various carapace operations:
@@ -209,3 +206,37 @@ carapace.run = function (script, argv, override, callback) {
return carapace;
};
+
+//
+// ### function load (plugin, callback)
+// #### @script {string} Path to the script to run inside the carapace;
+// #### @force {boolean} forces loading of the new script into repo
+//
+carapace.load = function load(script, force) {
+
+ var name = script.replace(/\.js$/, '');
+ name = name.substr(name.lastIndexOf('/')+1);
+
+ if (carapace.plugins[name] && !force) {
+ // if we already have the plugin,
+ return carapace.plugins[name];
+ }
+
+ if (!~['.', '/'].indexOf(script[0])) {
+ //
+ // If it is not a relative or absolute path, make it absolute
+ // from the current `process.cwd()`.
+ //
+ script = path.join(process.cwd(), script);
+ }
+
+ carapace.plugins[name] = script;
+
+ carapace.emit('carapace::plugin::loaded');
+ return script;
+};
+
+fs.readdirSync(path.join(__dirname, '../lib/plugins')).forEach(function (name) {
+ carapace.load(path.join(__dirname, '../lib/plugins', name), false);
+});
+
Please sign in to comment.
Something went wrong with that request. Please try again.