1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
|
'use strict';
var util = require('util');
var path = require('path');
var yeoman = require('yeoman-generator');
function ExpressGenerator(args, options, config) {
yeoman.generators.Base.apply(this, arguments);
this.on('end', function () {
this.installDependencies({ skipInstall: options['skip-install'] });
});
this.pkg = JSON.parse(this.readFileAsString(path.join(__dirname, '../package.json')));
}
util.inherits(ExpressGenerator, yeoman.generators.Base);
ExpressGenerator.prototype.promptType = function promptType() {
// Short circuit if an option was explicitly specified
if (this.options.mvc || this.options.basic) {
return true;
}
var done = this.async();
var prompt = [{
type: 'list',
name: 'type',
message: 'Select a version to install:',
choices: [
'Basic',
'MVC'
]
}];
this.prompt(prompt, function (responses) {
this.options.mvc = responses.type.match(/^MVC$/i) !== null;
done();
}.bind(this));
};
ExpressGenerator.prototype.promptViewEngine = function () {
if (this.options.viewEngine) {
return true;
}
var done = this.async();
var prompt = [{
type: 'list',
name: 'viewEngine',
message: 'Select a view engine to use:',
choices: [
'Jade',
'EJS'
]
}];
this.prompt(prompt, function (response) {
this.options.viewEngine = response.viewEngine.toLowerCase();
done();
}.bind(this));
};
ExpressGenerator.prototype.promptCssPreprocessor = function () {
if (this.options.cssPreprocessor) {
return true;
}
var done = this.async();
var prompt = [{
type: 'list',
name: 'cssPreprocessor',
message: 'Select a css preprocessor to use (Sass Requires Ruby):' ,
choices: [
'None',
'Node-Sass',
'Sass',
'less'
]
}];
this.prompt(prompt, function (response) {
this.options.cssPreprocessor = response.cssPreprocessor.toLowerCase();
done();
}.bind(this));
};
ExpressGenerator.prototype.promptDatabase = function () {
if (this.options.database || !this.options.mvc) {
return true;
}
var done = this.async();
var prompt = [{
type: 'list',
name: 'database',
message: 'Select a database to use:',
choices: [
'None',
'MongoDB',
'MySQL',
'PostgreSQL'
]
}];
this.prompt(prompt, function (response) {
this.options.database = response.database.toLowerCase();
done();
}.bind(this));
};
ExpressGenerator.prototype.promptBuildTool = function () {
if (this.options.buildTool) {
return true;
}
var done = this.async();
var prompt = [{
type: 'list',
name: 'buildTool',
message: 'Select a build tool to use:',
choices: [
'Grunt',
'Gulp'
]
}];
this.prompt(prompt, function (response) {
this.options.buildTool = response.buildTool.toLowerCase();
done();
}.bind(this));
};
ExpressGenerator.prototype.buildEnv = function buildEnv() {
this.sourceRoot(path.join(__dirname, 'templates', 'common'));
this.expandFiles('**', { cwd: this.sourceRoot() }).map(function (file) {
this.template(file, file.replace(/^_/, ''));
}, this);
var name = this.options.mvc ? 'mvc' : 'basic';
var filetype = 'js';
if (this.options.coffee) {
name += '-coffee';
filetype = 'coffee';
}
this.sourceRoot(path.join(__dirname, 'templates', name));
this.directory('.', '.');
var views = this.options.viewEngine;
this.sourceRoot(path.join(__dirname, 'templates', 'views', views));
if (this.options.mvc) {
this.directory('.', 'app/views');
} else {
this.directory('.', 'views');
}
var stylesheets = this.options.cssPreprocessor;
if (stylesheets === 'sass') {
this.sourceRoot(path.join(__dirname, 'templates', 'css', 'sass'));
} else if (stylesheets === 'node-sass') {
this.sourceRoot(path.join(__dirname, 'templates', 'css', 'sass'));
} else if (stylesheets === 'less') {
this.sourceRoot(path.join(__dirname, 'templates', 'css', 'less'));
} else if (stylesheets === 'none') {
this.sourceRoot(path.join(__dirname, 'templates', 'css', 'css'));
}
this.directory('.', 'public/css');
if (this.options.database === 'mysql' || this.options.database === 'postgresql') {
this.copy(path.join(__dirname, 'templates', 'extras', name, 'model-index.' + filetype), 'app/models/index.' + filetype);
}
var buildFile = this.options.buildTool === 'grunt' ? 'Gruntfile.js' : 'gulpfile.js';
this.copy(path.join(__dirname, 'templates', 'extras', name, buildFile), buildFile);
};
ExpressGenerator.prototype.assetsDirs = function assetsDirs() {
this.mkdir('public');
this.mkdir('public/components');
this.mkdir('public/js');
this.mkdir('public/css');
this.mkdir('public/img');
};
module.exports = ExpressGenerator;
|