Skip to content

Commit

Permalink
Adding the module generator.
Browse files Browse the repository at this point in the history
  • Loading branch information
thomaswmanion committed Apr 26, 2015
1 parent ed0b5c4 commit 4c2db52
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 0 deletions.
1 change: 1 addition & 0 deletions module/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('./module-generator');
74 changes: 74 additions & 0 deletions module/module-generator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
'use strict';
var yeoman = require('yeoman-generator');
var chalk = require('chalk');
var yosay = require('yosay');
var path = require('path');
var fs = require('fs');
var naming = require('../util/naming');
var moduleUtil = require('./module-util');
var gen;
module.exports = yeoman.generators.Base.extend({
initializing: function () {
this.pkg = require('../package.json');
gen = this;
},

promptiing: function() {
var done = this.async();

// Have Yeoman greet the user.
this.log(yosay('Time to scaffold a ' + chalk.red('module') + '!'));

var prompts = [{
type: 'input',
name: 'moduleName',
message: 'What would you like the name of your module to be?',
validate: function(moduleName) {
if (!moduleName || !moduleName.trim()) {
return false;
}
gen.moduleName = moduleName;
return true;
}
}, {
when: function(props) {
gen.defaultUrl = '/' + naming.toDasherized(props.moduleName);
return true;
},
type: 'input',
name: 'url',
message: 'What do you want the URL to be?',
default: function() {
return gen.defaultUrl;
}
}, {
when: function(props) {
gen.defaultPath = moduleUtil.getDefaultModuleFolder(props.moduleName);
return true;
},
type: 'input',
name: 'path',
message: 'Where would you like this module to be placed?',
default: function() {
return gen.defaultPath;
}
}];

this.prompt(prompts, function (props) {
this.props = props;
this.props.dasherizedModuleName = naming.toDasherized(this.props.moduleName);
this.props.lowerCamelCaseModuleName = naming.toLowerCamelCase(this.props.moduleName);
var moduleFilename = this.props.dasherizedModuleName + '.module.js';
this.props.fullPath = path.join('src', 'scripts', this.props.path, moduleFilename);

done();
}.bind(this));
},

writing: {
module: function() {
this.template(this.templatePath('full.module.js'), this.destinationPath(this.props.fullPath), this.props);
}
}

});
6 changes: 6 additions & 0 deletions module/module-util.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
var moduleUtil = module.exports = {};
var naming = require('../util/naming');

moduleUtil.getDefaultModuleFolder = function(moduleName) {
return naming.toDasherized(moduleName) + '/';
};
16 changes: 16 additions & 0 deletions module/templates/full.module.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
var m = require('mithril');
var routes = require('routes');

var <%= lowerCamelCaseModuleName %> = {};
<%= lowerCamelCaseModuleName %>.controller = function() {
this.goHome = function() {
m.route('/');
};
};
<%= lowerCamelCaseModuleName %>.view = function(vm) {
return m('div', [
m('p', '<%= moduleName %>'),
]);
};

routes['<%= url %>'] = <%= lowerCamelCaseModuleName %>;

0 comments on commit 4c2db52

Please sign in to comment.