Created
November 14, 2011 15:59
-
-
Save mklabs/1364264 to your computer and use it in GitHub Desktop.
playing with broadway + sugarskull cli mode
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// app.js - core application object extending broadway.App, overriding some of these methods. | |
var fs = require('fs'), | |
path = require('path'), | |
util = require('util'), | |
broadway = require('broadway'); | |
var App = exports.App = function (options) { | |
broadway.App.call(this, options); | |
}; | |
util.inherits(App, broadway.App); | |
App.prototype.init = function (callback) { | |
// console.log('init', arguments); | |
broadway.App.prototype.init.call(this, callback); | |
}; | |
App.prototype.use = function (plugin, options) { | |
broadway.App.prototype.use.apply(this, arguments); | |
}; | |
App.prototype.inspect = function (options, callback) { | |
options = options || {}; | |
var self = this, | |
plugins = Object.keys(this.plugins), | |
output = ['Plugins:'].concat(plugins.map(function(plugin) { | |
var configs = Object.keys(self.options[plugin]); | |
return ' » ' + plugin + (configs.length ? ' - options key: ' + Object.keys(self.options[plugin]).join(' ') : ''); | |
})); | |
console.log(output.join('\n')); | |
if(options.plugins && options.plugins.length) { | |
options.plugins.forEach(function(plugin) { | |
if(!self.options[plugin]) return console.warn('Unknown plugin ', plugin); | |
console.log(plugin, 'options:'); | |
console.log(util.inspect(self.options[plugin], false, 2, true)); | |
}); | |
} | |
broadway.App.prototype.inspect.apply(this, arguments); | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"author": "", | |
"name": "broadway-sugar-cli", | |
"description": "playing with broadway + sugarskull in cli mode", | |
"version": "0.0.0", | |
"repository": { | |
"url": "" | |
}, | |
"engines": { | |
"node": "~0.4.12" | |
}, | |
"dependencies": { | |
"broadway": "~0.1.0", | |
"sugarskull": "~1.0.1", | |
"optimist": "~0.2.8", | |
"prompt": "~0.1.10" | |
}, | |
"devDependencies": {} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* index.js: Top-level plugin exposing CLI features in flatiron | |
* | |
* (C) 2011, Nodejitsu Inc. | |
* MIT LICENSE | |
* | |
* | |
* Coming from https://github.com/flatiron/flatiron/blob/master/lib/flatiron/plugins/cli.js, | |
* sligtly edited here to fit custom needs. | |
* | |
*/ | |
var sugarskull = require('sugarskull'), | |
optimist = require('optimist'), | |
path = require('path'), | |
fs = require('fs'); | |
// | |
// ### Name this plugin | |
// | |
exports.name = 'cli'; | |
// | |
// ### Add a short description | |
// | |
exports.description = 'Top-level plugin exposing CLI features in flatiron'; | |
// | |
// ### function attach (options, done) | |
// #### @options {Object} Options for this plugin | |
// Initializes `this` (the application) with the core `cli` plugins consisting of: | |
// `argv`, `commands`, and `prompt` in that order. | |
// | |
exports.attach = function (options) { | |
var self = this; | |
options = options || {}; | |
// | |
// Setup `this.argv` to use `optimist`. | |
// | |
exports.argv.call(this, options); | |
// | |
// Setup `this.commands`. | |
// | |
exports.commands.call(this, options); | |
// | |
// Setup `this.prompt`. | |
// | |
exports.prompt.call(this, options.prompt); | |
// | |
// Setup `self.router` and associated core routing method. | |
// | |
var cli = self.cli = {}; | |
cli.router = new sugarskull.cli.Router().configure({ | |
async: self.async || options.async | |
}); | |
this.cmd = this.command = function (path, handler) { | |
self.cli.router.on(path, handler); | |
}; | |
if(options.dir) { | |
this.commands = {}; | |
fs.readdirSync(options.dir).forEach(function(command) { | |
var mod = path.join(options.dir, command); | |
if(!fs.statSync(mod).isFile()) return; | |
command = command.replace(path.extname(command), ''); | |
self.commands.__defineGetter__(command, function() { | |
return require(path.join(options.dir, command)); | |
}); | |
}); | |
} | |
self.start = function(callback) { | |
self.cli.router.dispatch('on', process.argv.slice(2).join(' '), self.log, callback); | |
}; | |
}; | |
exports.init = function(callback) { | |
var self = this, | |
original = process.argv.slice(2), | |
router = this.cli.router, | |
commands = this.commands, | |
usage = [this.usage || 'Usage: ']; | |
// commands? | |
if(commands) { | |
Object.keys(commands).forEach(function(cmd) { | |
var command = commands[cmd]; | |
usage.push(' ' + pad(command.description[0], 30) + command.description[1]); | |
console.log(cmd); | |
var handler = function handler() { | |
this.params.argv = { | |
remain: this.params._, | |
cooked: original, | |
original: original | |
}; | |
command.call(this, this.params, function(err) { | |
if(!err) return; | |
console.error(err.message || err.stack); | |
}); | |
}; | |
router.on(cmd, handler); | |
router.on(new RegExp(cmd + '.+'), handler); | |
}); | |
} | |
if(!router.routes.help) router.on('help', console.log.bind(console, usage.join('\n'))); | |
router.dispatch('on', original.join(' '), this.log, callback); | |
}; | |
exports.argv = function (options) { | |
if (options.argv && Object.keys(options.argv).length) { | |
this.argv = require('optimist').options(options).argv; | |
} | |
else { | |
this.argv = require('optimist').argv; | |
} | |
}; | |
exports.commands = function (options) { | |
this.commands = {}; | |
}; | |
exports.prompt = function (options) { | |
options = options || {}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var fs = require('fs'), | |
path = require('path'), | |
regp = /^plugin-/; | |
var plugins = module.exports = {}; | |
fs.readdirSync(__dirname).forEach(function(plugin) { | |
var ext = path.extname(plugin); | |
if(ext !== '.js' || !regp.test(plugin)) return; | |
plugin = plugin.replace(ext, ''); | |
plugins.__defineGetter__(plugin.replace(regp, ''), function() { | |
return require(path.join(__dirname, plugin)); | |
}); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// test and play with broadway, sugarskull (now Director) for cli use. | |
// | |
var plugins = require('./plugins'), | |
App = require('./app').App; | |
var app = new App(); | |
app.use(plugins.cli, { | |
// dir: require('path').join(__dirname, 'commands'), | |
dir: require('path').join(process.env.HOME, 'mklabs/gimme-assets/lib/commands') | |
}); | |
// dir setup the commands object, as lazy-loaded required module. | |
// console.log(app.commands); | |
app.cmd('inspect', function() { | |
app.inspect(); | |
}); | |
app.cmd('inspect .+', function() { | |
var remains = this.params._.slice(1); | |
app.inspect(remains.length ? { plugins: remains } : {}); | |
}); | |
app.cli.router.on('foo bar', function() { | |
console.log('You asked a foobar:'); | |
app.prompt.get('stuff', function(err) { | |
if(err) throw err; | |
app.log.info('got stuff', arguments); | |
}); | |
}); | |
app.cli.router.on('bar', function() { | |
console.log('bar'); | |
console.log(arguments); | |
}); | |
app.init(function(err) { | |
if(err) throw err; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment