Skip to content

Commit

Permalink
Check for Jake/gen commands sooner, allow them to parse options
Browse files Browse the repository at this point in the history
  • Loading branch information
mde committed Apr 22, 2013
1 parent 4c0bef8 commit 517e18a
Show file tree
Hide file tree
Showing 3 changed files with 200 additions and 172 deletions.
224 changes: 105 additions & 119 deletions bin/cli.js
Original file line number Diff line number Diff line change
@@ -1,136 +1,122 @@
#!/usr/bin/env node

// Dependencies
var geddy = require('../lib/geddy')
, fs = require('fs')
, path = require('path')
, utils = require('utilities')
, parseopts = require('../lib/parseopts')
, cmd = require('../lib/cmd');

// Variables
var cwd = process.cwd()
, args = process.argv.slice(2)
, parser
, optsMap
, cmds
, opts
, usage
, start;
var args = process.argv.slice(2);
args = cmd.parseArgs(args);

// Options available
optsMap = [
{ full: 'origins'
, abbr: 'o'
, args: true
, canon: 'origins'
}
, { full: ['hostname', 'bind']
, abbr: 'b'
, args: true
, canon: 'hostname'
}
, { full: 'port'
, abbr: 'p'
, args: true
, canon: 'port'
}
, { full: 'workers'
, abbr: ['n', 'w']
, args: true
, canon: 'workers'
}
, { full: 'version'
, abbr: ['v', 'V']
, args: false
, canon: 'version'
}
, { full: 'help'
, abbr: 'h'
, args: false
, canon: 'help'
}
, { full: 'debug'
, abbr: 'd'
, args: true
, canon: 'debug'
}
, { full: 'loglevel'
, abbr: 'l'
, args: true
, canon: 'loglevel'
}
, { full: 'environment'
, abbr: 'e'
, args: true
, canon: 'environment'
}
, { full: 'geddy-root'
, abbr: 'g'
, args: true
, canon: 'geddyRoot'
}
, { full: 'spawned'
, abbr: ['s', 'q', 'Q']
, args: true
, canon: 'spawned'
}
, { full: 'jade'
, abbr: 'j'
, args: false
, canon: 'jade'
}
, { full: ['handle', 'handlebars']
, abbr: 'H'
, args: false
, canon: 'handlebars'
}
, { full: 'mustache'
, abbr: 'm'
, args: false
, canon: 'mustache'
}
, { full: 'realtime'
, abbr: 'rt'
, args: false
, canon: 'realtime'
}
];
// Jake commands -- hand off to Jake to load up env
// and run whatever command. Jake parses all the CLI
// args itself
if (args[0] == 'jake') {
args.shift();
var c = new cmd.JakeCmd(args);
c.run();
}
// Generator commands -- the Cmd object will parse
// the args into commands and opts
else if (args[0] == 'gen') {
args.shift();
var c = new cmd.Cmd(args);
c.run();
}
// Run the server
else {
(function () {
var parser
, optsMap
, cmds
, opts
, usage
, die;

// Parse optsMap and generate options and cmd commands
parser = new parseopts.Parser(optsMap);
parser.parse(args);
cmds = parser.cmds;
opts = parser.opts;
// Server options
optsMap = [
{ full: 'origins'
, abbr: 'o'
, args: true
, canon: 'origins'
}
, { full: ['hostname', 'bind']
, abbr: 'b'
, args: true
, canon: 'hostname'
}
, { full: 'port'
, abbr: 'p'
, args: true
, canon: 'port'
}
, { full: 'workers'
, abbr: ['n', 'w']
, args: true
, canon: 'workers'
}
, { full: 'version'
, abbr: ['v', 'V']
, args: false
, canon: 'version'
}
, { full: 'help'
, abbr: 'h'
, args: false
, canon: 'help'
}
, { full: 'debug'
, abbr: 'd'
, args: true
, canon: 'debug'
}
, { full: 'loglevel'
, abbr: 'l'
, args: true
, canon: 'loglevel'
}
, { full: 'environment'
, abbr: 'e'
, args: true
, canon: 'environment'
}
, { full: 'geddy-root'
, abbr: 'g'
, args: true
, canon: 'geddyRoot'
}
, { full: 'spawned'
, abbr: ['s', 'q', 'Q']
, args: true
, canon: 'spawned'
}
];

// Set handlebars option to handle option
opts.handle = opts.handlebars || opts.handle;
// Parse optsMap and generate options and cmd commands
parser = new parseopts.Parser(optsMap)
parser.parse(args);
cmds = parser.cmds;
opts = parser.opts;

// Exit the process with a message
die = function (str) {
console.log(str);
process.exit();
};
// Exit the process with a message
die = function (str) {
console.log(str);
process.exit();
};

// Start Geddy with options
start = function () {
geddy.startCluster(opts);
};
if (opts.help) {
var usage = fs.readFileSync(path.join(__dirname, '..',
'usage.txt')).toString();
return die(usage);
}

if (opts.help) {
var usage = fs.readFileSync(path.join(__dirname, '..',
'usage.txt')).toString();
die(usage);
}
if (opts.version) {
die(geddy.version);
}
if (opts.version) {
return die(geddy.version);
}

// `geddy app foo` or `geddy resource bar` etc. -- run generators
if (cmds.length) {
cmd.run(cmds, opts);
}
// Just `geddy` -- start the server
else {
start();
geddy.startCluster(opts);
})();
}

Loading

0 comments on commit 517e18a

Please sign in to comment.