From 06fcc18caea2a3a7cfbe3d65257e7ed682a560a5 Mon Sep 17 00:00:00 2001 From: kingcody Date: Tue, 1 Sep 2015 23:30:12 -0400 Subject: [PATCH] feat(gen): support ES2015 syntax through `babel-core/register` --- app/generator.js | 434 ++++++++++++++++++++++++++++++++++++++++++ app/index.js | 417 +--------------------------------------- endpoint/generator.js | 151 +++++++++++++++ endpoint/index.js | 152 +-------------- package.json | 1 + 5 files changed, 594 insertions(+), 561 deletions(-) create mode 100644 app/generator.js create mode 100644 endpoint/generator.js diff --git a/app/generator.js b/app/generator.js new file mode 100644 index 000000000..517773bc8 --- /dev/null +++ b/app/generator.js @@ -0,0 +1,434 @@ +'use strict'; + +import fs from 'fs'; +import path from 'path'; +import genUtils from '../util.js'; +import {Base} from 'yeoman-generator'; +import chalk from 'chalk'; + +export default class Generator extends Base { + + constructor(...args) { + super(...args); + + this.argument('name', { type: String, required: false }); + this.option('app-suffix', { + desc: 'Allow a custom suffix to be added to the module name', + type: String, + required: 'false' + }); + } + + get initializing() { + return { + + init: function () { + this.appname = this.name || path.basename(process.cwd()); + this.appname = this._.camelize(this._.slugify(this._.humanize(this.appname))); + + this.scriptAppName = this.appname + genUtils.appName(this); + console.log(this.scriptAppName); + this.appPath = this.env.options.appPath; + this.pkg = require('../package.json'); + + this.filters = {}; + + // dynamic assertion statements + this.expect = function() { + return this.filters.expect ? 'expect(' : ''; + }.bind(this); + this.to = function() { + return this.filters.expect ? ').to' : '.should'; + }.bind(this); + }, + + info: function () { + this.log(this.welcome); + 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 + }); + + this.config.set('filters', this.filters); + this.config.forceSave(); + + cb(); + }.bind(this)); + } else { + cb(); + } + } + + }; + } + + get prompting() { + return { + + clientPrompts: function() { + if(this.skipConfig) return; + var cb = this.async(); + + this.log('# Client\n'); + + this.prompt([{ + type: 'list', + name: 'script', + message: 'What would you like to write scripts with?', + choices: [ 'JavaScript', 'JavaScript + Babel', 'CoffeeScript'], + filter: function( val ) { + return { + 'JavaScript': 'js', + 'JavaScript + Babel': 'babel', + 'CoffeeScript': 'coffee' + }[val]; + } + }, { + 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) { + + // also set 'js' to true if using babel + if(answers.script === '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 + } + ] + }, { + 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 + }, + { + value: 'twitterAuth', + name: 'Twitter', + checked: false + } + ] + }, { + 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; + } + 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)); + }, + + 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) { + /** + * Default to grunt until gulp support is implemented + */ + this.filters.grunt = true; + + 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; + } + + cb(); + }.bind(this)); + } + + }; + } + + get configuring() { + return { + + saveSettings: function() { + if(this.skipConfig) return; + this.config.set('endpointDirectory', 'server/api/'); + 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'; + } + + }; + } + + get default() { + return {}; + } + + get writing() { + return { + + generateProject: function() { + this.sourceRoot(path.join(__dirname, './templates')); + genUtils.processDirectory(this, '.', '.'); + }, + + generateEndpoint: function() { + var models; + if (this.filters.mongooseModels) { + models = 'mongoose'; + } else if (this.filters.sequelizeModels) { + models = 'sequelize'; + } + this.composeWith('angular-fullstack:endpoint', { + options: { + route: '/api/things', + models: models + }, + args: ['thing'] + }); + } + + }; + } + + get install() { + return { + + installDeps: function() { + this.installDependencies({ + skipInstall: this.options['skip-install'] + }); + } + + }; + } + + get end() { + return {}; + } + +} diff --git a/app/index.js b/app/index.js index cd55ab4eb..994cdee97 100644 --- a/app/index.js +++ b/app/index.js @@ -1,416 +1,7 @@ 'use strict'; -var fs = require('fs'); -var path = require('path'); -var util = require('util'); -var genUtils = require('../util.js'); -var yeoman = require('yeoman-generator'); -var chalk = require('chalk'); -var AngularFullstackGenerator = yeoman.generators.Base.extend({ +// Register the Babel require hook +require('babel-core/register'); - 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 statements - this.expect = function() { - return this.filters.expect ? 'expect(' : ''; - }.bind(this); - this.to = function() { - return this.filters.expect ? ').to' : '.should'; - }.bind(this); - }, - - info: function () { - this.log(this.welcome); - 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 - }); - - this.config.set('filters', this.filters); - this.config.forceSave(); - - cb(); - }.bind(this)); - } else { - cb(); - } - } - - }, - - prompting: { - - clientPrompts: function() { - if(this.skipConfig) return; - var cb = this.async(); - - this.log('# Client\n'); - - this.prompt([{ - type: 'list', - name: 'script', - message: 'What would you like to write scripts with?', - choices: [ 'JavaScript', 'JavaScript + Babel', 'CoffeeScript'], - filter: function( val ) { - return { - 'JavaScript': 'js', - 'JavaScript + Babel': 'babel', - 'CoffeeScript': 'coffee' - }[val]; - } - }, { - 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) { - - // also set 'js' to true if using babel - if(answers.script === '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 - } - ] - }, { - 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 - }, - { - value: 'twitterAuth', - name: 'Twitter', - checked: false - } - ] - }, { - 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; - } - 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)); - }, - - 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) { - /** - * Default to grunt until gulp support is implemented - */ - this.filters.grunt = true; - - 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; - } - - cb(); - }.bind(this)); - } - - }, - - configuring: { - - saveSettings: function() { - if(this.skipConfig) return; - this.config.set('endpointDirectory', 'server/api/'); - 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'; - } - - }, - - default: {}, - - writing: { - - generateProject: function() { - this.sourceRoot(path.join(__dirname, './templates')); - genUtils.processDirectory(this, '.', '.'); - }, - - generateEndpoint: function() { - var models; - if (this.filters.mongooseModels) { - models = 'mongoose'; - } else if (this.filters.sequelizeModels) { - models = 'sequelize'; - } - this.composeWith('angular-fullstack:endpoint', { - options: { - route: '/api/things', - models: models - }, - args: ['thing'] - }); - } - - }, - - install: { - - installDeps: function() { - this.installDependencies({ - skipInstall: this.options['skip-install'] - }); - } - - }, - - end: {} - -}); - -module.exports = AngularFullstackGenerator; +// Export the generator +exports = module.exports = require('./generator'); diff --git a/endpoint/generator.js b/endpoint/generator.js new file mode 100644 index 000000000..b4757d743 --- /dev/null +++ b/endpoint/generator.js @@ -0,0 +1,151 @@ +'use strict'; + +import path from 'path'; +import ngUtil from '../util'; +import ScriptBase from '../script-base.js'; + +export default class Generator extends ScriptBase { + + constructor(...args) { + super(...args); + + this.option('route', { + desc: 'URL for the endpoint', + type: String + }); + + this.option('models', { + desc: 'Specify which model(s) to use', + type: String + }); + + this.option('endpointDirectory', { + desc: 'Parent directory for enpoints', + type: String + }); + } + + prompting() { + var done = this.async(); + var promptCb = function (props) { + if(props.route.charAt(0) !== '/') { + props.route = '/' + props.route; + } + + this.route = props.route; + + if (props.models) { + delete this.filters.mongoose; + delete this.filters.mongooseModels; + delete this.filters.sequelize; + delete this.filters.sequelizeModels; + + this.filters[props.models] = true; + this.filters[props.models + 'Models'] = true; + } + done(); + }.bind(this); + + if (this.options.route) { + if (this.filters.mongoose && this.filters.sequelize) { + if (this.options.models) { + return promptCb(this.options); + } + } else { + if (this.filters.mongooseModels) { this.options.models = 'mongoose'; } + else if (this.filters.sequelizeModels) { this.options.models = 'sequelize'; } + else { delete this.options.models; } + return promptCb(this.options); + } + } + + var name = this.name; + + var base = this.config.get('routesBase') || '/api/'; + if(base.charAt(base.length-1) !== '/') { + base = base + '/'; + } + + // pluralization defaults to true for backwards compat + if (this.config.get('pluralizeRoutes') !== false) { + name = name + 's'; + } + + var self = this; + var prompts = [ + { + name: 'route', + message: 'What will the url of your endpoint be?', + default: base + name + }, + { + type: 'list', + name: 'models', + message: 'What would you like to use for the endpoint\'s models?', + choices: [ 'Mongoose', 'Sequelize' ], + default: self.filters.sequelizeModels ? 1 : 0, + filter: function( val ) { + return val.toLowerCase(); + }, + when: function() { + return self.filters.mongoose && self.filters.sequelize; + } + } + ]; + + this.prompt(prompts, promptCb); + } + + configuring() { + this.routeDest = path.join(this.options.endpointDirectory || + this.config.get('endpointDirectory') || 'server/api/', this.name); + } + + writing() { + this.sourceRoot(path.join(__dirname, './templates')); + ngUtil.processDirectory(this, '.', this.routeDest); + } + + end() { + if(this.config.get('insertRoutes')) { + var routesFile = this.config.get('registerRoutesFile'); + var reqPath = this.relativeRequire(this.routeDest, routesFile); + var routeConfig = { + file: routesFile, + needle: this.config.get('routesNeedle'), + splicable: [ + "app.use(\'" + this.route +"\', require(\'" + reqPath + "\'));" + ] + }; + ngUtil.rewriteFile(routeConfig); + } + + if (this.filters.socketio && this.config.get('insertSockets')) { + var socketsFile = this.config.get('registerSocketsFile'); + var reqPath = this.relativeRequire(this.routeDest + '/' + this.basename + + '.socket', socketsFile); + var socketConfig = { + file: socketsFile, + needle: this.config.get('socketsNeedle'), + splicable: [ + "require(\'" + reqPath + "\').register(socket);" + ] + }; + ngUtil.rewriteFile(socketConfig); + } + + if (this.filters.sequelize && this.config.get('insertModels')) { + var modelsFile = this.config.get('registerModelsFile'); + var reqPath = this.relativeRequire(this.routeDest + '/' + this.basename + + '.model', modelsFile); + var modelConfig = { + file: modelsFile, + needle: this.config.get('modelsNeedle'), + splicable: [ + "db." + this.classedName + " = db.sequelize.import(\'" + reqPath +"\');" + ] + }; + ngUtil.rewriteFile(modelConfig); + } + } +} diff --git a/endpoint/index.js b/endpoint/index.js index f92517436..994cdee97 100644 --- a/endpoint/index.js +++ b/endpoint/index.js @@ -1,151 +1,7 @@ 'use strict'; -var path = require('path'); -var yeoman = require('yeoman-generator'); -var util = require('util'); -var ngUtil = require('../util'); -var ScriptBase = require('../script-base.js'); -var Generator = module.exports = function Generator() { - ScriptBase.apply(this, arguments); +// Register the Babel require hook +require('babel-core/register'); - this.option('route', { - desc: 'URL for the endpoint', - type: String - }); - - this.option('models', { - desc: 'Specify which model(s) to use', - type: String - }); - - this.option('endpointDirectory', { - desc: 'Parent directory for enpoints', - type: String - }); -}; - -util.inherits(Generator, ScriptBase); - -Generator.prototype.prompting = function askFor() { - var done = this.async(); - var promptCb = function (props) { - if(props.route.charAt(0) !== '/') { - props.route = '/' + props.route; - } - - this.route = props.route; - - if (props.models) { - delete this.filters.mongoose; - delete this.filters.mongooseModels; - delete this.filters.sequelize; - delete this.filters.sequelizeModels; - - this.filters[props.models] = true; - this.filters[props.models + 'Models'] = true; - } - done(); - }.bind(this); - - if (this.options.route) { - if (this.filters.mongoose && this.filters.sequelize) { - if (this.options.models) { - return promptCb(this.options); - } - } else { - if (this.filters.mongooseModels) { this.options.models = 'mongoose'; } - else if (this.filters.sequelizeModels) { this.options.models = 'sequelize'; } - else { delete this.options.models; } - return promptCb(this.options); - } - } - - var name = this.name; - - var base = this.config.get('routesBase') || '/api/'; - if(base.charAt(base.length-1) !== '/') { - base = base + '/'; - } - - // pluralization defaults to true for backwards compat - if (this.config.get('pluralizeRoutes') !== false) { - name = name + 's'; - } - - var self = this; - var prompts = [ - { - name: 'route', - message: 'What will the url of your endpoint be?', - default: base + name - }, - { - type: 'list', - name: 'models', - message: 'What would you like to use for the endpoint\'s models?', - choices: [ 'Mongoose', 'Sequelize' ], - default: self.filters.sequelizeModels ? 1 : 0, - filter: function( val ) { - return val.toLowerCase(); - }, - when: function() { - return self.filters.mongoose && self.filters.sequelize; - } - } - ]; - - this.prompt(prompts, promptCb); -}; - -Generator.prototype.configuring = function config() { - this.routeDest = path.join(this.options.endpointDirectory || - this.config.get('endpointDirectory') || 'server/api/', this.name); -}; - -Generator.prototype.writing = function createFiles() { - this.sourceRoot(path.join(__dirname, './templates')); - ngUtil.processDirectory(this, '.', this.routeDest); -}; - -Generator.prototype.end = function registerEndpoint() { - if(this.config.get('insertRoutes')) { - var routesFile = this.config.get('registerRoutesFile'); - var reqPath = this.relativeRequire(this.routeDest, routesFile); - var routeConfig = { - file: routesFile, - needle: this.config.get('routesNeedle'), - splicable: [ - "app.use(\'" + this.route +"\', require(\'" + reqPath + "\'));" - ] - }; - ngUtil.rewriteFile(routeConfig); - } - - if (this.filters.socketio && this.config.get('insertSockets')) { - var socketsFile = this.config.get('registerSocketsFile'); - var reqPath = this.relativeRequire(this.routeDest + '/' + this.basename + - '.socket', socketsFile); - var socketConfig = { - file: socketsFile, - needle: this.config.get('socketsNeedle'), - splicable: [ - "require(\'" + reqPath + "\').register(socket);" - ] - }; - ngUtil.rewriteFile(socketConfig); - } - - if (this.filters.sequelize && this.config.get('insertModels')) { - var modelsFile = this.config.get('registerModelsFile'); - var reqPath = this.relativeRequire(this.routeDest + '/' + this.basename + - '.model', modelsFile); - var modelConfig = { - file: modelsFile, - needle: this.config.get('modelsNeedle'), - splicable: [ - "db." + this.classedName + " = db.sequelize.import(\'" + reqPath +"\');" - ] - }; - ngUtil.rewriteFile(modelConfig); - } -}; +// Export the generator +exports = module.exports = require('./generator'); diff --git a/package.json b/package.json index 7c401ec46..27dd3aed3 100644 --- a/package.json +++ b/package.json @@ -26,6 +26,7 @@ "test": "grunt test" }, "dependencies": { + "babel-core": "^5.8.23", "chalk": "^1.1.0", "generator-ng-component": "~0.1.0", "yeoman-generator": "~0.18.10"