forked from johannesjo/generator-angular-auto-admin-loopback
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sub-generator-base.js
292 lines (253 loc) · 9.9 KB
/
sub-generator-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
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
'use strict';
var path = require('path');
var yeoman = require('yeoman-generator');
var defaultSettings = require('./default-settings.js');
var helper = require('./helper.js');
var chalk = require('chalk');
var fs = require('fs');
var _s = require('underscore.string');
var _ = require('lodash');
module.exports = yeoman.Base.extend({
constructor: function() {
// super constructor needs to be called manually
// as the constructor-function is overwritten by this
yeoman.Base.apply(this, arguments);
this.argument('name', {type: String, required: true});
// set all the different name versions to be used in the templates
this.setModuleNames(this.name);
// set app name
this.setAppVariables();
// set sources root (for file-templates)
var sourceRoot = '/templates/app';
this.sourceRoot(path.join(__dirname, sourceRoot));
// additional variables
this.createdFiles = [];
},
/**
* parent initialize function, inherited by sub-gens
*/
init: function() {
this.mergeConfig();
this.overWriteTplPathIfSet();
// injection & template vars
// set current subgen
this.subGenCfg = this.subGenerators[this.templateName];
// the state name is the argument
this.stateName = this.name + this.subGenCfg.stateSuffix;
// template vars
this.modelServiceName = _s.classify(this.options.model.name);
this.modelName = this.options.model.name;
this.modelOptions = helper.simpleObjectToString(this.options.model, ' ');
this.overviewStateFull = this.baseState + this.name + this.subGenerators.overview.stateSuffix;
this.editStateFull = this.baseState + this.name + this.subGenerators.edit.stateSuffix;
this.viewStateFull = this.baseState + this.name + this.subGenerators.view.stateSuffix;
// instead use target folder to set the path
this.targetFolder = path.join(this.dirs.routes);
// names need to be reset
this.setModuleNames(this.name);
// create overview files
this.generateSourceAndTest(this.templateName);
},
/**
* helper function to merge default settings with the ones
* provided by the .yo-rc.json
*/
mergeConfig: function() {
// create a clone to avoid testing issues
var defaultCfg = _.cloneDeep(defaultSettings);
_.merge(defaultCfg, this.config.getAll());
_.merge(this, defaultCfg);
},
/**
* allows overwriting of the template path of the generator
* so users can specifiy their own template path
*/
overWriteTplPathIfSet: function() {
if (this.customTemplatesPath) {
if (fs.existsSync(this.customTemplatesPath)) {
this.sourceRoot(this.customTemplatesPath);
} else {
throw (new Error('custom template path ' + this.customTemplatesPath + ' does not exist. Check your .yo-rc.json.'));
}
}
},
/**
* set the app variables, e.g. the name of the app in the
* different required formats such as camelized or slugified
*/
setAppVariables: function() {
this.moduleName = this.config.get('moduleName') || 'aaal';
},
/**
* sets all the different name versions to be used in the templates
* @param name{string}
*/
setModuleNames: function(name) {
this.cameledName = _s.camelize(name);
this.classedName = _s.classify(name);
this.sluggedName = _s.slugify(name);
this.dashedName = _s.dasherize(name);
this.humanizedName = _s.humanize(name);
},
/**
* helper function to get rid of potential parent paths
* @param path{string}
* @returns {string}
*/
stripModuleAndAppPaths: function(path) {
path = path
.replace(this.dirs.basePath, '')
.replace(this.dirs.app, '');
return path;
},
/**
* helper function to format the path names to the preferred
* style as configured in the .yo-rc.json
* @param name{string}
* @returns {string}
*/
formatNamePath: function(name) {
var style = this.config.get('pathOutputStyle') || 'dasherize';
// first letter to lowercase to prevent issues with dasherize
name = name.charAt(0)
.toLowerCase() + name.slice(1);
return _s[style](name);
},
/**
*
* @returns {string}
*/
defineTargetFolder: function() {
var realTargetFolder;
// allow creating sub-modules via reading and parsing the path argument
if (this.targetFolder) {
this.targetFolder = this.stripModuleAndAppPaths(this.targetFolder);
realTargetFolder = path.join(this.targetFolder);
} else {
if (this.curGenCfg.globalDir) {
realTargetFolder = this.curGenCfg.globalDir;
} else {
realTargetFolder = '.'
}
}
// check if a same named parent directory should be created
// for directives and routes
if (this.curGenCfg.createDirectory && !this.options.noParentFolder) {
realTargetFolder = path.join(realTargetFolder, this.formatNamePath(this.name));
}
return realTargetFolder;
},
/**
* main file-creation pipeline function
* @param templateName
*/
generateSourceAndTest: function(templateName) {
this.templateName = templateName;
this.curGenCfg = this.subGenerators[templateName];
var realTargetFolder = this.defineTargetFolder();
var filesToCreate = [];
// create file paths
var inAppPath = path.join(this.dirs.basePath, realTargetFolder);
var generatorTargetPath = path.join(this.dirs.app, inAppPath);
var standardFileName = (this.curGenCfg.prefix || '') + this.formatNamePath(this.name) + (this.curGenCfg.suffix || '');
// prepare template template and data
this.tplUrl = path.join(inAppPath, standardFileName + this.fileExt.tpl)
// windows fix for url path
.replace(/\\/g, '/');
filesToCreate.push({
tpl: this.templateName + this.fileExt.tpl,
targetFileName: standardFileName + this.fileExt.tpl
});
if (this.addStyleFiles === true) {
filesToCreate.push({
tpl: this.stylePrefix + this.templateName + this.fileExt.style,
targetFileName: this.stylePrefix + standardFileName + this.fileExt.style
});
}
// add main file to queue
filesToCreate.push({
tpl: templateName + this.fileExt.script,
targetFileName: standardFileName + this.fileExt.script
});
// add test file to queue
if (this.addTestFiles === true) {
filesToCreate.push({
tpl: templateName + this.testSuffix + this.fileExt.script,
targetFileName: standardFileName + this.testSuffix + this.fileExt.script
});
}
// create files and create a files array for further use
for (var i = 0; i < filesToCreate.length; i++) {
var fileToCreate = filesToCreate[i];
var outputFile = path.join(generatorTargetPath, fileToCreate.targetFileName);
var customYoRcTpl = this.getCustomTplFromYoRc(fileToCreate);
// add to created files array
this.createdFiles.push(outputFile);
// set name suffix accordingly to template
if (fileToCreate.gen) {
this.nameSuffix = fileToCreate.gen.nameSuffix || '';
} else {
this.nameSuffix = this.curGenCfg.nameSuffix || '';
}
if (customYoRcTpl) {
this.writeCustomYoRcTpl(customYoRcTpl, outputFile);
} else {
this.fs.copyTpl(
this.templatePath(fileToCreate.tpl),
this.destinationPath(outputFile),
this
);
}
}
this.afterFileCreationHook();
},
/**
* checks for custom templates defined in the .yo-rc.json
* if set they will be used instead of the default ones
* @param fileToCreate
* @returns {string}
*/
getCustomTplFromYoRc: function(fileToCreate) {
var customYoRcTpl;
var curGenCfg = null;
var SPEC_REG_EX = new RegExp(this.testSuffix + '\\' + this.fileExt.script + '$');
var SCRIPTS_REG_EX = new RegExp(this.fileExt.script + '$');
var TPL_REG_EX = new RegExp(this.fileExt.tpl + '$');
var STYLE_REG_EX = new RegExp(this.fileExt.style + '$');
if (fileToCreate.gen) {
curGenCfg = this.subGenerators[fileToCreate.gen];
} else {
curGenCfg = this.curGenCfg;
}
// WHY is this necessary??
if (curGenCfg.tpl) {
if (fileToCreate.tpl.match(SPEC_REG_EX)) {
customYoRcTpl = curGenCfg.tpl['spec'];
} else if (fileToCreate.tpl.match(SCRIPTS_REG_EX)) {
customYoRcTpl = curGenCfg.tpl['script'];
} else if (fileToCreate.tpl.match(TPL_REG_EX)) {
customYoRcTpl = curGenCfg.tpl['tpl'];
} else if (fileToCreate.tpl.match(STYLE_REG_EX)) {
customYoRcTpl = curGenCfg.tpl['style'];
}
if (customYoRcTpl && typeof customYoRcTpl === 'string') {
return customYoRcTpl;
}
}
},
/**
* helper function used to create files by using the custom
* template string and underscores templating language
* @param customYoRcTpl{string}
* @param targetDir{string}
*/
writeCustomYoRcTpl: function(customYoRcTpl, targetDir) {
var tpl = _.template(customYoRcTpl, {})(this);
this.fs.write(targetDir, tpl);
},
/**
* things done after all files are created
*/
afterFileCreationHook: function() {
}
});