-
Notifications
You must be signed in to change notification settings - Fork 4
/
Gruntfile.js
391 lines (369 loc) · 14.2 KB
/
Gruntfile.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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
var path = require('path');
module.exports = function (grunt) {
// load all grunt tasks matching the `grunt-*` pattern
require('load-grunt-tasks')(grunt);
var appconf = {
dist: {
public: "./public/dist/scripts",
admin: "./public/dist/scripts/admin"
}
};
grunt.initConfig({
anyandgo: appconf,
jshint: {
options: {
reporter: require('jshint-stylish'),
// https://gist.github.com/haschek/2595796
jshintrc: './.jshintrc'
},
all: ['Grunfile.js', 'routes/*.js',
"models/*js", "forms/*js", 'test/**/*.js']
},
mochaTest: {
test: {
options: {
reporter: 'spec'
},
src: ['test/unit/**/*.js']
}
},
watch: {
scripts: {
options: { livereload: 35729 },
files: ['./app.js', './Gruntfile.js', './bin/www',
'./models/*.js', './routes/*.js', './forms/*.js'],
tasks: ['jshint','mochaTest'],
options: {
debounceDelay: 750
}
},
tests: {
options: { livereload: 35729 },
files: ['./test/**/*.js'],
tasks: ['mochaTest']
},
public: {
files: ['./public/scripts/**/*', './public/css/**/*', './views/**/*.jade', './views/**/*.styl'],
options: {
debounceDelay: 750,
livereload: 35729
},
task: ['']
},
boweradmin: {
files: ['./public/scripts/admin/bower.json'],
tasks: ['wiredep:admin']
},
bowersite: {
files: ['bower.json'],
tasks: ['wiredep:site']
}
},
nodemon: {
dev: {
script: './bin/www',
options: {
ignore: [
'node_modules/**',
'public/js/**/*',
'public/scripts/**/*'],
callback: function (nodemon) {
// opens browser on initial server start
nodemon.on('config:update', function () {
// Delay before server listens on port
setTimeout(function() {
require('open')('http://localhost:3000/');
}, 3000);
});
}
}
}
},
docco: {
debug: {
src: ['test/**/*.js','models/*.js','routes/*.js'],
options: {
output: 'docs/'
}
}
},
wiredep: {
/*
admin: {
src: ['views/layout-admin.jade'],
options: {
cwd: './public/scripts/admin/',
ignorePath: '../public',
overrides: {
"directory": "./bower_components/"
}
}
},
*/
site: {
src: ['views/layout.jade'],
options: {
ignorePath: '../public',
overrides: {
"directory": "./bower_components/"
}
}
}
},
concurrent: {
dev: {
tasks: ['nodemon', 'watch'],
options: {
logConcurrentOutput: true,
limit: 5
}
},
test: ['mochaTest']
},
clean: {
public: './public/dist/{,*/}*',
temporal: '.tmp'
},
useminPrepare: {
html: 'views/**/*.jade',
options: {
dest: './public/dist/',
flow: {
html: {
steps: {
js: ['concat', 'uglifyjs'],
css: ['cssmin']
},
post: {
js: [{
name: 'uglify',
createConfig: function (context, block) {
var generated = context.options.generated;
generated.options = {
mangle: false
/*
http://mathieuhicauber-java.blogspot.com.ar/2013/12/grunt-uglify-task-breaks-angular-app.html
http://stackoverflow.com/questions/21688681/unknown-provider-aprovider-a-how-do-i-find-the-original-provider
http://stackoverflow.com/a/20945907/467034
*/
};
}
}]
}
}
}
}
}
});
grunt.registerTask('buildprod', [
'clean:public',
//copy styles to .tmp
'useminPrepare',
//generates new task configs: concat, uglify
'concat',
'uglify',
'cssmin'
]);
grunt.task.registerTask('create', 'Create mongoose model + test', function(arg1, arg2) {
grunt.log.debug(arg1, arg2);
switch(arg1){
case 'model':
if(typeof arg2 !== 'undefined') {
// create model
var filemodel = grunt.template.process(grunt.file.read('./templates/mongoose-model.js.tpl'), {
data: {
'modelname': arg2
}
});
var filename = './models/'+arg2.toLowerCase()+'.js';
grunt.file.write(filename, filemodel);
// create test
var filetest = grunt.template.process(grunt.file.read('./templates/mongoose-model-tests.js.tpl'), {
data: {
'modelname': arg2
}
});
var filenametest = './test/unit/models/'+arg2.toLowerCase()+'-tests.js';
grunt.file.write(filenametest, filetest);
// append model to ./routes/main.js
var appendm = grunt.template.process(grunt.file.read('./templates/append-model-routes-main.tpl'), {
data: {
'modelname': arg2
}
});
var filename = './routes/main.js';
var mainroutes = grunt.file.read(filename);
var regexpm = new RegExp(".*"+appendm.replace("(", "\\(").replace(")","\\)")+".*");
if(!mainroutes.match(regexpm)){
grunt.file.write(filename,
mainroutes.replace("/* models:end */",appendm+" /* models:end */"));
}
// register model in ./routes/main.js
var appendm = grunt.template.process(grunt.file.read('./templates/append-model-registration.tpl'), {
data: {
'modelname': arg2
}
});
var filename = './routes/main.js';
var mainroutes = grunt.file.read(filename);
var regexpm = new RegExp(".*"+appendm.replace("(", "\\(").replace(")","\\)")+".*");
if(!mainroutes.match(regexpm)){
grunt.file.write(filename,
mainroutes.replace("/* models:registration:end */",appendm));
}
} else {
grunt.log.warn('Parameter name missing for '+arg1+' task');
}
break;
case 'page':
if(typeof arg2 !== 'undefined') {
// create model
var filepage = grunt.template.process(grunt.file.read('./templates/view-page.jade.tpl'), {
data: {
'pagename': arg2
}
});
var filename = './views/'+arg2.toLowerCase()+'.jade';
grunt.file.write(filename, filepage);
// append page route to ./routes/main.js
var appendp = grunt.template.process(grunt.file.read('./templates/append-page-routes-main.tpl'), {
data: {
'pagename': arg2
}
});
var filename = './routes/main.js';
var mainroutes = grunt.file.read(filename);
grunt.file.write(filename, mainroutes.replace("/* page:public:end */",appendp+"/* page:public:end */"));
// append page link to ./views/partials/site-menu.jade
var appendm = grunt.template.process(grunt.file.read('./templates/append-site-menu-item.tpl'), {
data: {
'pagename': arg2
}
});
var filename = './views/partials/site-menu.jade';
var mainroutes = grunt.file.read(filename);
grunt.file.write(filename, mainroutes.replace("// public:page:menu:end",appendm));
} else {
grunt.log.warn('Parameter name missing for '+arg1+' task');
}
break;
case 'crud':
if(typeof arg2 !== 'undefined') {
// create Front-end files
var feFilesRoot = {
"templates" : "./templates/crud/admin/controllers/",
"destination" : "./public/scripts/admin/controllers/"
};
var feFiles = {
"model-edit-js.tpl" : "<%=modelname%>-edit.js",
"model-new-js.tpl" : "<%=modelname%>-new.js",
"model-js.tpl" : "<%=modelname%>.js",
"../views/model-html.tpl" : "../views/<%=modelname%>.html"
};
for(var i in feFiles){
var filefetemp = feFilesRoot.templates+i;
var filefe = grunt.template.process(grunt.file.read(filefetemp), {
data: {
'modelname': arg2
}
});
var filenamefe = feFilesRoot.destination+feFiles[i].replace("<%=modelname%>",arg2.toLowerCase());
grunt.file.write(filenamefe, filefe);
}
// append Front-end files
var appendfe = grunt.template.process(grunt.file.read('./templates/crud/admin-crud-scripts-append.tpl'), {
data: {
'modelname': arg2
}
});
var filenamefe = './views/admin-panel.jade';
var mainfe = grunt.file.read(filenamefe);
grunt.file.write(filenamefe, mainfe.replace("//admin:crud:scripts:end",appendfe));
// append CRUD menu into admin
var appendcmenu = grunt.template.process(grunt.file.read('./templates/crud/admin-crud-menu-append.tpl'), {
data: {
'modelname': arg2
}
});
var filenamecmenu = './views/partials/admin-menu.jade';
var maincmenu = grunt.file.read(filenamecmenu);
grunt.file.write(filenamecmenu, maincmenu.replace("//admin:crud:menu:end",appendcmenu));
// append routes to ./public/scripts/admin/app.js
var appendcroute = grunt.template.process(grunt.file.read('./templates/crud/admin/app-route-append.tpl'), {
data: {
'modelname': arg2
}
});
var filenamecroute = './public/scripts/admin/app.js';
var maincroute = grunt.file.read(filenamecroute);
grunt.file.write(filenamecroute, maincroute.replace(".otherwise({",appendcroute));
// create tests
/*
var appendp = grunt.template.process(grunt.file.read('./templates/append-model-rest-main.tpl'), {
data: {
'modelname': arg2
}
});
var filename = './routes/main.js';
var mainroutes = grunt.file.read(filename);
*/
//grunt.file.write(filename, mainroutes.replace("/* rest:public:end */",appendp));
/*
// create test
var filetest = grunt.template.process(grunt.file.read('./templates/mongoose-model-rest-tests.js.tpl'), {
data: {
'modelname': arg2
}
});
var filenametest = './test/rest/models/'+arg2.toLowerCase()+'s-rest-tests.js';
grunt.file.write(filenametest, filetest);
*/
} else {
grunt.log.warn('Parameter name missing for '+arg1+' task');
}
break;
case 'rest':
if(typeof arg2 !== 'undefined') {
// append rest to ./routes/main.js
var appendp = grunt.template.process(grunt.file.read('./templates/append-model-rest-main.tpl'), {
data: {
'modelname': arg2
}
});
var filename = './routes/main.js';
var mainroutes = grunt.file.read(filename);
grunt.file.write(filename, mainroutes.replace("/* rest:public:end */",appendp));
// create test
var filetest = grunt.template.process(grunt.file.read('./templates/mongoose-model-rest-tests.js.tpl'), {
data: {
'modelname': arg2
}
});
var filenametest = './test/rest/models/'+arg2.toLowerCase()+'s-rest-tests.js';
grunt.file.write(filenametest, filetest);
} else {
grunt.log.warn('Parameter name missing for '+arg1+' task');
}
break;
case 'locale':
if(typeof arg2 !== 'undefined') {
// append locale to ./apps.js
var appendp = "'"+arg2.toLowerCase()+"',\n //global:translation:end"
var filename = './app.js';
var mainroutes = grunt.file.read(filename);
grunt.file.write(filename, mainroutes.replace("//global:translation:end",appendp));
// append page link to ./views/partials/site-menu.jade
var appendm = "li\n";
appendm += " a(href='#', langsupport='"+arg2.toLowerCase()+"') "+arg2.toLowerCase()+"\n";
appendm += " //public:translation:menu:end";
var filename = './views/partials/site-menu.jade';
var mainroutes = grunt.file.read(filename);
grunt.file.write(filename, mainroutes.replace("//public:translation:menu:end",appendm));
} else {
grunt.log.warn('Parameter name missing for '+arg1+' task');
}
break;
}
});
grunt.registerTask('default', ['jshint', 'concurrent']);
};