Skip to content

Commit

Permalink
Version 0.0.9, scaffolding
Browse files Browse the repository at this point in the history
  • Loading branch information
Anatoliy Chakkaev committed Mar 27, 2011
1 parent 67772ae commit 108de4c
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 11 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
@@ -1,3 +1,8 @@
0.0.9

Scaffold generator `railway generate scaffold ModelName field1:type1 field2:type2`
It generates model, crud controller and appropriated views

0.0.8

Now you can run `railway init blog` and railway will create directory structure
Expand Down
34 changes: 24 additions & 10 deletions bin/railway
Expand Up @@ -22,6 +22,13 @@ function createDir (dir) {
}
}

function appendToFile (filename, contents) {
var root = process.cwd() + '/',
fd = fs.openSync(root + filename, 'a');
fs.writeSync(fd, contents);
fs.closeSync(fd);
}

function createFile (filename, contents) {
var root = process.cwd();
if (options.appname) {
Expand Down Expand Up @@ -74,20 +81,19 @@ var sys = require('sys');
var path = require('path');
var generators = {
model: function (args) {
var config = JSON.parse(fs.readFileSync(process.cwd() + '/config/database.json', 'utf-8'))[process.env.NODE_ENV || 'development'];
var model = args.shift(), code = '';
if (!model) {
sys.puts($('Model name required').red.bold);
return;
}
var Model = model[0].toUpperCase() + model.slice(1);
var attrs = [], result = [];
var attrs = [], result = [], driver = ormDriver();
args.forEach(function (arg) {
var property = arg.split(':')[0],
plainType = arg.split(':')[1],
type = formatType(plainType);

if (config.driver == 'mongoose') {
if (driver == 'mongoose') {
attrs.push(property + ': { type: ' + type + ' }');
} else {
attrs.push(' property("' + property + '", ' + type + ');');
Expand All @@ -96,11 +102,13 @@ var generators = {
});
createDir('app/');
createDir('app/models/');
if (config.driver == 'mongoose') {
// code = 'var ModelSchema = new Schema({\n ' + attrs.join(',\n ') + '\n});\n';
// code += 'mongoose.model("Model", ModelSchema);\n';
// code += 'var Model = mongoose.model("Model");\n';
// code += 'export("' + Model + '", ' + Model + ');';
if (driver == 'mongoose') {
var schema = '\n\n/**\n * ' + Model + '\n */\nvar ' + Model + 'Schema = new Schema;\n';
schema += Model + 'Schema.add({\n ' + attrs.join(',\n ') + '\n});\n';
schema += 'mongoose.model("' + Model + '", ' + Model + 'Schema);\n';
schema += 'module.exports["' + Model + '"] = mongoose.model("' + Model + '");';
appendToFile('db/schema.js', schema);

code = '';
} else {
code = 'var ' + Model + ' = describe("' + Model + '", function () {\n' +
Expand Down Expand Up @@ -192,8 +200,7 @@ var generators = {
function controllerCode(model) {
var code, _model = model.toLowerCase();
try {
var config = JSON.parse(require('fs').readFileSync(process.cwd() + '/config/database.json', 'utf8'))['development'];
code = fs.readFileSync(__dirname + '/../templates/crud_controller_' + config.driver + '.js');
code = fs.readFileSync(__dirname + '/../templates/crud_controller_' + ormDriver() + '.js');
} catch (e) {
code = fs.readFileSync(__dirname + '/../templates/crud_controller_redis.js');
}
Expand Down Expand Up @@ -362,3 +369,10 @@ case 'generate':
function replaceAppname (template) {
return template.replace(/APPNAME/g, options.appname || 'default');
}

function ormDriver () {
if (!ormDriver.config) {
ormDriver.config = JSON.parse(require('fs').readFileSync(process.cwd() + '/config/database.json', 'utf8')).development;
}
return ormDriver.config.driver;
}
2 changes: 1 addition & 1 deletion package.json
@@ -1,5 +1,5 @@
{ "name": "express-on-railway"
, "version": "0.0.8"
, "version": "0.0.9"
, "author": "Anatoliy Chakkaev"
, "description": "Railway features for Express.js framework"
, "homepage": "http://github.com/1602/express-on-railway"
Expand Down
2 changes: 2 additions & 0 deletions templates/views/index.ejs
Expand Up @@ -5,3 +5,5 @@
<%- link_to('Edit', path_to.edit_model(model)) %>
<%- link_to('Delete', path_to.model(model), {method: 'delete'}) %><br/>
<% }); %>

<%- link_to('New model', path_to.new_model) %>

0 comments on commit 108de4c

Please sign in to comment.