forked from mazerte/generator-footguard
-
Notifications
You must be signed in to change notification settings - Fork 1
/
footguard-base.js
137 lines (114 loc) · 3.53 KB
/
footguard-base.js
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
'use strict';
var path = require('path'),
util = require('util'),
grunt = require('grunt'),
yeoman = require('yeoman-generator'),
generatorUtil = require('./util.js');
grunt.util._.mixin( require('underscore.inflections') );
var Generator = function Generator() {
yeoman.generators.NamedBase.apply(this, arguments);
this.appname = path.basename(process.cwd());
this.argument('folder', { type: String, required: false, defaults: '' });
this.sourceRoot(path.join(__dirname, './templates'));
this.options['skip-install'] = true;
};
util.inherits(Generator, yeoman.generators.NamedBase);
module.exports = Generator;
/////////////
// PROMPTS //
/////////////
Generator.prototype.promptForTest = function promptForTest() {
return [{
type: 'confirm',
name: 'test',
message: 'Would you like to create associate unit test ?',
default: true
}];
};
Generator.prototype.promptForModel = function promptForModel(name) {
return this._promptForNamed('model', 'model', name);
};
Generator.prototype.promptForTemplate = function promptForTemplate(name) {
return this._promptForNamed('tpl', 'template', name);
};
Generator.prototype.promptForSass = function promptForSass(name) {
return this._promptForNamed('sass', 'sass file', name);
};
Generator.prototype._promptForNamed = function _promptForNamed(type, typeName, name) {
return [{
type: 'confirm',
name: type,
message: 'Would you like to create associate ' + typeName + ' ?',
default: true
}, {
type: 'input',
name: type + 'Name',
message: 'What name would you like ?',
default: name,
when: function( answers ) {
return answers[type];
}
}];
};
Generator.prototype.parsePromptsResult = function parsePromptsResult(callback) {
var _this = this;
return function(answers) {
_this._parsePromptForName('model', answers);
_this._parsePromptForName('tpl', answers);
_this._parsePromptForName('sass', answers);
_this.test = answers.test;
callback(answers);
};
};
Generator.prototype._parsePromptForName = function _parsePromptForName(name, answers) {
if (answers[name] !== undefined) {
this[name] = answers[name];
if (this[name]) {
this[name] = answers[name + 'Name'];
}
}
};
//////////////////
// DESTINATIONS //
//////////////////
Generator.prototype.getElementDest = function getElementDest(type) {
return path.join(
'src/coffee/app/' + grunt.util._.pluralize(type),
this.folder,
this.name + '_' + type + '.coffee'
);
};
////////////
// CREATE //
////////////
Generator.prototype.createElementTest = function createElementTest(type) {
if( this.test ) {
var folder = grunt.util._.pluralize(type);
var dest = path.join(folder, this.folder, this.name + '_' + type);
this.createTest('unit', type + '_spec.coffee', dest);
}
};
Generator.prototype.createTest = function createTest(type, template, file) {
var dest = path.join('src/coffee/spec/', type, file + '_spec.coffee');
this.template(template, dest);
generatorUtil.rewriteFile({
file: 'src/coffee/spec/' + type + '/all_' + type + '_tests.coffee',
needle: '# <" + type + "> don\'t remove this comment',
splicable: [
' "' + path.join('spec/', type, file) + '_spec"'
]
});
};
Generator.prototype.createModel = function createModel(name, folder, test) {
name = name || this.name;
folder = folder || this.folder;
test = test || this.test;
if( this.model ) {
this.env.run(['footguard:model', name, folder], {
skipPrompt: true,
args: {
test: test
}
});
}
};