Skip to content

Commit

Permalink
feat(generators): add answers validation and laundering, normalize ac…
Browse files Browse the repository at this point in the history
…ross subgenerators, tweak and normalize phrasings
  • Loading branch information
cueedee committed May 7, 2015
1 parent b2b4bb8 commit 400648d
Showing 14 changed files with 173 additions and 90 deletions.
37 changes: 27 additions & 10 deletions generators/app/index.js
Original file line number Diff line number Diff line change
@@ -4,10 +4,14 @@
// Yeoman bat app generator.
//

var generators = require( 'yeoman-generator' )
, yosay = require( 'yosay' )
var generators = require( 'yeoman-generator' )
, yosay = require( 'yosay' )
, youtil = require( './../../lib/youtil.js' )
, _ = require( 'lodash' )
;

var clean = require( 'underscore.string/clean' );

// Use different delimiters when our template itself is meant to be a template or template-like.
//
var tpl_tpl_settings =
@@ -34,38 +38,51 @@ module.exports = generators.Base.extend(
{
askSomeQuestions: function ()
{
/* jshint laxbreak: true */

var done = this.async();

// Have Yeoman greet the user.
//
this.log( yosay( 'Welcome to the BAT generator! (Backbone Application Template) \n Powered by Marviq' ) );
this.log( yosay(
'Welcome to the BAT generator! (Backbone Application Template)\n'
+ 'Powered by marviq'
));

// Ask the user for the webapp details
//
var prompts = [
{
name: 'packageName'
, message: 'What is the name of this webapp?'
, message: 'What is the package name of this webapp?'
, default: this.appname
, validate: youtil.isNpmName
}
, {
name: 'packageDescription'
, message: 'What is the purpose (description) of this webapp?'
, validate: youtil.isNonBlank
, filter: youtil.sentencify
}
, {
name: 'authorName'
, message: 'What is your name?'
, default: this.user.git.name()
, validate: youtil.isNonBlank
, filter: clean
}
, {
name: 'authorEmail'
, message: 'What is your email?'
, message: 'What is your email address?'
, default: this.user.git.email()
, validate: youtil.isNonBlank
, filter: _.trim
}
, {
name: 'authorUrl'
, message: 'If any, by what url would you like to be known?'
, message: 'If any, by what URL would you like to be known?'
, default: ''
, filter: _.trim
}
, {
name: 'multiLanguage'
@@ -94,13 +111,11 @@ module.exports = generators.Base.extend(
this.packageName = answers.packageName;
this.packageDescription = answers.packageDescription;
this.authorName = answers.authorName;
this.authorEmail = answers.authorEmail.trim();
this.authorUrl = answers.authorUrl.trim();
this.authorEmail = answers.authorEmail;
this.authorUrl = answers.authorUrl;
this.ie8 = answers.ie8;
this.demo = answers.demo;

this.i18n = answers.multiLanguage || answers.demo;

done();

}.bind( this )
@@ -110,6 +125,8 @@ module.exports = generators.Base.extend(

, configuring: function ()
{
this.i18n = this.multiLanguage || this.demo;

//
// Save a '.yo-rc.json' config file.
// At the very least this marks your project root for sub-generators.
4 changes: 2 additions & 2 deletions generators/app/templates/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<%= _.slugify(packageName) %>
<%= packageName %>
======================

## introduction
@@ -7,7 +7,7 @@

## installation
```bash
$ npm install <%= _.slugify(packageName) %> --save
$ npm install <%= packageName %> --save
```

## usage
2 changes: 1 addition & 1 deletion generators/app/templates/_package.json
Original file line number Diff line number Diff line change
@@ -22,7 +22,7 @@
}
],
"main": "dist/app/bundle.js",
"name": "<%= _.slugify(packageName) %>",
"name": "<%= packageName %>",
"private": true,
"scripts": {
"prepublish": "command -v grunt > /dev/null || { echo >&2 'It appears that \"grunt\" is not installed. Consider running \"npm install grunt --save-dev\" first.'; exit ; } && grunt",
2 changes: 1 addition & 1 deletion generators/app/templates/demo/_index.template.html
Original file line number Diff line number Diff line change
@@ -9,7 +9,7 @@
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>#- _.slugify(packageName) -#</title>
<title>#- packageName -#</title>
<meta name="description" content="#- packageDescription -#">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">

2 changes: 1 addition & 1 deletion generators/app/templates/src/_index.template.html
Original file line number Diff line number Diff line change
@@ -27,7 +27,7 @@
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>#- _.slugify(packageName) -#</title>
<title>#- packageName -#</title>
<meta name="description" content="#- packageDescription -#">

<link rel="stylesheet" type="text/css" href="<%- styleUrl %>" />#% if ( ie8 ) { %#<%
54 changes: 37 additions & 17 deletions generators/collection/index.js
Original file line number Diff line number Diff line change
@@ -4,12 +4,14 @@
// Yeoman bat:collection sub-generator.
//

var generators = require( 'yeoman-generator' )
, yosay = require( 'yosay' )
, varname = require( 'varname' )
, _ = require( 'lodash' )
var generators = require( 'yeoman-generator' )
, yosay = require( 'yosay' )
, youtil = require( './../../lib/youtil.js' )
, _ = require( 'lodash' )
;

var decapitalize = require( 'underscore.string/decapitalize' );

var CollectionGenerator = generators.Base.extend(
{
initializing: function ()
@@ -32,12 +34,18 @@ var CollectionGenerator = generators.Base.extend(
var prompts = [
{
name: 'collectionName'
, message: 'What\'s the name of this collection you so desire? ( use camelcasing! )'
, message: 'What is the name of this collection you so desire?'
, validate: youtil.isIdentifier
, filter: function ( value )
{
return decapitalize( _.trim( value ).replace( /collection$/i, '' ));
}
}
, {
name: 'description'
, message: 'What\'s the description for this collection?'
, default: 'No description'
, message: 'What is the purpose (description) of this collection?'
, validate: youtil.isNonBlank
, filter: youtil.sentencify
}
, {
type: 'confirm'
@@ -79,8 +87,13 @@ var CollectionGenerator = generators.Base.extend(
var prompts = [
{
name: 'modelName'
, message: 'Whats the model name for this collection ( use camelcasing! )'
, message: 'What is the model name for this collection?'
, default: modelName
, validate: youtil.isIdentifier
, filter: function ( value )
{
return decapitalize( _.trim( value ).replace( /model$/i, '' ));
}
}
, {
type: 'confirm'
@@ -104,20 +117,27 @@ var CollectionGenerator = generators.Base.extend(
}
}

, configuring: function ()
{
var collectionName = this.collectionName
, modelName = this.modelName
;

this.className = _.capitalize( collectionName ) + 'Collection';
this.fileBase = _.kebabCase( _.deburr( collectionName ));

this.modelClassName = _.capitalize( modelName ) + 'Model';
this.modelFileName = _.kebabCase( _.deburr( modelName )) + '.coffee';
}

, writing:
{
createCollection: function ()
{
// Create the needed variables
this.modelClass = this.modelName.charAt(0).toUpperCase() + this.modelName.slice(1);
this.className = this.collectionName.charAt(0).toUpperCase() + this.collectionName.slice(1);
this.modelFileName = varname.dash( this.modelName );
this.fileName = varname.dash( this.collectionName );

// Create the collection
this.template( 'collection.coffee', 'src/collections/' + this.fileName + '.coffee' );
// Create the collection
this.template( 'collection.coffee', 'src/collections/' + this.fileBase + '.coffee' );

// Create the model if needed
// Create the model if needed
if ( this.createModel === true )
{
this.invoke(
16 changes: 8 additions & 8 deletions generators/collection/templates/collection.coffee
Original file line number Diff line number Diff line change
@@ -2,17 +2,17 @@
if typeof exports is 'object'
module.exports = factory(
require( 'backbone' )
require( './../models/<%= modelFileName %>.coffee' )
require( './../models/<%= modelFileName %>' )
)
else if typeof define is 'function' and define.amd
define( [
'backbone'
'./../models/<%= modelFileName %>.coffee'
'./../models/<%= modelFileName %>'
], factory )

)((
Backbone
<%= modelClass %>Model
<%= modelClassName %>
) ->

###*
@@ -26,28 +26,28 @@
###*<% if ( description ) { %>
# <%= description %>
#<% } %>
# @class <%= className %>Collection
# @class <%= className %>
# @extends Backbone.Collection<% if ( singleton ) { %>
# @static<% } else { %>
# @constructor<% } %>
###

class <%= className %>Collection extends Backbone.Collection
class <%= className %> extends Backbone.Collection

###*
# @property model
#
# @default <%= modelClass %>Model
# @default <%= modelClassName %>
# @type Class
# @static
# @final
###

model: <%= modelClass %>Model<% if ( singleton ) { %>
model: <%= modelClassName %><% if ( singleton ) { %>


## Export singleton
##
return new <%= className %>Collection()<% } %>
return new <%= className %>()<% } %>

)
38 changes: 24 additions & 14 deletions generators/model/index.js
Original file line number Diff line number Diff line change
@@ -4,12 +4,14 @@
// Yeoman bat:model sub-generator.
//

var generators = require( 'yeoman-generator' )
, yosay = require( 'yosay' )
, varname = require( 'varname' )
, _ = require( 'lodash' )
var generators = require( 'yeoman-generator' )
, yosay = require( 'yosay' )
, youtil = require( './../../lib/youtil.js' )
, _ = require( 'lodash' )
;

var decapitalize = require( 'underscore.string/decapitalize' );

var ModelGenerator = generators.Base.extend(
{
constructor: function ( args, options )
@@ -59,12 +61,18 @@ var ModelGenerator = generators.Base.extend(
var prompts = [
{
name: 'modelName'
, message: 'What\'s the name of this model you so desire? ( use camelcasing! )'
, message: 'What is the name of this model you so desire?'
, validate: youtil.isIdentifier
, filter: function ( value )
{
return decapitalize( _.trim( value ).replace( /model$/i, '' ));
}
}
, {
name: 'description'
, message: 'What\'s the description for this model?'
, default: 'No description'
, message: 'What is the purpose (description) of this model?'
, validate: youtil.isNonBlank
, filter: youtil.sentencify
}
, {
type: 'confirm'
@@ -94,17 +102,19 @@ var ModelGenerator = generators.Base.extend(
}
}

, configuring: function ()
{
var modelName = this.modelName;

this.className = _.capitalize( modelName ) + 'Model';
this.fileBase = _.kebabCase( _.deburr( modelName ));
}

, writing:
{
createModel: function ()
{
this.fileName = varname.dash( this.modelName );

// Class names start with a capital by convention
//
this.className = this.modelName.charAt( 0 ).toUpperCase() + this.modelName.slice( 1 );

this.template( 'model.coffee', 'src/models/' + this.fileName + '.coffee' );
this.template( 'model.coffee', 'src/models/' + this.fileBase + '.coffee' );
}
}
}
6 changes: 3 additions & 3 deletions generators/model/templates/model.coffee
Original file line number Diff line number Diff line change
@@ -23,17 +23,17 @@
###*<% if ( description ) { %>
# <%= description %>
#<% } %>
# @class <%= className %>Model
# @class <%= className %>
# @extends Backbone.Model<% if ( singleton ) { %>
# @static<% } else { %>
# @constructor<% } %>
###

class <%= className %>Model extends Backbone.Model<% if ( singleton ) { %>
class <%= className %> extends Backbone.Model<% if ( singleton ) { %>


## Export singleton
##
return new <%= className %>Model()<% } %>
return new <%= className %>()<% } %>

)
Loading

0 comments on commit 400648d

Please sign in to comment.