Skip to content
Browse files

Adding the beginnings of automated component creation to the 'create'…

… task.
  • Loading branch information...
1 parent ddb32ee commit 9c960a0361d08d15b24c816a6d97e5fb6a64f929 @thatismatt committed Jun 7, 2010
Showing with 43 additions and 13 deletions.
  1. +43 −13 lib/josi/tasks/create.js
View
56 lib/josi/tasks/create.js
@@ -5,21 +5,48 @@ var utilities = require('josi/utilities');
this.task = {
name: 'create',
- doc: 'create a new josi app or parts of an existing josi app',
+ doc: 'create a new josi app or components of an existing josi app',
execute: function(opts, args) {
- var appName = opts.name || args[0];
- if (!appName) {
- sys.puts('ERROR: When creating a josi app you must specify its name.');
- return;
+ if (args.length < 2) {
+ var appName = opts.name || args[0];
+ if (!appName) {
+ sys.puts('ERROR: When creating a josi app you must specify its name.');
+ return;
+ }
+ if (utilities.fileOrDirectoryExists(appName)) {
+ sys.puts('ERROR: Can\'t create a josi app named "' + appName + '",' +
+ ' as a file or directory with that name already exists.');
+ return;
+ }
+ sys.puts('Creating app...');
+ createApp(appName);
+ 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.')
+ return;
+ }
+ var component = args[0].toLowerCase();
+ switch (component) {
+ case 'controller':
+ sys.puts('Creating controller...');
+ createController('.', args[1]);
+ 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':
+ // todo: case 'test':
+ default:
+ sys.puts('ERROR: "' + args[1] + '" is not a valid josi component to create.');
+ return;
+ }
}
- if (utilities.fileOrDirectoryExists(appName)) {
- sys.puts('ERROR: Can\'t create a josi app named "' + appName + '",' +
- ' as a file or directory with that name already exists.');
- return;
- }
- sys.puts('Creating app...');
- createApp(appName);
- sys.puts('App created. Type "cd ' + appName + '; josi run" to start the web server.');
},
}
@@ -55,6 +82,8 @@ var createController = function(appName, controllerName) {
if (!utilities.fileOrDirectoryExists(controllersDir)) {
fs.mkdirSync(controllersDir, 0777);
}
+ // todo: don't overwrite controller with the same name
+ // todo: if appname is '.' create a different index action
fs.writeFileSync(appName + '/controllers/' + controllerName + '.js',
[ 'var view = require(\'josi/actionresults\').view;',
'',
@@ -78,6 +107,7 @@ var createView = function(appName, controllerName, viewName) {
if (!utilities.fileOrDirectoryExists(viewsDir)) {
fs.mkdirSync(viewsDir, 0777);
}
+ // todo: don't overwrite view with the same name
fs.writeFileSync(appName + '/views/' + controllerName + '/' + viewName + '.html',
[
'<p>App name: <b><%= name %></b></p>',

0 comments on commit 9c960a0

Please sign in to comment.
Something went wrong with that request. Please try again.