diff --git a/app/index.js b/app/index.js index b987d44ce..5c3ef0673 100644 --- a/app/index.js +++ b/app/index.js @@ -8,379 +8,412 @@ var chalk = require('chalk'); var AngularFullstackGenerator = yeoman.generators.Base.extend({ - init: function () { - this.argument('name', { type: String, required: false }); - this.appname = this.name || path.basename(process.cwd()); - this.appname = this._.camelize(this._.slugify(this._.humanize(this.appname))); - - this.option('app-suffix', { - desc: 'Allow a custom suffix to be added to the module name', - type: String, - required: 'false' - }); - this.scriptAppName = this.appname + genUtils.appName(this); - this.appPath = this.env.options.appPath; - this.pkg = require('../package.json'); - - this.filters = {}; - - // dynamic assertion statement - this.does = this.is = function(foo) { - if (this.filters.should) { - return foo + '.should'; + initializing: { + + init: function () { + this.argument('name', { type: String, required: false }); + this.appname = this.name || path.basename(process.cwd()); + this.appname = this._.camelize(this._.slugify(this._.humanize(this.appname))); + + this.option('app-suffix', { + desc: 'Allow a custom suffix to be added to the module name', + type: String, + required: 'false' + }); + this.scriptAppName = this.appname + genUtils.appName(this); + this.appPath = this.env.options.appPath; + this.pkg = require('../package.json'); + + this.filters = {}; + + // dynamic assertion statement + this.does = this.is = function(foo) { + if (this.filters.should) { + return foo + '.should'; + } else { + return 'expect(' + foo + ').to'; + } + }.bind(this); + }, + + info: function () { + this.log(this.yeoman); + this.log('Out of the box I create an AngularJS app with an Express server.\n'); + }, + + checkForConfig: function() { + var cb = this.async(); + + if(this.config.get('filters')) { + this.prompt([{ + type: 'confirm', + name: 'skipConfig', + message: 'Existing .yo-rc configuration found, would you like to use it?', + default: true, + }], function (answers) { + this.skipConfig = answers.skipConfig; + + this.filters = this._.defaults(this.config.get('filters'), { + bootstrap: true, + uibootstrap: true, + jasmine: true + }); + + // NOTE: temp(?) fix for #403 + if(typeof this.filters.oauth==='undefined') { + var strategies = Object.keys(this.filters).filter(function(key) { + return key.match(/Auth$/) && key; + }); + + if(strategies.length) this.filters.oauth = true; + } + + this.config.forceSave(); + + cb(); + }.bind(this)); } else { - return 'expect(' + foo + ').to'; + cb(); } - }.bind(this); - }, + } - info: function () { - this.log(this.yeoman); - this.log('Out of the box I create an AngularJS app with an Express server.\n'); }, - checkForConfig: function() { - var cb = this.async(); + prompting: { - if(this.config.get('filters')) { - this.prompt([{ - type: 'confirm', - name: 'skipConfig', - message: 'Existing .yo-rc configuration found, would you like to use it?', - default: true, - }], function (answers) { - this.skipConfig = answers.skipConfig; - - this.filters = this._.defaults(this.config.get('filters'), { - bootstrap: true, - uibootstrap: true, - jasmine: true - }); - - // NOTE: temp(?) fix for #403 - if(typeof this.filters.oauth==='undefined') { - var strategies = Object.keys(this.filters).filter(function(key) { - return key.match(/Auth$/) && key; - }); - - if(strategies.length) this.filters.oauth = true; - } + clientPrompts: function() { + if(this.skipConfig) return; + var cb = this.async(); - this.config.forceSave(); + this.log('# Client\n'); - cb(); - }.bind(this)); - } else { - cb(); - } - }, + this.prompt([{ + type: 'list', + name: 'script', + message: 'What would you like to write scripts with?', + choices: [ 'JavaScript', 'CoffeeScript'], + filter: function( val ) { + var filterMap = { + 'JavaScript': 'js', + 'CoffeeScript': 'coffee' + }; + + return filterMap[val]; + } + }, { + type: 'confirm', + name: 'babel', + message: 'Would you like to use Javascript ES6 in your client by preprocessing it with Babel?', + when: function (answers) { + return answers.script === 'js'; + } + }, { + type: 'list', + name: 'markup', + message: 'What would you like to write markup with?', + choices: ['HTML', 'Jade'], + filter: function( val ) { return val.toLowerCase(); } + }, { + type: 'list', + name: 'stylesheet', + default: 1, + message: 'What would you like to write stylesheets with?', + choices: [ 'CSS', 'Sass', 'Stylus', 'Less'], + filter: function( val ) { return val.toLowerCase(); } + }, { + type: 'list', + name: 'router', + default: 1, + message: 'What Angular router would you like to use?', + choices: [ 'ngRoute', 'uiRouter'], + filter: function( val ) { return val.toLowerCase(); } + }, { + type: 'confirm', + name: 'bootstrap', + message: 'Would you like to include Bootstrap?' + }, { + type: 'confirm', + name: 'uibootstrap', + message: 'Would you like to include UI Bootstrap?', + when: function (answers) { + return answers.bootstrap; + } + }], function (answers) { + + this.filters.babel = !!answers.babel; + if(this.filters.babel){ this.filters.js = true; } + this.filters[answers.script] = true; + this.filters[answers.markup] = true; + this.filters[answers.stylesheet] = true; + this.filters[answers.router] = true; + this.filters.bootstrap = !!answers.bootstrap; + this.filters.uibootstrap = !!answers.uibootstrap; + cb(); + }.bind(this)); + }, - clientPrompts: function() { - if(this.skipConfig) return; - var cb = this.async(); + serverPrompts: function() { + if(this.skipConfig) return; + var cb = this.async(); + var self = this; - this.log('# Client\n'); + this.log('\n# Server\n'); - this.prompt([{ + this.prompt([{ + type: 'checkbox', + name: 'odms', + message: 'What would you like to use for data modeling?', + choices: [ + { + value: 'mongoose', + name: 'Mongoose (MongoDB)', + checked: true + }, + { + value: 'sequelize', + name: 'Sequelize (MySQL, SQLite, MariaDB, PostgreSQL)', + checked: false + } + ] + }, { type: 'list', - name: 'script', - message: 'What would you like to write scripts with?', - choices: [ 'JavaScript', 'CoffeeScript'], + name: 'models', + message: 'What would you like to use for the default models?', + choices: [ 'Mongoose', 'Sequelize' ], filter: function( val ) { - var filterMap = { - 'JavaScript': 'js', - 'CoffeeScript': 'coffee' - }; - - return filterMap[val]; + return val.toLowerCase(); + }, + when: function(answers) { + return answers.odms && answers.odms.length > 1; } }, { type: 'confirm', - name: 'babel', - message: 'Would you like to use Javascript ES6 in your client by preprocessing it with Babel?', + name: 'auth', + message: 'Would you scaffold out an authentication boilerplate?', when: function (answers) { - return answers.script === 'js'; + return answers.odms && answers.odms.length !== 0; } }, { - type: 'list', - name: 'markup', - message: 'What would you like to write markup with?', - choices: ['HTML', 'Jade'], - filter: function( val ) { return val.toLowerCase(); } - }, { - type: 'list', - name: 'stylesheet', - default: 1, - message: 'What would you like to write stylesheets with?', - choices: [ 'CSS', 'Sass', 'Stylus', 'Less'], - filter: function( val ) { return val.toLowerCase(); } - }, { - type: 'list', - name: 'router', - default: 1, - message: 'What Angular router would you like to use?', - choices: [ 'ngRoute', 'uiRouter'], - filter: function( val ) { return val.toLowerCase(); } - }, { - type: 'confirm', - name: 'bootstrap', - message: 'Would you like to include Bootstrap?' + type: 'checkbox', + name: 'oauth', + message: 'Would you like to include additional oAuth strategies?', + when: function (answers) { + return answers.auth; + }, + choices: [ + { + value: 'googleAuth', + name: 'Google', + checked: false + }, + { + value: 'facebookAuth', + name: 'Facebook', + checked: false + }, + { + value: 'twitterAuth', + name: 'Twitter', + checked: false + } + ] }, { type: 'confirm', - name: 'uibootstrap', - message: 'Would you like to include UI Bootstrap?', + name: 'socketio', + message: 'Would you like to use socket.io?', + // to-do: should not be dependent on ODMs when: function (answers) { - return answers.bootstrap; - } + return answers.odms && answers.odms.length !== 0; + }, + default: true }], function (answers) { + if(answers.socketio) this.filters.socketio = true; + if(answers.auth) this.filters.auth = true; + if(answers.odms && answers.odms.length > 0) { + var models; + if(!answers.models) { + models = answers.odms[0]; + } else { + models = answers.models; + } + this.filters.models = true; + this.filters[models + 'Models'] = true; + answers.odms.forEach(function(odm) { + this.filters[odm] = true; + }.bind(this)); + } else { + this.filters.noModels = true; + } + if(answers.oauth) { + if(answers.oauth.length) this.filters.oauth = true; + answers.oauth.forEach(function(oauthStrategy) { + this.filters[oauthStrategy] = true; + }.bind(this)); + } - this.filters.babel = !!answers.babel; - if(this.filters.babel){ this.filters.js = true; } - this.filters[answers.script] = true; - this.filters[answers.markup] = true; - this.filters[answers.stylesheet] = true; - this.filters[answers.router] = true; - this.filters.bootstrap = !!answers.bootstrap; - this.filters.uibootstrap = !!answers.uibootstrap; cb(); }.bind(this)); - }, + }, - serverPrompts: function() { - if(this.skipConfig) return; - var cb = this.async(); - var self = this; - - this.log('\n# Server\n'); - - this.prompt([{ - type: 'checkbox', - name: 'odms', - message: 'What would you like to use for data modeling?', - choices: [ - { - value: 'mongoose', - name: 'Mongoose (MongoDB)', - checked: true - }, - { - value: 'sequelize', - name: 'Sequelize (MySQL, SQLite, MariaDB, PostgreSQL)', - checked: false + projectPrompts: function() { + if(this.skipConfig) return; + var cb = this.async(); + var self = this; + + this.log('\n# Project\n'); + + this.prompt([{ + type: 'list', + name: 'testing', + message: 'What would you like to write tests with?', + choices: [ 'Jasmine', 'Mocha + Chai + Sinon'], + filter: function( val ) { + var filterMap = { + 'Jasmine': 'jasmine', + 'Mocha + Chai + Sinon': 'mocha' + }; + + return filterMap[val]; } - ] - }, { - type: 'list', - name: 'models', - message: 'What would you like to use for the default models?', - choices: [ 'Mongoose', 'Sequelize' ], - filter: function( val ) { - return val.toLowerCase(); - }, - when: function(answers) { - return answers.odms && answers.odms.length > 1; - } - }, { - type: 'confirm', - name: 'auth', - message: 'Would you scaffold out an authentication boilerplate?', - when: function (answers) { - return answers.odms && answers.odms.length !== 0; - } - }, { - type: 'checkbox', - name: 'oauth', - message: 'Would you like to include additional oAuth strategies?', - when: function (answers) { - return answers.auth; - }, - choices: [ - { - value: 'googleAuth', - name: 'Google', - checked: false - }, - { - value: 'facebookAuth', - name: 'Facebook', - checked: false + }, { + type: 'list', + name: 'chai', + message: 'What would you like to write Chai assertions with?', + choices: ['Expect', 'Should'], + filter: function( val ) { + return val.toLowerCase(); }, - { - value: 'twitterAuth', - name: 'Twitter', - checked: false + when: function( answers ) { + return answers.testing === 'mocha'; } - ] - }, { - type: 'confirm', - name: 'socketio', - message: 'Would you like to use socket.io?', - // to-do: should not be dependent on ODMs - when: function (answers) { - return answers.odms && answers.odms.length !== 0; - }, - default: true - }], function (answers) { - if(answers.socketio) this.filters.socketio = true; - if(answers.auth) this.filters.auth = true; - if(answers.odms && answers.odms.length > 0) { - var models; - if(!answers.models) { - models = answers.odms[0]; - } else { - models = answers.models; + }], function (answers) { + this.filters[answers.testing] = true; + if (answers.testing === 'mocha') { + this.filters.jasmine = false; + this.filters.should = false; + this.filters.expect = false; + this.filters[answers.chai] = true; + } + if (answers.testing === 'jasmine') { + this.filters.mocha = false; + this.filters.should = false; + this.filters.expect = false; } - this.filters.models = true; - this.filters[models + 'Models'] = true; - answers.odms.forEach(function(odm) { - this.filters[odm] = true; - }.bind(this)); - } else { - this.filters.noModels = true; - } - if(answers.oauth) { - if(answers.oauth.length) this.filters.oauth = true; - answers.oauth.forEach(function(oauthStrategy) { - this.filters[oauthStrategy] = true; - }.bind(this)); - } - cb(); - }.bind(this)); + cb(); + }.bind(this)); + } + }, - projectPrompts: function() { - if(this.skipConfig) return; - var cb = this.async(); - var self = this; - - this.log('\n# Project\n'); - - this.prompt([{ - type: 'list', - name: 'testing', - message: 'What would you like to write tests with?', - choices: [ 'Jasmine', 'Mocha + Chai + Sinon'], - filter: function( val ) { - var filterMap = { - 'Jasmine': 'jasmine', - 'Mocha + Chai + Sinon': 'mocha' - }; - - return filterMap[val]; - } - }, { - type: 'list', - name: 'chai', - message: 'What would you like to write Chai assertions with?', - choices: ['Expect', 'Should'], - filter: function( val ) { - return val.toLowerCase(); - }, - when: function( answers ) { - return answers.testing === 'mocha'; - } - }], function (answers) { - this.filters[answers.testing] = true; - if (answers.testing === 'mocha') { - this.filters.jasmine = false; - this.filters.should = false; - this.filters.expect = false; - this.filters[answers.chai] = true; - } - if (answers.testing === 'jasmine') { - this.filters.mocha = false; - this.filters.should = false; - this.filters.expect = false; - } + configuring: { + + saveSettings: function() { + if(this.skipConfig) return; + this.config.set('insertRoutes', true); + this.config.set('registerRoutesFile', 'server/routes.js'); + this.config.set('routesNeedle', '// Insert routes below'); + + this.config.set('routesBase', '/api/'); + this.config.set('pluralizeRoutes', true); + + this.config.set('insertSockets', true); + this.config.set('registerSocketsFile', 'server/config/socketio.js'); + this.config.set('socketsNeedle', '// Insert sockets below'); + + this.config.set('insertModels', true); + this.config.set('registerModelsFile', 'server/sqldb/index.js'); + this.config.set('modelsNeedle', '// Insert models below'); + + this.config.set('filters', this.filters); + this.config.forceSave(); + }, + + ngComponent: function() { + if(this.skipConfig) return; + var appPath = 'client/app/'; + var extensions = []; + var filters = [ + 'ngroute', + 'uirouter', + 'jasmine', + 'mocha', + 'expect', + 'should' + ].filter(function(v) {return this.filters[v];}, this); + + if(this.filters.ngroute) filters.push('ngroute'); + if(this.filters.uirouter) filters.push('uirouter'); + if(this.filters.babel) extensions.push('babel'); + if(this.filters.coffee) extensions.push('coffee'); + if(this.filters.js) extensions.push('js'); + if(this.filters.html) extensions.push('html'); + if(this.filters.jade) extensions.push('jade'); + if(this.filters.css) extensions.push('css'); + if(this.filters.stylus) extensions.push('styl'); + if(this.filters.sass) extensions.push('scss'); + if(this.filters.less) extensions.push('less'); + + this.composeWith('ng-component', { + options: { + 'routeDirectory': appPath, + 'directiveDirectory': appPath, + 'filterDirectory': appPath, + 'serviceDirectory': appPath, + 'filters': filters, + 'extensions': extensions, + 'basePath': 'client' + } + }, { local: require.resolve('generator-ng-component/app/index.js') }); + }, + + ngModules: function() { + var angModules = [ + "'ngCookies'", + "'ngResource'", + "'ngSanitize'" + ]; + if(this.filters.ngroute) angModules.push("'ngRoute'"); + if(this.filters.socketio) angModules.push("'btford.socket-io'"); + if(this.filters.uirouter) angModules.push("'ui.router'"); + if(this.filters.uibootstrap) angModules.push("'ui.bootstrap'"); + + this.angularModules = '\n ' + angModules.join(',\n ') +'\n'; + } - cb(); - }.bind(this)); }, - saveSettings: function() { - if(this.skipConfig) return; - this.config.set('insertRoutes', true); - this.config.set('registerRoutesFile', 'server/routes.js'); - this.config.set('routesNeedle', '// Insert routes below'); + default: {}, - this.config.set('routesBase', '/api/'); - this.config.set('pluralizeRoutes', true); + writing: { - this.config.set('insertSockets', true); - this.config.set('registerSocketsFile', 'server/config/socketio.js'); - this.config.set('socketsNeedle', '// Insert sockets below'); + generateProject: function() { + this.sourceRoot(path.join(__dirname, './templates')); + genUtils.processDirectory(this, '.', '.'); + }, - this.config.set('insertModels', true); - this.config.set('registerModelsFile', 'server/sqldb/index.js'); - this.config.set('modelsNeedle', '// Insert models below'); + generateEndpoint: function() { + var name = this.name = this.cameledName = 'thing'; + this.classedName = name.charAt(0).toUpperCase() + name.slice(1); + this.route = '/api/' + name + 's'; + this.sourceRoot(path.join(__dirname, '..', 'endpoint', 'templates')); + genUtils.processDirectory(this, '.', 'server/api/' + name); + } - this.config.set('filters', this.filters); - this.config.forceSave(); }, - compose: function() { - if(this.skipConfig) return; - var appPath = 'client/app/'; - var extensions = []; - var filters = [ - 'ngroute', - 'uirouter', - 'jasmine', - 'mocha', - 'expect', - 'should' - ].filter(function(v) {return this.filters[v];}, this); - - if(this.filters.ngroute) filters.push('ngroute'); - if(this.filters.uirouter) filters.push('uirouter'); - if(this.filters.babel) extensions.push('babel'); - if(this.filters.coffee) extensions.push('coffee'); - if(this.filters.js) extensions.push('js'); - if(this.filters.html) extensions.push('html'); - if(this.filters.jade) extensions.push('jade'); - if(this.filters.css) extensions.push('css'); - if(this.filters.stylus) extensions.push('styl'); - if(this.filters.sass) extensions.push('scss'); - if(this.filters.less) extensions.push('less'); - - this.composeWith('ng-component', { - options: { - 'routeDirectory': appPath, - 'directiveDirectory': appPath, - 'filterDirectory': appPath, - 'serviceDirectory': appPath, - 'filters': filters, - 'extensions': extensions, - 'basePath': 'client' - } - }, { local: require.resolve('generator-ng-component/app/index.js') }); - }, + install: { - ngModules: function() { - var angModules = [ - "'ngCookies'", - "'ngResource'", - "'ngSanitize'" - ]; - if(this.filters.ngroute) angModules.push("'ngRoute'"); - if(this.filters.socketio) angModules.push("'btford.socket-io'"); - if(this.filters.uirouter) angModules.push("'ui.router'"); - if(this.filters.uibootstrap) angModules.push("'ui.bootstrap'"); - - this.angularModules = '\n ' + angModules.join(',\n ') +'\n'; - }, + installDeps: function() { + this.installDependencies({ + skipInstall: this.options['skip-install'] + }); + } - generate: function() { - this.sourceRoot(path.join(__dirname, './templates')); - genUtils.processDirectory(this, '.', '.'); }, - end: function() { - this.installDependencies({ - skipInstall: this.options['skip-install'] - }); - } + end: {} + }); module.exports = AngularFullstackGenerator; diff --git a/app/templates/server/api/thing/index.js b/app/templates/server/api/thing/index.js deleted file mode 100644 index fde8f3968..000000000 --- a/app/templates/server/api/thing/index.js +++ /dev/null @@ -1,15 +0,0 @@ -'use strict'; - -var express = require('express'); -var controller = require('./thing.controller'); - -var router = express.Router(); - -router.get('/', controller.index);<% if (filters.models) { %> -router.get('/:id', controller.show); -router.post('/', controller.create); -router.put('/:id', controller.update); -router.patch('/:id', controller.update); -router.delete('/:id', controller.destroy);<% } %> - -module.exports = router; diff --git a/app/templates/server/api/thing/index.spec.js b/app/templates/server/api/thing/index.spec.js deleted file mode 100644 index 91f02f6df..000000000 --- a/app/templates/server/api/thing/index.spec.js +++ /dev/null @@ -1,97 +0,0 @@ -'use strict'; - -var proxyquire = require('proxyquire').noPreserveCache(); - -var thingCtrlStub = { - index: 'thingCtrl.index'<% if (filters.models) { %>, - show: 'thingCtrl.show', - create: 'thingCtrl.create', - update: 'thingCtrl.update', - destroy: 'thingCtrl.destroy'<% } %> -}; - -var routerStub = { - get: sinon.spy()<% if (filters.models) { %>, - put: sinon.spy(), - patch: sinon.spy(), - post: sinon.spy(), - delete: sinon.spy()<% } %> -}; - -// require the index with our stubbed out modules -var thingIndex = proxyquire('./index.js', { - 'express': { - Router: function() { - return routerStub; - } - }, - './thing.controller': thingCtrlStub -}); - -describe('Thing API Router:', function() { - - it('should return an express router instance', function() { - thingIndex.should.equal(routerStub); - }); - - describe('GET /api/things', function() { - - it('should route to thing.controller.index', function() { - routerStub.get - .withArgs('/', 'thingCtrl.index') - .should.have.been.calledOnce; - }); - - });<% if (filters.models) { %> - - describe('GET /api/things/:id', function() { - - it('should route to thing.controller.show', function() { - routerStub.get - .withArgs('/:id', 'thingCtrl.show') - .should.have.been.calledOnce; - }); - - }); - - describe('POST /api/things', function() { - - it('should route to thing.controller.create', function() { - routerStub.post - .withArgs('/', 'thingCtrl.create') - .should.have.been.calledOnce; - }); - - }); - - describe('PUT /api/things/:id', function() { - - it('should route to thing.controller.update', function() { - routerStub.put - .withArgs('/:id', 'thingCtrl.update') - .should.have.been.calledOnce; - }); - - }); - - describe('PATCH /api/things/:id', function() { - - it('should route to thing.controller.update', function() { - routerStub.patch - .withArgs('/:id', 'thingCtrl.update') - .should.have.been.calledOnce; - }); - - }); - - describe('DELETE /api/things/:id', function() { - - it('should route to thing.controller.destroy', function() { - routerStub.delete - .withArgs('/:id', 'thingCtrl.destroy') - .should.have.been.calledOnce; - }); - - });<% } %> - -}); diff --git a/app/templates/server/api/thing/thing.controller(models).js b/app/templates/server/api/thing/thing.controller(models).js deleted file mode 100644 index c7874b515..000000000 --- a/app/templates/server/api/thing/thing.controller(models).js +++ /dev/null @@ -1,124 +0,0 @@ -/** - * Using Rails-like standard naming convention for endpoints. - * GET /things -> index - * POST /things -> create - * GET /things/:id -> show - * PUT /things/:id -> update - * DELETE /things/:id -> destroy - */ - -'use strict'; - -var _ = require('lodash');<% if (filters.mongooseModels) { %> -var Thing = require('./thing.model');<% } %><% if (filters.sequelizeModels) { %> -var sqldb = require('../../sqldb') -var Thing = sqldb.Thing;<% } %> - -function handleError(res, statusCode) { - statusCode = statusCode || 500; - return function(err) { - res.status(statusCode).send(err); - }; -} - -function responseWithResult(res, statusCode) { - statusCode = statusCode || 200; - return function(entity) { - if (entity) { - return res.status(statusCode).json(entity); - } - }; -} - -function handleEntityNotFound(res) { - return function(entity) { - if (!entity) { - res.status(404).end(); - return null; - } - return entity; - }; -} - -function saveUpdates(updates) { - return function(entity) { - <% if (filters.mongooseModels) { %>var updated = _.merge(entity, updates); - return updated.saveAsync() - .spread(function(updated) {<% } - if (filters.sequelizeModels) { %>return entity.updateAttributes(updates) - .then(function(updated) {<% } %> - return updated; - }); - }; -} - -function removeEntity(res) { - return function(entity) { - if (entity) { - <% if (filters.mongooseModels) { %>return entity.removeAsync()<% } - if (filters.sequelizeModels) { %>return entity.destroy()<% } %> - .then(function() { - return res.status(204).end(); - }); - } - }; -} - -// Get list of things -exports.index = function(req, res) { - <% if (filters.mongooseModels) { %>Thing.findAsync()<% } - if (filters.sequelizeModels) { %>Thing.findAll()<% } %> - .then(responseWithResult(res)) - .catch(handleError(res)); -}; - -// Get a single thing -exports.show = function(req, res) { - <% if (filters.mongooseModels) { %>Thing.findByIdAsync(req.params.id)<% } - if (filters.sequelizeModels) { %>Thing.find({ - where: { - _id: req.params.id - } - })<% } %> - .then(handleEntityNotFound(res)) - .then(responseWithResult(res)) - .catch(handleError(res)); -}; - -// Creates a new thing in the DB. -exports.create = function(req, res) { - <% if (filters.mongooseModels) { %>Thing.createAsync(req.body)<% } - if (filters.sequelizeModels) { %>Thing.create(req.body)<% } %> - .then(responseWithResult(res, 201)) - .catch(handleError(res)); -}; - -// Updates an existing thing in the DB. -exports.update = function(req, res) { - if (req.body._id) { - delete req.body._id; - } - <% if (filters.mongooseModels) { %>Thing.findByIdAsync(req.params.id)<% } - if (filters.sequelizeModels) { %>Thing.find({ - where: { - _id: req.params.id - } - })<% } %> - .then(handleEntityNotFound(res)) - .then(saveUpdates(req.body)) - .then(responseWithResult(res)) - .catch(handleError(res)); -}; - -// Deletes a thing from the DB. -exports.destroy = function(req, res) { - <% if (filters.mongooseModels) { %>Thing.findByIdAsync(req.params.id)<% } - if (filters.sequelizeModels) { %>Thing.find({ - where: { - _id: req.params.id - } - })<% } %> - .then(handleEntityNotFound(res)) - .then(removeEntity(res)) - .catch(handleError(res)); -}; diff --git a/app/templates/server/api/thing/thing.controller(noModels).js b/app/templates/server/api/thing/thing.controller(noModels).js deleted file mode 100644 index a39041258..000000000 --- a/app/templates/server/api/thing/thing.controller(noModels).js +++ /dev/null @@ -1,38 +0,0 @@ -/** - * Using Rails-like standard naming convention for endpoints. - * GET /things -> index - * POST /things -> create - * GET /things/:id -> show - * PUT /things/:id -> update - * DELETE /things/:id -> destroy - */ - -'use strict'; - -// Get list of things -exports.index = function(req, res) { - res.json([{ - name: 'Development Tools', - info: 'Integration with popular tools such as Bower, Grunt, Karma, Mocha, JSHint, ' + - 'Node Inspector, Livereload, Protractor, Jade, Stylus, Sass, CoffeeScript, and Less.' - }, { - name: 'Server and Client integration', - info: 'Built with a powerful and fun stack: MongoDB, Express, AngularJS, and Node.' - }, { - name: 'Smart Build System', - info: 'Build system ignores `spec` files, allowing you to keep tests alongside code. ' + - 'Automatic injection of scripts and styles into your index.html' - }, { - name: 'Modular Structure', - info: 'Best practice client and server structures allow for more code reusability and ' + - 'maximum scalability' - }, { - name: 'Optimized Build', - info: 'Build process packs up your templates as a single JavaScript payload, minifies ' + - 'your scripts/css/images, and rewrites asset names for caching.' - }, { - name: 'Deployment Ready', - info: 'Easily deploy your app to Heroku or Openshift with the heroku and openshift ' + - 'sub-generators' - }]); -}; diff --git a/app/templates/server/api/thing/thing.integration.js b/app/templates/server/api/thing/thing.integration.js deleted file mode 100644 index 3eb5d05d8..000000000 --- a/app/templates/server/api/thing/thing.integration.js +++ /dev/null @@ -1,147 +0,0 @@ -'use strict'; - -var app = require('../../app'); -var request = require('supertest');<% if (filters.models) { %> - -var newThing;<% } %> - -describe('Thing API:', function() { - - describe('GET /api/things', function() { - var things; - - beforeEach(function(done) { - request(app) - .get('/api/things') - .expect(200) - .expect('Content-Type', /json/) - .end(function(err, res) { - if (err) { - return done(err); - } - things = res.body; - done(); - }); - }); - - it('should respond with JSON array', function() { - things.should.be.instanceOf(Array); - }); - - });<% if (filters.models) { %> - - describe('POST /api/things', function() { - beforeEach(function(done) { - request(app) - .post('/api/things') - .send({ - name: 'New Thing', - info: 'This is the brand new thing!!!' - }) - .expect(201) - .expect('Content-Type', /json/) - .end(function(err, res) { - if (err) { - return done(err); - } - newThing = res.body; - done(); - }); - }); - - it('should respond with the newly created thing', function() { - newThing.name.should.equal('New Thing'); - newThing.info.should.equal('This is the brand new thing!!!'); - }); - - }); - - describe('GET /api/things/:id', function() { - var thing; - - beforeEach(function(done) { - request(app) - .get('/api/things/' + newThing._id) - .expect(200) - .expect('Content-Type', /json/) - .end(function(err, res) { - if (err) { - return done(err); - } - thing = res.body; - done(); - }); - }); - - afterEach(function() { - thing = {}; - }); - - it('should respond with the requested thing', function() { - thing.name.should.equal('New Thing'); - thing.info.should.equal('This is the brand new thing!!!'); - }); - - }); - - describe('PUT /api/things/:id', function() { - var updatedThing - - beforeEach(function(done) { - request(app) - .put('/api/things/' + newThing._id) - .send({ - name: 'Updated Thing', - info: 'This is the updated thing!!!' - }) - .expect(200) - .expect('Content-Type', /json/) - .end(function(err, res) { - if (err) { - return done(err); - } - updatedThing = res.body; - done(); - }); - }); - - afterEach(function() { - updatedThing = {}; - }); - - it('should respond with the updated thing', function() { - updatedThing.name.should.equal('Updated Thing'); - updatedThing.info.should.equal('This is the updated thing!!!'); - }); - - }); - - describe('DELETE /api/things/:id', function() { - - it('should respond with 204 on successful removal', function(done) { - request(app) - .delete('/api/things/' + newThing._id) - .expect(204) - .end(function(err, res) { - if (err) { - return done(err); - } - done(); - }); - }); - - it('should respond with 404 when thing does not exist', function(done) { - request(app) - .delete('/api/things/' + newThing._id) - .expect(404) - .end(function(err, res) { - if (err) { - return done(err); - } - done(); - }); - }); - - });<% } %> - -}); diff --git a/app/templates/server/api/thing/thing.model(mongooseModels).js b/app/templates/server/api/thing/thing.model(mongooseModels).js deleted file mode 100644 index d1e6e49ea..000000000 --- a/app/templates/server/api/thing/thing.model(mongooseModels).js +++ /dev/null @@ -1,12 +0,0 @@ -'use strict'; - -var mongoose = require('mongoose-bird')(); -var Schema = mongoose.Schema; - -var ThingSchema = new Schema({ - name: String, - info: String, - active: Boolean -}); - -module.exports = mongoose.model('Thing', ThingSchema); diff --git a/app/templates/server/api/thing/thing.model(sequelizeModels).js b/app/templates/server/api/thing/thing.model(sequelizeModels).js deleted file mode 100644 index 8e8072da4..000000000 --- a/app/templates/server/api/thing/thing.model(sequelizeModels).js +++ /dev/null @@ -1,15 +0,0 @@ -'use strict'; - -module.exports = function(sequelize, DataTypes) { - return sequelize.define('Thing', { - _id: { - type: DataTypes.INTEGER, - allowNull: false, - primaryKey: true, - autoIncrement: true - }, - name: DataTypes.STRING, - info: DataTypes.STRING, - active: DataTypes.BOOLEAN - }); -}; diff --git a/app/templates/server/api/thing/thing.socket(socketio).js b/app/templates/server/api/thing/thing.socket(socketio).js deleted file mode 100644 index d0d98a6ca..000000000 --- a/app/templates/server/api/thing/thing.socket(socketio).js +++ /dev/null @@ -1,33 +0,0 @@ -/** - * Broadcast updates to client when the model changes - */ - -'use strict'; -<% if (filters.mongooseModels) { %> -var thing = require('./thing.model');<% } %><% if (filters.sequelizeModels) { %> -var thing = require('../../sqldb').Thing;<% } %> - -exports.register = function(socket) {<% if (filters.sequelizeModels) { %> - thing.hook('afterCreate', function(doc, fields, fn) { - onSave(socket, doc); - fn(null); - });<% } %> - <% if (filters.mongooseModels) { %>thing.schema.post('save', function(doc) {<% } - if (filters.sequelizeModels) { %>thing.hook('afterUpdate', function(doc, fields, fn) {<% } %> - onSave(socket, doc);<% if (filters.sequelizeModels) { %> - fn(null);<% } %> - }); - <% if (filters.mongooseModels) { %>thing.schema.post('remove', function(doc) {<% } - if (filters.sequelizeModels) { %>thing.hook('afterDestroy', function(doc, fields, fn) {<% } %> - onRemove(socket, doc);<% if (filters.sequelizeModels) { %> - fn(null);<% } %> - }); -}; - -function onSave(socket, doc, cb) { - socket.emit('thing:save', doc); -} - -function onRemove(socket, doc, cb) { - socket.emit('thing:remove', doc); -}