Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: ae3f611a00
Fetching contributors…

Cannot retrieve contributors at this time

219 lines (180 sloc) 7.069 kb
// Load the basic Geddy toolkit
require('../lib/geddy')
// Dependencies
var fs = require('fs')
, path = require('path')
, existsSync = typeof fs.existsSync == 'function' ?
fs.existsSync : path.existsSync
, inflection = require('../deps/inflection')
, utils = require('../lib/utils')
, adapter = require('../lib/template/adapters').Adapter
, templato = new (require('../deps/templato'));
namespace('gen', function () {
var _writeTemplate = function (name, filename, dirname, options) {
options = options || {};
var names = _getInflections(name)
, text = fs.readFileSync(path.join(__dirname, filename + '.ejs'), 'utf8').toString()
, templ
, filePath;
// Render with the right model name
templ = new adapter({ data: {ext: '.ejs'}, text: text, templato: templato });
templ.process({names: names});
// Write file
filePath = path.join('app', dirname, names.filename[options.inflection] + '.js');
fs.writeFileSync(filePath, templ.markup, 'utf8');
console.log('[ADDED] ' + filePath);
};
var _getInflections = function (nameParam) {
var name = utils.string.snakeize(nameParam)
, namePlural = inflection.pluralize(name);
return {
filename: {
singular: name
, plural: namePlural
},
constructor: {
singular: utils.string.camelize(name, true)
, plural: utils.string.camelize(namePlural, true)
},
property: {
singular: utils.string.camelize(name)
, plural: utils.string.camelize(namePlural)
}
};
};
// Creates a new Geddy app scaffold
task('app', [], function(name, engine) {
if(!name) throw new Error('No app-name specified.');
if(!engine) engine = 'ejs';
var mkdirs = [
''
, 'config'
, 'app/models'
, 'app/controllers'
, 'lib'
, 'log'
, 'node_modules'
]
, cps = [
['views/' + engine, 'app/views']
, ['public', '']
, ['router.js', 'config']
, ['init.js', 'config']
, ['environment.js', 'config']
, ['development.js', 'config']
, ['production.js', 'config']
, ['main.js', 'app/controllers']
, ['application.js', 'app/controllers']
, ['favicon.ico', 'public']
];
mkdirs.forEach(function(dir) {
jake.mkdirP(path.join(name, dir));
});
cps.forEach(function(cp) {
jake.cpR(path.join(__dirname, 'base', cp[0]), path.join(name, cp[1]));
});
console.log('Created app ' + name + '.');
});
// Creates a resource(Model, Controller, Views and resource route)
task('resource', function(name, engine) {
jake.Task['gen:model'].invoke(name);
jake.Task['gen:controller'].invoke(name);
jake.Task['gen:route'].invoke(name);
jake.Task['gen:views'].invoke(name, { engine: engine });
});
task('model', [], function(name) {
_writeTemplate(name, 'resource_model', 'models', { inflection: 'singular' });
});
task('controller', [], function(name) {
_writeTemplate(name, 'resource_controller', 'controllers', { inflection: 'plural' });
});
task('route', [], function(name, options) {
options = options || {};
var names = _getInflections(name)
, routeType = options.bare ? 'Bare' : 'Resource'
, filePath = path.normalize('config/router.js')
, text = fs.readFileSync(filePath, 'utf8').toString()
, newRoute
, routeArr;
if (options.bare) {
newRoute = 'router.match(\'/' + names.filename.plural +
'\').to({controller: \'' + names.constructor.plural +
'\', action: \'index\'});';
} else newRoute = 'router.resource(\'' + names.filename.plural + '\');'
// Don't add the same route over and over
if(text.indexOf(newRoute) == -1) {
// Add the new resource route just above the export
routerArr = text.split('exports.router');
routerArr[0] += newRoute + '\n';
text = routerArr.join('exports.router');
fs.writeFileSync(filePath, text, 'utf8');
console.log(routeType + ' ' + names.filename.plural +
' route added to ' + filePath);
}
else {
console.log('(' + routeType + ' ' + names.filename.plural +
' route already defined in ' + filePath + ')');
}
});
task('views', [], function(name, options) {
options = options || {};
// If `engine` option isn't set then default to EJS
if(!options.engine) options.engine = 'ejs';
var names = _getInflections(name)
, engine = options.engine
, appViewDir = path.join('app', 'views', names.filename.plural)
, templateViewDir = path.join(__dirname, 'views', engine)
, cmds = []
, ext = '.html'
, appLayoutPath
, actions
, addActionView;
// Set extension based on engine option
if(engine === 'ejs') ext += '.ejs';
if(engine === 'jade') ext += '.jade';
if(engine === 'handlebars') ext += '.hbs';
if(engine === 'mustache') ext += '.ms';
// Set application layout path
appLayoutPath = path.join('app', 'views', 'layouts', 'application');
// Copy template view to application path
addActionView = function(action) {
jake.cpR(path.join(templateViewDir, action + ext), appViewDir);
};
jake.mkdirP(appViewDir);
jake.mkdirP('app/views/layouts');
addActionView('index');
// Add views for the other CRUD actions when doing a full-on resource
if(!options.bare) {
['add', 'edit', 'show'].forEach(function (action) {
addActionView(action);
});
}
// Create default layout if one doesn't exist
// Hack: There should be a better way to detect if a application layout exists
if(!existsSync(appLayoutPath + '.html.ejs') && !existsSync(appLayoutPath + '.html.jade') &&
!existsSync(appLayoutPath + '.html.hbs') && !existsSync(appLayoutPath + '.html.ms')) {
// Copy template layout file to apps application layout file
jake.cpR(path.join(templateViewDir, 'layout' + ext), appLayoutPath + ext);
}
console.log('Created view templates.');
});
task('bareController', [], function(name, engine) {
_writeTemplate(name, 'bare_controller', 'controllers', { inflection: 'plural' });
jake.Task['gen:route'].invoke(name, { bare: true });
jake.Task['gen:views'].invoke(name, { bare: true, engine: engine });
});
// Generate a new application secret in environment.js
task('secret', [], function(name) {
var filename = path.join(process.cwd(), 'config/environment.js')
, conf = fs.readFileSync(filename).toString()
, secret = utils.string.uuid(128)
, confArr;
// Remove any old secret
conf = conf.replace(/\nconfig.secret.+;\n/, '');
confArr = conf.split('module.exports = config;');
conf = confArr[0] + "config.secret = '" + secret + "';\n\n" +
'module.exports = config;' + confArr[1];
fs.writeFileSync(filename, conf);
console.log('app-secret added to environment.js config.');
});
});
Jump to Line
Something went wrong with that request. Please try again.