Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

[minor] api changes

  • Loading branch information...
commit b4111a71666c3503731021e2a9d2d07d97d7b2c0 1 parent 74ac13d
@edwardhotchkiss authored
View
2  bin/angular.js
@@ -11,7 +11,7 @@ switch(command) {
case 'version':
case '-v':
case '--version':
- console.log(angular.version().green);
+ angular.log('version', angular.version());
break;
case 'n':
case 'new':
View
4 lib/index.js
@@ -1,6 +1,7 @@
/**
* @class angular
+ * @returns {Object}
*/
var angular = module.exports = {
@@ -10,7 +11,8 @@ var angular = module.exports = {
server : require('./node-angular/server'),
help : require('./node-angular/help'),
load : require('./node-angular/loader'),
- init : require('./node-angular/init')
+ init : require('./node-angular/init'),
+ log : require('./node-angular/log')
};
/* EOF */
View
64 lib/node-angular/generate.js
@@ -5,7 +5,15 @@
* @params {Object} args
*/
-require('colors')
+require('colors');
+
+/**
+ * deps
+ */
+
+var fs = require('fs')
+ , path = require('path')
+ , log = require('./log')
var generate = module.exports = function(args) {
if (!args[0]) {
@@ -15,30 +23,48 @@ var generate = module.exports = function(args) {
switch (generator) {
case 'scaffold':
if (args.length < 3) {
- console.error('node-angular'.yellow.inverse + ' action name, and model attributes required!'.yellow);
+ log('warn', 'action name, and model attributes required!');
} else {
- console.log(args);
+ // action
var action = args[1];
+ // model attributes
var attributes = args.splice(2, args.length);
- console.log(attributes);
- /*
- create app/models/post.js
- create app/controllers/posts_controller.js
- create app/helpers/posts_helper.js
- create app/views/layouts/posts_layout.ejs
- create public/stylesheets/scaffold.css
- create app/views/posts/
- create app/views/posts/_form.ejs
- create app/views/posts/new.ejs
- create app/views/posts/edit.ejs
- create app/views/posts/index.ejs
- create app/views/posts/show.ejs
- patch config/routes.js
- */
+ // generate model
+ var model = '\n';
+ model += "var mongoose = require('mongoose)'\n";
+ model += " , Schema = mongoose.Schema\n";
+ model += " , ObjectId = Schema.ObjectId;\n\n";
+ model += "var " + action + " = mongoose.model('" + action + "', {\n";
+ model += " id : { 'type' : ObjectId },\n";
+ attributes.map(function(attribute) {
+ if (/:/.test(attribute)) {
+ var parts = attribute.split(':');
+ var attributeName = parts[0];
+ var attributeType = parts[1];
+ model += " " + attributeName + " : { 'type' : " + attributeType + " },\n";
+ } else {
+ model += " " + attribute + " : { 'type' : String },\n";
+ };
+ });
+ model += " created_at : { 'type' : Date, 'default': Date.now }\n";
+ model += "});\n\n";
+ model += "module.exports = " + action + ";\n";
+ // save model
+ path.exists(process.cwd() + '/app/models/', function(exists) {
+ if (exists) {
+ var modelDestination = process.cwd() + '/app/models/' + action + '.js';
+ fs.writeFile(modelDestination, model, 'utf8', function(error, result) {
+ console.log(error, result);
+ });
+ } else {
+ log('error', 'Not in an node-angular project!');
+ };
+ });
+ // generate route
};
break;
default:
- console.error('node-angular'.yellow.inverse + ' unknown generator type!'.yellow);
+ log('warn', 'unknown generator type!');
};
}
};
View
5 lib/node-angular/init.js
@@ -8,11 +8,12 @@
require('colors');
var fs = require('fs')
- , utils = require('./utils');
+ , utils = require('./utils')
+ , log = require('./log');
var init = module.exports = function(args) {
if (!args[0]) {
- console.log('"new" requires a project name (String)'.yellow);
+ log('error', '"new" requires a project name!');
return;
};
var project = args[0];
View
11 lib/node-angular/loader.js
@@ -7,7 +7,8 @@
*/
var fs = require('fs')
- , path = require('path');
+ , path = require('path')
+ , log = require('./log')
function models(directory) {
directory = process.cwd() + (directory || '/app/models');
@@ -15,9 +16,9 @@ function models(directory) {
if (error) {
console.error(error);
if (/ENOENT/.test(error.message)) {
- throw new Error('angular looks in /app/controllers/ by default, try "angular --help"');
+ log('error', 'angular looks in /app/controllers/ by default, try "angular --help"');
} else {
- throw new Error(error);
+ log('error', error.message);
};
} else {
files.forEach(function(file) {
@@ -37,9 +38,9 @@ function controllers(directory, app) {
if (error) {
console.error(error);
if (/ENOENT/.test(error.message)) {
- throw new Error('angular looks in /app/controllers/ by default, try "angular --help"');
+ log('error', 'angular looks in /app/controllers/ by default, try "angular --help"');
} else {
- throw new Error(error);
+ log('error', error.message);
};
} else {
files.forEach(function(file) {
View
30 lib/node-angular/log.js
@@ -0,0 +1,30 @@
+
+/**
+ * @class angular
+ * @method log
+ * @param {String} directory Directory to create
+ * @param {String} project Project name
+ */
+
+var log = module.exports = function(type, message) {
+ switch(type) {
+ case 'create':
+ console.log('create'.green + ' ' + message);
+ break;
+ case 'patch':
+ console.log('patch'.magenta + ' ' + message);
+ break;
+ case 'warn':
+ console.log('warn:'.yellow.inverse + ' ' + message.yellow);
+ break;
+ case 'error':
+ console.log('error:'.red.inverse + ' ' + message.red);
+ break;
+ case 'version':
+ console.log('node-angular'.green.inverse + ' ' + message.green);
+ default:
+ break;
+ };
+};
+
+/* EOF */
View
5 lib/node-angular/server.js
@@ -6,17 +6,16 @@
*/
var path = require('path')
- , utils = require('./utils');
+ , log = require('./utils');
var server = module.exports = function(args) {
- var port = (args.length > 0) ? args[0] : 9000;
var server = process.cwd() + '/server.js'
path.exists(server, function(exists) {
if (exists) {
var serverFile = require(server);
serverFile.listen(port);
} else {
- utils.log('warn', 'No server.js file found!');
+ log('warn', 'No server.js file found!');
};
});
};
View
29 lib/node-angular/utils.js
@@ -12,7 +12,8 @@ var utils = module.exports = {};
var fs = require('fs')
, util = require('util')
, path = require('path')
- , colors = require('colors');
+ , colors = require('colors')
+ , log = require('./log')
/**
* @class utils
@@ -49,30 +50,4 @@ var mkAppDirSync = utils.mkAppDirSync = function(directory, project) {
};
};
-/**
- * @class utils
- * @method log
- * @param {String} directory Directory to create
- * @param {String} project Project name
- */
-
-var log = utils.log = function(type, message) {
- switch(type) {
- case 'create':
- console.log('create'.green + ' ' + message);
- break;
- case 'patch':
- console.log('patch'.magenta + ' ' + message);
- break;
- case 'warn':
- console.log('warn'.yellow.inverse + ' ' + message);
- break;
- case 'error':
- console.log('create'.red.inverse + ' ' + message);
- break;
- default:
- break;
- };
-};
-
/* EOF */
View
2  package.json
@@ -6,7 +6,7 @@
],
"homepage":"http://github.com/edwardhotchkiss/node-angular",
"description":"Node.JS + Angular.JS project generator",
- "version":"0.0.4",
+ "version":"0.0.5",
"repository":{
"type":"git",
"url":"git://github.com/edwardhotchkiss/node-angular.git"
Please sign in to comment.
Something went wrong with that request. Please try again.