Skip to content

Commit bb421e9

Browse files
committed
refacto index.js & format.js & add node tests
1 parent 43d954a commit bb421e9

31 files changed

+1582
-498
lines changed

app/advanced-prompts.json

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
[
2+
{
3+
"type": "checkbox",
4+
"name": "advancedFeatures",
5+
"message": "Do you want include these features?",
6+
"choices": [
7+
{
8+
"value": "modernizr",
9+
"name": "Modernizr: the feature detection library for HTML5/CSS3",
10+
"checked": false
11+
},
12+
{
13+
"value": "imagemin",
14+
"name": "gulp-imagemin: minify PNG, JPEG, GIF and SVG images with imagemin",
15+
"checked": false
16+
},
17+
{
18+
"value": "qrcode",
19+
"name": "QRCode: display a qrcode in terminal with 'gulp serve'",
20+
"checked": false
21+
}
22+
]
23+
}
24+
]

app/index.js

Lines changed: 13 additions & 166 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,6 @@ var yeoman = require('yeoman-generator');
55
var yosay = require('yosay');
66
var chalk = require('chalk');
77

8-
var prompts = require('./prompts.json');
9-
var options = require('./options.json');
10-
var utils = require('./src/utils.js');
11-
128
var GulpAngularGenerator = yeoman.generators.Base.extend({
139

1410
constructor: function () {
@@ -20,15 +16,7 @@ var GulpAngularGenerator = yeoman.generators.Base.extend({
2016
required: false
2117
});
2218

23-
// Define options
24-
options.forEach(function(option) {
25-
this.option(option.name, {
26-
type: global[option.type],
27-
required: option.required,
28-
desc: option.desc,
29-
defaults: option.defaults
30-
});
31-
}.bind(this));
19+
this.props = {};
3220
},
3321

3422
info: function () {
@@ -38,162 +26,21 @@ var GulpAngularGenerator = yeoman.generators.Base.extend({
3826
chalk.yellow('You\'re using the fantastic generator for scaffolding an application with Angular and Gulp!')
3927
));
4028
}
29+
}
4130

42-
if (this.options['default']) {
43-
var mockPrompts = require('./src/mock-prompts.js');
44-
var mockOptions = require('./src/mock-options.js');
45-
var savableOptionsDefaults = this._.filter(mockOptions.defaults, function(value, name) {
46-
return this._.find(options, { name: name }).save;
47-
}.bind(this));
48-
this.props = {
49-
paths: {
50-
src: mockOptions.defaults['app-path'],
51-
dist: mockOptions.defaults['dist-path'],
52-
e2e: mockOptions.defaults['e2e-path'],
53-
tmp: mockOptions.defaults['tmp-path']
54-
}
55-
}
56-
this.config.set('props', this._.merge(this.props, mockPrompts.defaults));
57-
58-
this.log('__________________________');
59-
this.log('You use ' + chalk.green('--default') + ' option:');
60-
this.log('\t* angular 1.3.x\n\t* ngAnimate\n\t* ngCookies\n\t* ngTouch\n\t* ngSanitize\n\t* jQuery 1.x.x\n\t* ngResource\n\t* ngRoute\n\t* bootstrap\n\t* ui-bootstrap\n\t* node-sass');
61-
this.log('\n\t* --app-path=\'src\'\n\t* --dist-path=\'dist\'\n\t* --e2e-path=\'e2e\'\n\t* --tmp-path=\'.tmp\'');
62-
this.log('__________________________\n');
63-
}
64-
},
65-
66-
checkYoRc: function() {
67-
var cb = this.async();
68-
69-
if(this.config.get('props') && !this.options['default']) {
70-
this.prompt([{
71-
type: 'confirm',
72-
name: 'skipConfig',
73-
message: 'Existing ' + chalk.green('.yo-rc') + ' configuration found, would you like to use it?',
74-
default: true,
75-
}], function (answers) {
76-
this.skipConfig = answers.skipConfig;
77-
78-
cb();
79-
}.bind(this));
80-
} else {
81-
cb();
82-
}
83-
},
84-
85-
retrieveOptions: function() {
86-
if (this.skipConfig || this.options['default']) {
87-
return;
88-
}
89-
90-
['app-path', 'dist-path', 'e2e-path', 'tmp-path'].forEach(function (name) {
91-
if (utils.isAbsolutePath(this.options[name]))
92-
this.env.error(name + ' must be a relative path');
93-
this.options[name] = utils.normalizePath(this.options[name]);
94-
}.bind(this));
95-
96-
this.props = {
97-
paths: {
98-
src: this.options['app-path'],
99-
dist: this.options['dist-path'],
100-
e2e: this.options['e2e-path'],
101-
tmp: this.options['tmp-path']
102-
}
103-
}
104-
},
105-
106-
askQuestions: function () {
107-
if (this.skipConfig || this.options['default']) {
108-
return ;
109-
}
110-
111-
var done = this.async();
112-
113-
this._.findWhere(prompts, {name: 'bootstrapComponents'}).when = function(props) {
114-
return props.ui.key === 'bootstrap';
115-
};
116-
117-
this._.findWhere(prompts, {name: 'foundationComponents'}).when = function(props) {
118-
return props.ui.key === 'foundation';
119-
};
120-
121-
this.prompt(prompts, function (props) {
122-
if(props.ui.key !== 'bootstrap') {
123-
props.bootstrapComponents = {
124-
name: null,
125-
version: null,
126-
key: null,
127-
module: null
128-
};
129-
}
130-
131-
if(props.ui.key !== 'foundation') {
132-
props.foundationComponents = {
133-
name: null,
134-
version: null,
135-
key: null,
136-
module: null
137-
};
138-
}
139-
140-
this.props = this._.merge(this.props, props);
141-
this.config.set('props', this.props);
142-
143-
done();
144-
}.bind(this));
145-
},
146-
147-
askAdvancedQuestions: function () {
148-
if (this.skipConfig || !this.options['advanced']) {
149-
return ;
150-
}
151-
152-
var done = this.async();
153-
154-
this.prompt({
155-
"type": "checkbox",
156-
"name": "advancedFeatures",
157-
"message": "Do you want include these features?",
158-
"choices": [
159-
{
160-
"value": "modernizr",
161-
"name": "Modernizr: the feature detection library for HTML5/CSS3",
162-
"checked": false
163-
},
164-
{
165-
"value": "imagemin",
166-
"name": "gulp-imagemin: minify PNG, JPEG, GIF and SVG images with imagemin",
167-
"checked": false
168-
},
169-
{
170-
"value": "qrcode",
171-
"name": "QRCode: display a qrcode in terminal with 'gulp serve'",
172-
"checked": false
173-
}
174-
]
175-
}, function (props) {
176-
177-
this.props.advancedFeatures = props.advancedFeatures;
178-
this.config.set('props', this.props);
179-
180-
done();
181-
}.bind(this));
182-
},
31+
});
18332

184-
// Format props to template values
185-
formatProps: require('./src/format'),
33+
require('./src/options')(GulpAngularGenerator);
34+
require('./src/prompts')(GulpAngularGenerator);
35+
require('./src/paths')(GulpAngularGenerator);
36+
require('./src/files')(GulpAngularGenerator);
18637

187-
// Write files (copy, template)
188-
writeFiles: require('./src/write'),
38+
require('./src/modules')(GulpAngularGenerator);
39+
require('./src/techs')(GulpAngularGenerator);
40+
require('./src/ui')(GulpAngularGenerator);
41+
require('./src/router')(GulpAngularGenerator);
42+
require('./src/preprocessors')(GulpAngularGenerator);
18943

190-
// Install dependencies
191-
install: function () {
192-
this.installDependencies({
193-
skipInstall: this.options['skip-install'],
194-
skipMessage: this.options['skip-message']
195-
});
196-
}
197-
});
44+
require('./src/write')(GulpAngularGenerator);
19845

19946
module.exports = GulpAngularGenerator;

app/options.json

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,71 +4,62 @@
44
"type": "String",
55
"required": false,
66
"desc": "Angular app folder, relative to cwd",
7-
"defaults": "src",
8-
"save": true
7+
"defaults": "src"
98
},
109
{
1110
"name" : "dist-path",
1211
"type": "String",
1312
"required": false,
1413
"desc": "Build destination folder, relative to cwd",
15-
"defaults": "dist",
16-
"save": true
14+
"defaults": "dist"
1715
},
1816
{
1917
"name": "e2e-path",
2018
"type": "String",
2119
"required": false,
2220
"desc": "e2e test specs folder, relative to cwd",
23-
"defaults": "e2e",
24-
"save": true
21+
"defaults": "e2e"
2522
},
2623
{
2724
"name": "tmp-path",
2825
"type": "String",
2926
"required": false,
3027
"desc": "temp build folder serving preprocessed files, relative to cwd",
31-
"defaults": ".tmp",
32-
"save": true
28+
"defaults": ".tmp"
3329
},
3430
{
3531
"name": "skip-install",
3632
"type": "Boolean",
3733
"required": false,
3834
"desc": "skip npm install and bower install after project generated",
39-
"defaults": false,
40-
"save": false
35+
"defaults": false
4136
},
4237
{
4338
"name": "skip-welcome-message",
4439
"type": "Boolean",
4540
"required": false,
4641
"desc": "skip yo welcome message",
47-
"defaults": false,
48-
"save": false
42+
"defaults": false
4943
},
5044
{
5145
"name": "skip-message",
5246
"type": "Boolean",
5347
"required": false,
5448
"desc": "skip install message",
55-
"defaults": false,
56-
"save": false
49+
"defaults": false
5750
},
5851
{
5952
"name": "default",
6053
"type": "Boolean",
6154
"required": false,
6255
"desc": "use default configurations",
63-
"defaults": false,
64-
"save": false
56+
"defaults": false
6557
},
6658
{
6759
"name": "advanced",
6860
"type": "Boolean",
6961
"required": false,
7062
"desc": "prompt for advanced additional features",
71-
"defaults": false,
72-
"save": true
63+
"defaults": false
7364
}
7465
]

app/src/files.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
'use strict';
2+
3+
var path = require('path');
4+
5+
var files = require('../files.json');
6+
7+
function resolvePaths(template) {
8+
return function(file) {
9+
var src = file, dest = file;
10+
11+
if(template) {
12+
var basename = path.basename(file);
13+
src = file.replace(basename, '_' + basename);
14+
}
15+
16+
if(src.match(/\.js$/)) {
17+
var preprocessorFile = this.sourceRoot() + '/' + src.replace(/\.js$/, '.' + this.props.jsPreprocessor.srcExtension);
18+
if(this.fs.exists(preprocessorFile)) {
19+
src = src.replace(/\.js$/, '.' + this.props.jsPreprocessor.srcExtension);
20+
dest = dest.replace(/\.js$/, '.' + this.props.jsPreprocessor.extension);
21+
}
22+
}
23+
24+
return {
25+
src: src,
26+
dest: dest,
27+
template: template
28+
};
29+
};
30+
}
31+
32+
module.exports = function(GulpAngularGenerator) {
33+
34+
GulpAngularGenerator.prototype.prepareFiles = function prepareFiles() {
35+
36+
this.files = []
37+
.concat(files.staticFiles.map(resolvePaths(false), this))
38+
.concat(files.templates.map(resolvePaths(true), this));
39+
40+
};
41+
42+
};

0 commit comments

Comments
 (0)