-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbase-public.js
More file actions
315 lines (281 loc) · 10.3 KB
/
base-public.js
File metadata and controls
315 lines (281 loc) · 10.3 KB
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
var path = require('path');
var Generator = require('yeoman-generator');
var _ = require('underscore.string');
var fs = require('fs');
module.exports = class extends Generator {
/**
* prepare basic stuff for sub generators
*
* @param args
* @param options
*/
constructor (args, options) {
super(...arguments);
Object.assign(this, {
publicDir: './', // public dir of the frontend app parts
srcDir: './src', // soruce dir, contains all ng modules created by generator
module: {
name: '', // angular module name
nameCamel: '', // camelcse version of name, ideal for js class name
label: '', // module human name
dir: '', // module directory name
path: '', // full path to module dir
},
file: {
name: '', // dasshed name of the file, includes module name and all path
label: '', // label is human redable title version of name
nameLowCamel: '', // camelcase with firs lower letter, ideal for dirctive name
nameCamel: '', // camecase of the name, ideal for controller or js clsass name
dir: '',
dirParts: ''
},
moduleRequired: true, // is name argument required
nameRequired: true, // is name argument required
nameDescription: 'in most cases it\'s file name you want to generate', // give the halp description of the name argument
nameSuffix: '', // suffix for name attribute
fileSuffix: '', // suffix for name file
fileSubDir: '', // suffix for name file
/**
* remove one last level of dir for the file,
* example: when set to true
* user/login/form
* will be
* user/login/user-login-form.js
* otherwise
* user/login/form/user-login-form.js
*/
fileSliceDir: true,
})
this._ = _;
if(!this.config.get('publicDir')) {
this.error("you have to init your app with yo angular-flow command, or make sure you run commands at root level of your project");
}
// config props
this.publicDir = this.config.get('publicDir') || this.publicDir;
this.srcDir = path.join(this.publicDir, this.config.get('srcDir'));
// module name is required
this.argument('moduleName', { type: String, required: false });
// subGenerator name is optional, depends on this.requiredName prop, witch you can be overwrite
this.argument('fileName', { type: String, required: false });
this.moduleName = this.options.moduleName;
this.fileName = this.options.fileName;
if(!this.moduleName) {
this.log.error('Module name is required, try: yo:'+this.options.namespace+' [moduleName]');
this.log('available modules: ', this.getModules());
this.exit();
}
if(this.nameRequired && !this.fileName) {
this.log.error('Name is required, try: yo:'+this.options.namespace+' [moduleName] [fileName]');
this.log(this.nameDescription);
this.exit();
}
// set module and file properties based on user input
this.setModule(this.moduleName);
this.setName(this.fileName, this.nameSuffix);
this.setFile(this.fileName, this.fileSuffix);
// console.log("MODULE\n", this.module);
// console.log("NAME\n", this.name);
// console.log("FILE\n", this.file);
// test if module exists
var moduleFile = this.module.path;
// if we createin new module, tyr if it already exists
if((this.options.namespace === 'angular-flow:module') && fs.existsSync(moduleFile)) {
this.error('Module "'+this.module.dir+'" already exist, can\'t overwrite it');
}
// if you create somethink for the module, test if it exists
if((this.options.namespace !== 'angular-flow:module') && !fs.existsSync(moduleFile)) {
this.error('Module "'+this.module.dir+'" does not exist, create it first with angular-flow:module [moduleName]' );
}
}
/**
* sets this
*
* @param name
*/
setModule (name) {
var dir = _.slugify(_.humanize(name));
var name = _.camelize(dir, true);
var nameCamel = _.classify(dir);
var label = _.humanize(dir);
this.module = {
name: name,
dir: dir,
path: path.join(this.srcDir,dir).replace(/\\/g, '/'),
label: label,
nameCamel: nameCamel
}
}
setFile(name, sufix) {
name = name||'';
sufix = sufix||'';
this.file = this.normalizeName(name.replace(/\//g, '-')+sufix);
// console.log('FILE', this.file)
this.file.dirParts = name.split('/').map(function(n){
return _.slugify(_.humanize(n));
}.bind(this));
this.file.dir = this.file.dirParts.join('/');
}
setName(name, sufix) {
name = name||'';
sufix = sufix||'';
this.name = this.normalizeName(name.replace(/\//g, '-')+sufix);
this.name.dirParts = name.split('/').map(function(n){
return _.slugify(_.humanize(n));
}.bind(this));
this.name.dir = this.name.dirParts.join('/');
}
/**
* return list of created modules names
*
* @returns []
*/
getModules() {
if(!this.modules) {
var baseDir = './src';//todo get this from config
this.modules = fs.readdirSync(baseDir).filter(function(file) {
if(fs.statSync(path.join(baseDir, file)).isDirectory()) {
return true;
} else {
return false;
}
});
}
return this.modules;
}
/**
* converts user input string to normalized file/module names
*
* @param name
*/
normalizeName (str) {
var dir = _.slugify(_.humanize(str));
var name = dir;
if(str.substr(-6)=='-modal') {
name = str.substr(0, str.length-6)+'.modal'
}
if(str.substr(-8)==='-service') {
name = str.substr(0, str.length-8)+'.service'
}
var label = _.humanize(dir);
var nameCamel = _.classify(dir);
var nameLowCamel = _.camelize(dir, true);
if(dir.substr(-10)==='-component') {
name = str.substr(0, str.length-10)+'.component'
label = _.humanize(dir);
nameCamel = _.classify(dir);
nameLowCamel = _.camelize(dir, true);
dir = str.substr(0, str.length-10)
}
return {
dir: dir,
name: name,
label: label,
nameLowCamel: nameLowCamel,
nameCamel: nameCamel
}
}
/**
* creates require() with provided file inside of main module file
* file has to have module directory as it's first part of the url
*
* @param file
*/
moduleAppendFile(file) {
var moduleFile = path.join(this.srcDir, this.module.dir, this.module.dir)+'-module.js';
var file = file.replace(/\\/g, '/');// no matter win or unix we use unix style
var str = "require('./"+file+"');";
var data = fs.readFileSync(moduleFile);
if(data.indexOf(str) < 0){
fs.appendFileSync(moduleFile, "\n"+str);
this.log.info(str, 'appended to', moduleFile);
} else {
this.log.info(str, 'already exists in', moduleFile);
}
}
/**
*
* @param templateFile template file from subgen template folders
* @param ext file extenasion (with dot included), to append to dest file
* @param appendToModule, append the file to module
*/
copyFileTemplate (templateFile, ext, appendToModule) {
var filePath = this.file.dirParts.join('/');
if(this.fileSliceDir) {
filePath = this.file.dirParts.slice(0,-1).join('/');
}
var file = path.join(this.fileSubDir,filePath, this.file.name);
var filePath = path.join(this.srcDir, this.module.dir, file);
this.fs.copyTpl(
this.templatePath(templateFile),
this.destinationPath(filePath+ext),
this
);
if(appendToModule) {
this.moduleAppendFile(file);
}
}
/**
* display error message
*
* @param msg
*/
error (msg, err) {
if(arguments.length==1) err = '';
this.log.error(msg, err);
this.log('');
this.exit();
}
/**
* exits process
*
* @param msg
*/
exit () {
process.exit(1);
}
/**
* get component config
* all | by name | by array of names
*
* @param name
* @returns {*}
*/
//getComponents: function(name){
//
// if(this.bower_components === null) {
// this.bower_components = [];
// if(fs.existsSync('bower-components.json')) {
// this.bower_components = JSON.parse(fs.readFileSync('bower-components.json'));
// }
//
// this.bower_components = this.bower_components.concat(JSON.parse(fs.readFileSync(__dirname+'/bower-components.json')));
// }
// // return list if no name given
// if(arguments.length === 0) {
// return this.bower_components;
// }
//
// // if array, its array of names, so convert it to array of configs
// if(_.isArray(name)) {
// var bower_components = this.bower_components;
// return name.map(function(component_name){
// var c = _.find(bower_components, 'name', component_name);
// if(!c) {
// this.error('bower component "'+component_name+'" not found!!!')
// }
// return c;
// })
// }
//
// // else its string, so return it's config
// if(!name) {
// this.error('bower component name is empty!!!')
// }
// var c = _.find(this.bower_components, 'name', name);
// if(!c) {
// this.error('bower component '+name+' not found!!!')
// }
// return c;
//
//},
};