Permalink
Browse files

The 'create' task can now add actions to controllers.

  • Loading branch information...
1 parent 5e2b434 commit 78fe68fbd9541eb88e608a1a5558d561c5ad4363 @thatismatt committed Jun 11, 2010
Showing with 36 additions and 11 deletions.
  1. +36 −11 lib/josi/tasks/create.js
View
@@ -23,7 +23,7 @@ this.task = {
sys.puts('App created. Type "cd ' + appName + '; josi run" to start the web server.');
} else {
if (!utilities.cwdContainsApp()) {
- sys.puts('ERROR: The creation of josi components can only be done within a josi app.')
+ sys.puts('ERROR: The creation of josi components can only be done within a josi app.');
return;
}
var component = args[0].toLowerCase();
@@ -34,13 +34,20 @@ this.task = {
createView('.', args[1], 'index');
sys.puts('Controller created.');
break;
- // todo: case 'view':
- // // todo: fail nicely if missing args
- // sys.puts('Creating view...');
- // createView('.', arg[1], arg[2]);
- // sys.puts('View created.');
- // break;
- // todo: case 'action':
+ case 'action':
+ if (args.length != 3) {
+ sys.puts('ERROR: The name of the action to be created must be specified.');
+ return;
+ }
+ if (!utilities.fileOrDirectoryExists('controllers/' + args[1] + '.js')) {
+ sys.puts('ERROR: The "' + args[1] + '" controller doesn\'t exist.');
+ return;
+ }
+ sys.puts('Creating action...');
+ createView('.', args[1], args[2]);
+ addActionToController(args[1], args[2]);
+ sys.puts('Action created.');
+ break;
// todo: case 'test':
default:
sys.puts('ERROR: "' + args[1] + '" is not a valid josi component to create.');
@@ -66,7 +73,9 @@ var createApp = function(appName) {
fs.mkdirSync(appName + '/views', 0777);
fs.writeFileSync(appName + '/views/master.html',
[ '<html>',
- ' <head><title><%= title %></title></head>',
+ ' <head>',
+ ' <title><%= title %></title>',
+ ' </head>',
' <body>',
' <h1><%= title %></h1>',
' <%= main %>',
@@ -84,7 +93,7 @@ var createController = function(appName, controllerName) {
}
var filename = appName + '/controllers/' + controllerName + '.js';
if (utilities.fileOrDirectoryExists(filename)) {
- throw new Error('ERROR: Controller already exists');
+ throw new Error('Controller already exists');
}
// todo: if appname is '.' create a different index action
fs.writeFileSync(filename,
@@ -116,7 +125,23 @@ var createView = function(appName, controllerName, viewName) {
}
fs.writeFileSync(filename,
[ '<p>App name: <b><%= name %></b></p>',
- '<p><%= description %></p>'
+ '<p><%= description %></p>', ''
+ ].join('\r\n')
+ );
+};
+
+var addActionToController = function(controllerName, actionName) {
+ var writeStream = fs.createWriteStream('controllers/' + controllerName + '.js', { flags: 'a' });
+ writeStream.write(
+ [ '',
+ 'this.' + actionName + ' = function() {',
+ ' return view({',
+ ' title: \'' + controllerName + '.' + actionName + '\',',
+ ' name: \'' + controllerName + '.' + actionName + '\',',
+ ' description: \'This is the ' + actionName + ' action on the ' + controllerName + ' controller.\'',
+ ' });',
+ '};', ''
].join('\r\n')
);
+ writeStream.end();
};

0 comments on commit 78fe68f

Please sign in to comment.