Permalink
Browse files

Created app and resource generators for Jade, Handlebars and Mustache

  • Loading branch information...
1 parent 0feb0b2 commit ae3f611a00b7ee7ba06ac097a6bd1b6bc06f42a8 @larzconwell larzconwell committed Jul 2, 2012
Showing with 521 additions and 238 deletions.
  1. +155 −130 bin/cli.js
  2. +3 −0 lib/utils/file.js
  3. +3 −0 lib/utils/index.js
  4. +132 −107 templates/Jakefile
  5. +51 −0 templates/base/views/ejs/layouts/application.html.ejs
  6. 0 templates/base/views/{ → ejs}/main/index.html.ejs
  7. +1 −1 templates/base/views/{layouts/application.html.ejs → handlebars/layouts/application.html.hbs}
  8. +6 −0 templates/base/views/handlebars/main/index.html.hbs
  9. +46 −0 templates/base/views/jade/layouts/application.html.jade
  10. +6 −0 templates/base/views/jade/main/index.html.jade
  11. 0 templates/views/{ → ejs}/add.html.ejs
  12. 0 templates/views/{ → ejs}/edit.html.ejs
  13. 0 templates/views/{ → ejs}/index.html.ejs
  14. +12 −0 templates/views/ejs/layout.html.ejs
  15. 0 templates/views/{ → ejs}/show.html.ejs
  16. +6 −0 templates/views/handlebars/add.html.hbs
  17. +6 −0 templates/views/handlebars/edit.html.hbs
  18. +6 −0 templates/views/handlebars/index.html.hbs
  19. +12 −0 templates/views/handlebars/layout.html.hbs
  20. +6 −0 templates/views/handlebars/show.html.hbs
  21. +5 −0 templates/views/jade/add.html.jade
  22. +5 −0 templates/views/jade/edit.html.jade
  23. +5 −0 templates/views/jade/index.html.jade
  24. +8 −0 templates/views/jade/layout.html.jade
  25. +5 −0 templates/views/jade/show.html.jade
  26. +6 −0 templates/views/mustache/add.html.ms
  27. +6 −0 templates/views/mustache/edit.html.ms
  28. +6 −0 templates/views/mustache/index.html.ms
  29. +12 −0 templates/views/mustache/layout.html.ms
  30. +6 −0 templates/views/mustache/show.html.hbs
  31. +6 −0 templates/views/mustache/show.html.ms
View
@@ -1,163 +1,188 @@
#!/usr/bin/env node
-// Load the basic Geddy toolkit
-var geddy = require('../lib/geddy');
-
-var exec = require('child_process').exec
+// Dependencies
+var geddy = require('../lib/geddy')
+ , exec = require('child_process').exec
, fs = require('fs')
, path = require('path')
, parseopts = require('../lib/parseopts')
- , utils = require('../lib/utils/index')
- , parser
- , cwd = process.cwd()
+ , utils = require('../lib/utils/index');
+
+// Variables
+var cwd = process.cwd()
, args = process.argv.slice(2)
+ , parser
, optsMap
, cmds
, opts
, usage
, cmd
- , filepath;
-
-usage = ''
- + 'Geddy web framework for Node.js\n'
- + '*********************************************************************************************\n'
- + 'With no flags, server starts running on port 4000, on http://localhost, in development mode\n'
- + '*********************************************************************************************\n'
- + '{Usage}: geddy [options]\n'
- + '\n'
- + '{Options}:\n'
- + ' -e, --environment Evironment config to use\n'
- + ' -p, --port NUM Port number, defaults to 4000\n'
- + ' -n, --workers NUM Number of worker processes to use, defaults to 2\n'
- + ' -V/v, --version Outputs the version of geddy that you have installed\n'
- + ' -d, --debug Sets the log level to output debug messages to the console\n'
- + ' -h, --help Outputs help information\n'
- + '';
+ , filepath
+ , die
+ , start;
+
+// Help dialog
+usage = [
+ 'Geddy web framework for Node.js'
+ , ''
+ , 'With no flags, Geddy starts on port 4000, on http://localhost in development mode'
+ , ''
+ , 'Usage: geddy [options] [arguments]'
+ , ''
+ , 'Options:'
+ , ' --environment, -e # Environment to use'
+ , ' --port, -p # Port to connect to'
+ , ' --workers, -w # Number of worker processes to use(default: 2)'
+ , ' --version, -v # Output the version of Geddy installed'
+ , ' --debug, -d # Sets the log level to output debug messages to the console'
+ , ' --help, -h # Output the help dialog'
+ , ' --jade, -j # Generate Jade templates for the app/resource/controller'
+ , ' commands(Default: EJS)'
+ , ' --handle, -H # Generate Handlebars templates for the'
+ , ' app/resource/controller commands(Default: EJS)'
+ , ' --mustache, -m # Generate Mustache templates for the'
+ , ' app/resource/controller commands(Default: EJS)'
+ , ''
+ , ' app # Create a new Geddy application'
+ , ' resource # Create a new resource'
+ , ' (Views, Model, Controller and resource route)'
+ , ' secret # Create new app secret in `environment.js`'
+ , ' controller # Generate a new controller(views and routes included)'
+ , ' model # Generate a new model'
+ , ''
+].join('\n');
+// Options available
optsMap = [
- { full: 'origins'
- , abbr: 'o'
- }
-, { full: 'port'
- , abbr: 'p'
- }
-, { full: 'workers'
- , abbr: 'n'
- }
-, { full: 'version'
- , abbr: 'V'
- }
-, { full: 'version'
- , abbr: 'v'
- }
-, { full: 'help'
- , abbr: 'h'
- }
-, { full: 'debug'
- , abbr: 'd'
- }
-, { full: 'logLevel'
- , abbr: 'l'
-}
-, { full: 'environment'
- , abbr: 'e'
- }
-, { full: 'spawned'
- , abbr: 'Q'
- }
+ { full: 'origins', abbr: 'o' }
+ , { full: 'port', abbr: 'p' }
+ , { full: 'workers', abbr: 'n' } // Compat
+ , { full: 'workers', abbr: 'w' }
+ , { full: 'version', abbr: 'v' }
+ , { full: 'version', abbr: 'V' } // Compat
+ , { full: 'help', abbr: 'h' }
+ , { full: 'debug', abbr: 'd' }
+ , { full: 'loglevel', abbr: 'l' }
+ , { full: 'environment', abbr: 'e' }
+ , { full: 'spawned', abbr: 'Q' } // Compat
+ , { full: 'spawned', abbr: 'q' } // Compat
+ , { full: 'spawned', abbr: 's' }
+ , { full: 'jade', abbr: 'j' }
+ , { full: 'handle', abbr: 'H' }
+ , { full: 'handlebars', abbr: 'H' }
+ , { full: 'mustache', abbr: 'm' }
];
+// Parse optsMap and generate options and cmd commands
parser = new parseopts.Parser(optsMap);
parser.parse(args);
cmds = parser.cmds;
opts = parser.opts;
-var die = function (str) {
+// Set handlebars option to handle option
+opts.handle = opts.handlebars || opts.handle;
+
+// Exit the process with a message
+die = function(str) {
console.log(str);
process.exit();
};
-var start = function () {
+// Start Geddy with options
+start = function() {
geddy.config(opts);
geddy.start();
};
-if (typeof opts.help != 'undefined') {
- die(usage);
-}
-else if (typeof opts.version != 'undefined') {
- die(geddy.version);
-}
-else {
- // `geddy app foo` or `geddy resource bar` etc. -- run generators
- if (cmds.length) {
- filepath = path.normalize(path.join(__dirname, '..', 'templates', 'Jakefile'));
- if (process.platform == 'win32') {
- filepath = '"' + filepath + '"';
- }
- cmd = 'jake -t -f ' + filepath + ' ';
- if (cmds[0] != 'secret' && !cmds[1]) {
- throw new Error(cmds[0] + ' command requires another argument.');
- }
- switch (cmds[0]) {
- case 'app':
- cmd += 'gen:app[' + cmds[1] + ']';
- break;
- case 'resource':
- cmd += 'gen:resource[' + cmds[1] + ']';
- break;
- case 'controller':
- cmd += 'gen:bareController[' + cmds[1] + ']';
- break;
- case 'model':
- cmd += 'gen:model[' + cmds[1] + ']';
- break;
- case 'secret':
- cmd += 'gen:secret';
- break;
- default:
- die(cmds[0] + ' is not a Geddy command.');
- }
- cmd += ' --quiet';
- exec(cmd, function (err, stdout, stderr) {
- if (err) {
- throw err;
- }
- else {
- if (stderr) {
- console.log(utils.string.trim(stderr));
- }
- if (stdout) {
- console.log(utils.string.trim(stdout));
- }
- }
- });
+if(opts.help) die(usage);
+if(opts.version) die(geddy.version);
+
+// `geddy app foo` or `geddy resource bar` etc. -- run generators
+if(cmds.length) {
+ filepath = path.normalize(path.join(__dirname, '..', 'templates', 'Jakefile'));
+
+ // Wrap quotes in case path has spaces
+ if(process.platform === 'win32') filepath = '"' + filepath + '"';
+ cmd = 'jake -t -f ' + filepath + ' ';
+
+ // If command isn't secret and has no other argument
+ if (cmds[0] !== 'secret' && !cmds[1]) {
+ throw new Error(cmds[0] + ' command requires another argument.');
}
- // Just `geddy` -- start the server
- else {
- var relPath = '';
-
- // Search for the `config` directory needed to run Geddy
- // - up to 5 parent directories then show the usage
- for(var i = 0, len = 5; i <= len; i++) {
- var configPath = path.join(cwd, relPath, 'config')
- , existsSync = typeof fs.existsSync == 'function' ?
- fs.existsSync : path.existsSync
- , geddyApp = existsSync(configPath);
-
- if(geddyApp) {
- break;
- } else {
- // If 5 directories up show usage
- if(i === 5) die(usage);
-
- // Add a relative parent directory
- relPath += '../';
- process.chdir(path.join(cwd, relPath));
- }
- }
- start(); // Start the server up
+
+ switch(cmds[0]) {
+ case 'app':
+ // Generating application
+ cmd += 'gen:app[' + cmds[1];
+
+ if(opts.jade) cmd += ',' + 'jade';
+ if(opts.handle) cmd += ',' + 'handlebars';
+ if(opts.mustache) cmd += ',' + 'mustache';
+
+ cmd += ']';
+ break;
+ case 'resource':
+ // Generating resources
+ cmd += 'gen:resource[' + cmds[1];
+
+ if(opts.jade) cmd += ',' + 'jade';
+ if(opts.handle) cmd += ',' + 'handlebars';
+ if(opts.mustache) cmd += ',' + 'mustache';
+
+ cmd += ']';
+ break;
+ case 'controller':
+ // Generating controller
+ cmd += 'gen:bareController[' + cmds[1];
+
+ if(opts.jade) cmd += ',' + 'jade';
+ if(opts.handle) cmd += ',' + 'handlebars';
+ if(opts.mustache) cmd += ',' + 'mustache';
+
+ cmd += ']';
+ break;
+ case 'model':
+ // Generating model
+ cmd += 'gen:model[' + cmds[1] + ']';
+ break;
+ case 'secret':
+ // Generating new app secret
+ cmd += 'gen:secret';
+ break;
+ default:
+ die(cmds[0] + ' is not a Geddy command.');
}
+
+ cmd += ' --quiet';
+ exec(cmd, function(err, stdout, stderr) {
+ if(err) throw err;
+
+ if(stderr) console.log(utils.string.trim(stderr));
+ if(stdout) console.log(utils.string.trim(stdout));
+ });
}
+// Just `geddy` -- start the server
+else {
+ var relPath = '';
+
+ // Search for the `config` directory needed to run Geddy
+ // - up to 5 parent directories then show the usage
+ for(var i = 0, len = utils.file.maxParentDir; i <= len; i++) {
+ var configPath = path.join(cwd, relPath, 'config')
+ , existsSync = typeof fs.existsSync == 'function' ?
+ fs.existsSync : path.existsSync
+ , geddyApp = existsSync(configPath);
+ if(geddyApp) {
+ break;
+ } else {
+ // If 5 directories up show usage
+ if(i === len) die(usage);
+ // Add a relative parent directory
+ relPath += '../';
+ process.chdir(path.join(cwd, relPath));
+ }
+ }
+ start(); // Start the server up
+}
View
@@ -249,6 +249,9 @@ var fileUtils = new (function () {
return str || '.';
};
+ // Maximum directory count to search for parent directories
+ this.maxParentDir = 5;
+
})();
module.exports = fileUtils;
View
@@ -17,6 +17,7 @@
*/
var geddy
, string = require('./string').string
+ , fileUtils = require('./file')
, async = require('./async').async
, uri = require('./uri').uri
, array = require('./array').array
@@ -113,6 +114,8 @@ var utils = new (function () {
})();
utils.string = string;
+utils.file = fileUtils;
+utils.fileUtils = fileUtils;
utils.async = async;
utils.uri = uri;
utils.array = array;
Oops, something went wrong.

0 comments on commit ae3f611

Please sign in to comment.