Skip to content

Commit 4c2db52

Browse files
committed
Adding the module generator.
1 parent ed0b5c4 commit 4c2db52

File tree

4 files changed

+97
-0
lines changed

4 files changed

+97
-0
lines changed

module/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = require('./module-generator');

module/module-generator.js

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
'use strict';
2+
var yeoman = require('yeoman-generator');
3+
var chalk = require('chalk');
4+
var yosay = require('yosay');
5+
var path = require('path');
6+
var fs = require('fs');
7+
var naming = require('../util/naming');
8+
var moduleUtil = require('./module-util');
9+
var gen;
10+
module.exports = yeoman.generators.Base.extend({
11+
initializing: function () {
12+
this.pkg = require('../package.json');
13+
gen = this;
14+
},
15+
16+
promptiing: function() {
17+
var done = this.async();
18+
19+
// Have Yeoman greet the user.
20+
this.log(yosay('Time to scaffold a ' + chalk.red('module') + '!'));
21+
22+
var prompts = [{
23+
type: 'input',
24+
name: 'moduleName',
25+
message: 'What would you like the name of your module to be?',
26+
validate: function(moduleName) {
27+
if (!moduleName || !moduleName.trim()) {
28+
return false;
29+
}
30+
gen.moduleName = moduleName;
31+
return true;
32+
}
33+
}, {
34+
when: function(props) {
35+
gen.defaultUrl = '/' + naming.toDasherized(props.moduleName);
36+
return true;
37+
},
38+
type: 'input',
39+
name: 'url',
40+
message: 'What do you want the URL to be?',
41+
default: function() {
42+
return gen.defaultUrl;
43+
}
44+
}, {
45+
when: function(props) {
46+
gen.defaultPath = moduleUtil.getDefaultModuleFolder(props.moduleName);
47+
return true;
48+
},
49+
type: 'input',
50+
name: 'path',
51+
message: 'Where would you like this module to be placed?',
52+
default: function() {
53+
return gen.defaultPath;
54+
}
55+
}];
56+
57+
this.prompt(prompts, function (props) {
58+
this.props = props;
59+
this.props.dasherizedModuleName = naming.toDasherized(this.props.moduleName);
60+
this.props.lowerCamelCaseModuleName = naming.toLowerCamelCase(this.props.moduleName);
61+
var moduleFilename = this.props.dasherizedModuleName + '.module.js';
62+
this.props.fullPath = path.join('src', 'scripts', this.props.path, moduleFilename);
63+
64+
done();
65+
}.bind(this));
66+
},
67+
68+
writing: {
69+
module: function() {
70+
this.template(this.templatePath('full.module.js'), this.destinationPath(this.props.fullPath), this.props);
71+
}
72+
}
73+
74+
});

module/module-util.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
var moduleUtil = module.exports = {};
2+
var naming = require('../util/naming');
3+
4+
moduleUtil.getDefaultModuleFolder = function(moduleName) {
5+
return naming.toDasherized(moduleName) + '/';
6+
};

module/templates/full.module.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
var m = require('mithril');
2+
var routes = require('routes');
3+
4+
var <%= lowerCamelCaseModuleName %> = {};
5+
<%= lowerCamelCaseModuleName %>.controller = function() {
6+
this.goHome = function() {
7+
m.route('/');
8+
};
9+
};
10+
<%= lowerCamelCaseModuleName %>.view = function(vm) {
11+
return m('div', [
12+
m('p', '<%= moduleName %>'),
13+
]);
14+
};
15+
16+
routes['<%= url %>'] = <%= lowerCamelCaseModuleName %>;

0 commit comments

Comments
 (0)