Merge pull request #169 from resin-io/jviotti/refactor/plugins

Upgrade Nplugm to v3.0.0
This commit is contained in:
Juan Cruz Viotti 2015-08-19 12:49:32 -04:00
commit fcc44949a7
8 changed files with 51 additions and 190 deletions

View File

@ -1,102 +0,0 @@
(function() {
var _, async, commandOptions, form, plugins, visuals;
_ = require('lodash');
visuals = require('resin-cli-visuals');
commandOptions = require('./command-options');
plugins = require('../plugins');
form = require('resin-cli-form');
async = require('async');
exports.list = {
signature: 'plugins',
description: 'list all plugins',
help: 'Use this command to list all the installed resin plugins.\n\nExamples:\n\n $ resin plugins',
permission: 'user',
action: function(params, options, done) {
return plugins.list(function(error, resinPlugins) {
if (error != null) {
return done(error);
}
if (_.isEmpty(resinPlugins)) {
console.log('You don\'t have any plugins yet');
return done();
}
console.log(visuals.table.horizontal(resinPlugins, ['name', 'version', 'description', 'license']));
return done();
});
}
};
exports.install = {
signature: 'plugin install <name>',
description: 'install a plugin',
help: 'Use this command to install a resin plugin\n\nUse `--quiet` to prevent information logging.\n\nExamples:\n\n $ resin plugin install hello',
permission: 'user',
action: function(params, options, done) {
return plugins.install(params.name, function(error) {
if (error != null) {
return done(error);
}
console.info("Plugin installed: " + params.name);
return done();
});
}
};
exports.update = {
signature: 'plugin update <name>',
description: 'update a plugin',
help: 'Use this command to update a resin plugin\n\nUse `--quiet` to prevent information logging.\n\nExamples:\n\n $ resin plugin update hello',
permission: 'user',
action: function(params, options, done) {
return plugins.update(params.name, function(error, version) {
if (error != null) {
return done(error);
}
console.info("Plugin updated: " + params.name + "@" + version);
return done();
});
}
};
exports.remove = {
signature: 'plugin rm <name>',
description: 'remove a plugin',
help: 'Use this command to remove a resin.io plugin.\n\nNotice this command asks for confirmation interactively.\nYou can avoid this by passing the `--yes` boolean option.\n\nExamples:\n\n $ resin plugin rm hello\n $ resin plugin rm hello --yes',
options: [commandOptions.yes],
permission: 'user',
action: function(params, options, done) {
return async.waterfall([
function(callback) {
if (options.yes) {
return callback(null, true);
} else {
return form.ask({
message: 'Are you sure you want to delete the plugin?',
type: 'confirm',
"default": false
}).nodeify(callback);
}
}, function(confirmed, callback) {
if (!confirmed) {
return callback();
}
return plugins.remove(params.name, callback);
}, function(error) {
if (error != null) {
return done(error);
}
console.info("Plugin removed: " + params.name);
return done();
}
]);
}
};
}).call(this);

View File

@ -1,11 +1,11 @@
(function() { (function() {
var _, actions, async, capitano, errors, plugins, resin, update; var Promise, _, actions, capitano, errors, plugins, resin, update;
_ = require('lodash'); _ = require('lodash');
async = require('async'); Promise = require('bluebird');
capitano = require('capitano'); capitano = Promise.promisifyAll(require('capitano'));
resin = require('resin-sdk'); resin = require('resin-sdk');
@ -13,7 +13,7 @@
errors = require('./errors'); errors = require('./errors');
plugins = require('./plugins'); plugins = require('./utils/plugins');
update = require('./utils/update'); update = require('./utils/update');
@ -96,14 +96,10 @@
update.notify(); update.notify();
async.waterfall([ plugins.register(/^resin-plugin-(.+)$/).then(function() {
function(callback) { var cli;
return plugins.register('resin-plugin-', callback); cli = capitano.parse(process.argv);
}, function(callback) { return capitano.executeAsync(cli);
var cli; })["catch"](errors.handle);
cli = capitano.parse(process.argv);
return capitano.execute(cli, callback);
}
], errors.handle);
}).call(this); }).call(this);

View File

@ -1,39 +0,0 @@
(function() {
var Nplugm, _, capitano, nplugm, registerPlugin;
Nplugm = require('nplugm');
_ = require('lodash');
capitano = require('capitano');
nplugm = null;
registerPlugin = function(plugin) {
if (!_.isArray(plugin)) {
return capitano.command(plugin);
}
return _.each(plugin, capitano.command);
};
exports.register = function(prefix, callback) {
nplugm = new Nplugm(prefix);
return nplugm.list(function(error, plugins) {
var i, len, plugin;
if (error != null) {
return callback(error);
}
for (i = 0, len = plugins.length; i < len; i++) {
plugin = plugins[i];
try {
registerPlugin(nplugm.require(plugin));
} catch (_error) {
error = _error;
console.error(error.message);
}
}
return callback();
});
};
}).call(this);

23
build/utils/plugins.js Normal file
View File

@ -0,0 +1,23 @@
(function() {
var _, capitano, nplugm;
nplugm = require('nplugm');
_ = require('lodash');
capitano = require('capitano');
exports.register = function(regex) {
return nplugm.list(regex).map(function(plugin) {
var command;
command = require(plugin);
if (!_.isArray(command)) {
return capitano.command(command);
}
return _.each(command, capitano.command);
})["catch"](function(error) {
return console.error(error.message);
});
};
}).call(this);

View File

@ -1,10 +1,10 @@
_ = require('lodash') _ = require('lodash')
async = require('async') Promise = require('bluebird')
capitano = require('capitano') capitano = Promise.promisifyAll(require('capitano'))
resin = require('resin-sdk') resin = require('resin-sdk')
actions = require('./actions') actions = require('./actions')
errors = require('./errors') errors = require('./errors')
plugins = require('./plugins') plugins = require('./utils/plugins')
update = require('./utils/update') update = require('./utils/update')
capitano.permission 'user', (done) -> capitano.permission 'user', (done) ->
@ -70,13 +70,7 @@ capitano.command(actions.logs)
update.notify() update.notify()
async.waterfall([ plugins.register(/^resin-plugin-(.+)$/).then ->
cli = capitano.parse(process.argv)
(callback) -> capitano.executeAsync(cli)
plugins.register('resin-plugin-', callback) .catch(errors.handle)
(callback) ->
cli = capitano.parse(process.argv)
capitano.execute(cli, callback)
], errors.handle)

View File

@ -1,22 +0,0 @@
Nplugm = require('nplugm')
_ = require('lodash')
capitano = require('capitano')
nplugm = null
registerPlugin = (plugin) ->
return capitano.command(plugin) if not _.isArray(plugin)
return _.each(plugin, capitano.command)
exports.register = (prefix, callback) ->
nplugm = new Nplugm(prefix)
nplugm.list (error, plugins) ->
return callback(error) if error?
for plugin in plugins
try
registerPlugin(nplugm.require(plugin))
catch error
console.error(error.message)
return callback()

11
lib/utils/plugins.coffee Normal file
View File

@ -0,0 +1,11 @@
nplugm = require('nplugm')
_ = require('lodash')
capitano = require('capitano')
exports.register = (regex) ->
nplugm.list(regex).map (plugin) ->
command = require(plugin)
return capitano.command(command) if not _.isArray(command)
return _.each(command, capitano.command)
.catch (error) ->
console.error(error.message)

View File

@ -46,7 +46,7 @@
"html-to-text": "^1.3.1", "html-to-text": "^1.3.1",
"lodash": "^3.10.0", "lodash": "^3.10.0",
"mkdirp": "~0.5.0", "mkdirp": "~0.5.0",
"nplugm": "^2.2.0", "nplugm": "^3.0.0",
"npm": "^2.13.0", "npm": "^2.13.0",
"open": "0.0.5", "open": "0.0.5",
"resin-cli-form": "^1.1.0", "resin-cli-form": "^1.1.0",