-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathGruntfile.js
840 lines (731 loc) · 23.6 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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
module.exports = function(grunt) {
'use strict'
// Import the modules which is necessary.
var fs = require('fs')
, path = require('path')
, underscore = require('underscore')
, timer = require('grunt-timer')
, _ = grunt.util._
// Load the plugin
grunt.loadNpmTasks('grunt-contrib-clean')
grunt.loadNpmTasks('grunt-contrib-copy')
grunt.loadNpmTasks('grunt-contrib-imagemin')
grunt.loadNpmTasks('grunt-contrib-jade')
grunt.loadNpmTasks('grunt-contrib-less')
grunt.loadNpmTasks('grunt-contrib-concat')
grunt.loadNpmTasks('grunt-contrib-uglify')
grunt.loadNpmTasks('grunt-contrib-jshint')
grunt.loadNpmTasks('grunt-contrib-watch')
grunt.loadNpmTasks('grunt-hashmap')
grunt.loadNpmTasks('grunt-favicons')
grunt.loadNpmTasks('grunt-spritesmith')
grunt.loadNpmTasks('grunt-html2js')
grunt.loadNpmTasks('grunt-bower')
grunt.loadNpmTasks('grunt-shell')
grunt.loadNpmTasks('grunt-karma')
// Configuring
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
name: '<%= pkg.name.replace(/^[A-Z]/, function(str) { return str.toLowerCase(); }) %>',
environment: 'DEVELOPMENT',
buildPath: '.build',
browserPath: 'bower_components',
stylePublicPath: 'client/public/styles',
styleDistPath: 'assets/styles',
scriptPublicPath: 'client/public/scripts',
scriptDistPath: 'assets/scripts',
hashmapFile: 'assets/hashmap/version.json',
spriteImageFile: 'assets/panels/sprites.png',
spriteLessFile: '<%= buildPath %>/styles/sprites.less',
stateFile: '<%= buildPath %>/grunt.state.json',
clean: {
build: '<%= buildPath %>',
dist: 'assets',
},
bower: {
dist: {
dest: 'assets/',
js_dest: 'assets/scripts',
css_dest: 'assets/styles',
fonts_dest: 'assets/fonts',
less_dest: 'client/public/styles',
/**
* Modify options what you want to import.
*/
options: {
expand: true,
ignorePackages: [
'jquery'
],
packageSpecific: {
'font-awesome': {
keepExpandedHierarchy: true,
stripGlobBase: true,
files: [
'css/*.css',
'fonts/*.{eot,svg,ttf,woff,woff2}',
]
},
'bootstrap': {
keepExpandedHierarchy: true,
stripGlobBase: true,
files: [
'dist/scripts/*.js',
'dist/fonts/*.{eot,svg,ttf,woff,woff2}',
'dist/css/*.css'
]
},
'iscroll': {
keepExpandedHierarchy: true,
stripGlobBase: true,
files: [
'build/*.js'
]
},
'lesshat': {
keepExpandedHierarchy: true,
stripGlobBase: true,
less_dest: 'client/public/styles/mixins',
files: [
'build/*.less'
]
},
'script': {
keepExpandedHierarchy: true,
stripGlobBase: true,
files: [
'dist/*.js'
]
},
'pace': {
keepExpandedHierarchy: true,
stripGlobBase: true,
files: [
'themes/orange/*.css',
'*.js'
]
}
}
}
}
},
favicons: {
options: {
trueColor: true,
precomposed: true,
appleTouchPadding: 0,
appleTouchBackgroundColor: 'auto',
coast: true,
windowsTile: true,
tileBlackWhite: false,
tileColor: 'auto',
},
ico: {
src: 'client/public/panels/david-logo.png',
dest: 'assets/panels/'
}
},
/**
* 制作精灵图,所有精灵元素均可以保存在 `client/public/sprites/` 目录下
*/
sprite: {
dist: {
src: 'client/public/panels/sprites/*.png',
dest: '<%= spriteImageFile %>',
destCss: '<%= spriteLessFile %>',
padding: 10,
cssFormat: 'less',
cssTemplate: 'client/public/panels/sprites.less.mustache',
cssVarMap: function(sprite) {
sprite.name = 'sp-' + sprite.name;
},
cssOpts: {
basepath: '../../panels/sprites.png',
functions: true
}
}
},
imagemin: {
picture: {
options: {
optimizationLevel: 3
},
cwd: 'client/public/panels/',
src: ['*.{png,jpg,gif}'],
dest: 'assets/panels/',
expand: true,
}
},
copy: {
assets: {
cwd: 'client/public/',
dest: 'assets/',
src: ['audio/**', 'fonts/**', 'panels/*.{png,jpg,gif,ico}'],
expand: true
}
},
less: {
options: {
compress: true,
yuicompress: true,
sourceMap: true,
outputSourceFiles: true,
paths: ['./', 'client/public/styles/'],
},
public: {
options: {
banner: '/* <%= pkg.name %>.css#<%= pkg.version %> - <%= grunt.template.today("yyyy-mm-dd HH:MM:ss") %> */\n',
sourceMapFilename: '<%= styleDistPath %>/<%= name %>.min.css.map',
},
src: ['<%= buildPath %>/styles/bootstrap.less'],
dest: '<%= styleDistPath %>/<%= name %>/<%= name %>.min.css',
}
},
script: {
srcModules: [],
srcFiles: [],
},
// html|jade|exjs|haml to angular module.
html2js: {
ui: {
options: {
useStrict: true,
base: 'client/public/scripts/ui/templates',
module: null,
rename: function(name) {
return 'templates/' + name.replace(path.extname(name), '.html');
}
},
dest: '<%= buildPath %>/scripts/ui/templates',
cwd: 'client/public/scripts/ui/templates',
src: ['**/*.jade'],
ext: '.html.js',
expand: true
}
},
concat: {
options: {
separator: ';'
},
public: {
options: {
banner: '// <%= pkg.name %>.js#<%= pkg.version %> - <%= grunt.template.today("yyyy-mm-dd HH:MM:ss") %>\n',
},
src: ['<%= script.srcFiles %>'],
dest: '<%= scriptDistPath %>/<%= name %>/<%= name %>.js',
}
},
uglify: {
options: {
sourceMap: true,
},
public: {
options: {
banner: '// <%= pkg.name %>.min.js#<%= pkg.version %> - <%= grunt.template.today("yyyy-mm-dd HH:MM:ss") %>\n',
},
src: ['<%= concat.public.dest %>'],
dest: '<%= scriptDistPath %>/<%= name %>/<%= name %>.min.js',
},
app: {
dest: 'assets/scripts/app/',
cwd: 'assets/scripts/app/',
src: ['*.js'],
expand: true,
},
},
hashmap: {
options: {
keep: true,
output: '<%= hashmapFile %>',
rename: '#{= dirname}/#{= basename}.#{= hash}#{= extname}'
},
dist: {
dest: './',
cwd: './',
src: [
'assets/scripts/*/*.js',
'assets/styles/*/*.css',
'assets/fonts/*/*.{eot,svg,ttf,woff,woff2}',
'assets/panels/*/*.{png,jpg,gif}'
]
}
},
jade: {
options: {
data: function() {
return {
pkg: grunt.config('pkg'),
makeVersion: makeVersion,
PUBLIC: 'client/public/templates/',
}
}
},
views: {
dest: 'assets/views/',
cwd: 'client/app/',
src: ['*/*.jade'],
ext: '.html',
expand: true,
}
},
jshint: {
options: {
jshintrc: true,
},
gruntfile: {
src: './Gruntfile.js',
},
scripts: {
src: ['client/public/scripts/**/*.js', 'client/apps/**/scripts/*.js'],
},
},
karma: {
public: {
configFile: 'karma.conf.js',
}
},
shell: {
grunt: {
command: 'npm install',
},
bower: {
command: 'bower install',
},
},
watch: {
// config
'config-package': {
options: {
event: ['added', 'changed'],
},
files: ['package.json'],
tasks: ['shell:grunt', '<%= environment === "DEVELOPMENT" ? "development" : "production" %>']
},
'config-bower': {
options: {
event: ['added', 'changed'],
},
files: ['bower.json'],
tasks: ['shell:bower', 'bower']
},
'config-gruntfile': {
options: {
event: ['added', 'changed'],
reload: true,
spawn: false,
},
files: ['Gruntfile.js'],
tasks: ['<%= environment === "DEVELOPMENT" ? "development" : "production" %>']
},
'style': {
files: ['client/public/styles/*/*.less', 'client/app/*/styles/*/*.less'],
tasks: ['compileStyle']
},
'script': {
files: [
'client/public/scripts/**/*.js'
, 'client/public/scripts/ui/templates/**/*.jade'
, 'client/app/*/scripts/**/*.js'
],
tasks: ['concatJS']
},
'layout': {
files: [
'client/app/*/templates/**/*.jade'
, 'client/app/*/templates/*.jade'
, 'client/app/*/index.jade'
],
tasks: ['compileJade']
}
},
})
// Merged Tasks
// ================
/**
* 该任务将会合并并编译 `client/public/styles` 下所有的样式。你可以在
* `client/public/styles/bootstrap.json` 里面配置一下文件导入顺序,
* 因为公共样式并没有很强的单一依赖关系,则多个文件可能互相不依赖,但他们都
* 必须引入到公用样式用,因此我们必须安排他们的顺序。
*
* `bootstrap.json` (因为有 `/*` 所以加上反斜杠)
* [
* "/variables/**\/*.less"
* , "/mixins/**\/*.less"
* , "/resets/**\/*.less"
* , "/core/**\/*.less"
* , "/effects/**\/*.less"
* , "/utilities/**\/*.less"
* , "/components/**\/*.less"
* ]
*
* 除此以外,任务还会根据 `client/app/` 下的目录再合并各个应用。我们可以将
* 这些应用看成一个 SPA,而这些应用下的各种情景我们可以将它定义为一个模块。
*
* 例如:
* client/
* app/
* index/
* styles/
* index.less
* user.less
*
* 这样我们就是生成一个名叫 `index` 的应用,而不同模块的样式我们统一放在
* `styles/` 里面,因为里面的样式一般都各自不没有任何关联,因此我们将他们
* 的所有样式文件合并成一个与应用名一样的 CSS 文件,并存放在 `assets/styles/app`
* 目录下;同时这些文件可以 `@import` 文件夹 `client/public/style` 下的所有文件。
* 若有特殊的 mixins 我们可以再在 `style` 目录下再建立 `mixins`
* 等文件夹,再通过 `@import` 引入。
*
* 注意:
*
* 系统会将应用样式写入到 `watch` 任务中,同时也会将扩展一个 `less` 任务,
* 当应用样式修改的时候,只会执行该生成的 `less` 任务。
*
* 当公共样式有 `新增/修改/删除` 的时候,系统将会重新执行该任务;应用样式有 `新增/删除` 的时候,
* 系统也会重新执行该任务;而应用样式 修改 后只会执行重新编译该应用的样式。
*/
grunt.registerTask('less2Css', 'Find the style file and compile them to css.', function() {
var bootstrapFile = grunt.config('buildPath') + '/styles/bootstrap.less'
, distPath = grunt.config('styleDistPath')
, absPaths = (grunt.file.readJSON('client/public/styles/bootstrap.json') || [])
.map(function(path) {
return grunt.config('stylePublicPath') + path
})
, modules = []
// Public style
// ============
grunt.log.subhead('Compile public less files.')
modules = findLesses(grunt.file.expand(absPaths))
grunt.file.write(bootstrapFile, _.pluck(modules, 'srcFile')
.map(function(file) {
grunt.log.writeln('File ' + file.cyan + ' imported.')
return '@import ' + enquote(file) + ';'
})
.join('\n'))
grunt.log.writeln('Above state will be saved in temp file ' + enquote(bootstrapFile).cyan)
// Apps style
// ==========
grunt.file
.expand('client/app/*')
.forEach(function(dir) {
var stats = fs.lstatSync(dir)
, name = dir.split('/').splice(-1, 1)[0]
, bootstrapFile = grunt.config('buildPath') + '/styles/app/' + name + '.less'
, modules = []
if (!stats.isDirectory()) {
return
}
grunt.log.subhead('Compile module `' + name + '` less files.')
modules = findLesses(grunt.file.expand(dir + '/styles/*.less'))
grunt.file.write(bootstrapFile, _.pluck(modules, 'srcFile')
.map(function(file) {
grunt.log.writeln('File ' + file.cyan + ' imported.')
return '@import ' + enquote(file) + ';'
})
.join('\n'))
grunt.log.writeln('Above state will be saved in temp file ' + enquote(bootstrapFile).cyan)
// Extend jade task.
grunt.config('less.$' + name, {
options: _.extend(grunt.config('less.options'), {
banner: '/* ' + name + '.css#<%= pkg.version %> - <%= grunt.template.today("yyyy-mm-dd HH:MM:ss") %> */\n',
sourceMapFilename: distPath + '/app/' + name + '.min.css.map',
}),
src: ['<%= buildPath %>/styles/app/' + name + '.less'],
dest: distPath + '/app/' + name + '.min.css',
})
})
grunt.task.run(['less', 'saveState'])
})
/**
* concatJS 任务首先会根据各应用中的模块 `client/app/*` 获取所有需要用到的公共模块,
* 这些公共模块再会通过依赖关系获取相应的模块,这样就可以列出相应的顺序引入到公共模块。
* 然后再合并 `client/app\/*\/scripts/` 下的所有模块生成名为应用名的脚本文件。
*/
grunt.registerTask('concatJS', 'According to dependencies, merge and sort the angularjs files.', function() {
var appFiles = []
, appModules = []
, cmmModules = []
, cmmDeps = []
, uiTplModules = []
appFiles = grunt.file
.expand('client/app/*/scripts/*.js')
.map(function(regexp) {
return grunt.config.process(regexp)
})
cmmModules = findScripts(appFiles, { isRoot: true })
.filter(function(module) {
if (-1 === appFiles.indexOf(module.srcFile)) {
return true
}
else {
appModules.push(module)
return false
}
})
// Find out all common moudles' dependencies.
_.pluck(cmmModules, 'dependencies')
.forEach(function(deps) {
if (deps.length > 0) {
cmmDeps = cmmDeps.concat(deps)
}
})
cmmDeps = underscore.unique(cmmDeps)
/**
* Find out all the angular ui tempalte file.
* Compile them to angular modules and save to the tmp path.
*/
uiTplModules = grunt.file
.expand(grunt.config('scriptPublicPath') + '/ui/templates/*/*.jade')
.map(function(filePath) {
var file = path.basename(filePath).replace(path.extname(filePath), '.html')
, relativePath = path.dirname(filePath).split('/').pop() + '/' + file
, name = 'templates/' + relativePath
, moduleName = enquote(name)
, srcFile = grunt.config('buildPath') + '/scripts/ui/templates/' + relativePath + '.js'
return { name: name, moduleName: moduleName, srcFile: srcFile }
})
.filter(function(module) {
return -1 !== cmmDeps.indexOf(module.name)
})
// Pakage configure & Run concat task
var pubModules = cmmModules.concat(uiTplModules)
, srcModules = _.pluck(pubModules, 'moduleName')
, srcFiles = _.pluck(pubModules, 'srcFile')
srcFiles
.forEach(function(file) {
grunt.log.writeln('File ' + file.cyan + ' imported.')
})
grunt.config('script.srcModules', srcModules)
grunt.config('script.srcFiles', srcFiles)
// Add app file concat task and add a watching task.
var apps = {}
appModules
.forEach(function(module) {
var name = path
.dirname(module.srcFile)
.split('\/')
.slice(2,3)[0]
if (!apps[name]) {
apps[name] = []
}
apps[name].push(module)
})
var name = ''
for (name in apps) {
grunt.log.subhead('Concat scripts file of `' + name + '` SPA.')
// Extend concat task.
grunt.config('concat.$' + name, {
options: {
banner: '// ' + name + '.js#<%= pkg.version %> - <%= grunt.template.today("yyyy-mm-dd HH:MM:ss") %>\n',
},
src: apps[name]
.map(function(app) {
grunt.log.writeln('File ' + app.srcFile.cyan + ' imported.')
return app.srcFile
}),
dest: '<%= scriptDistPath %>/app/' + name + '.js',
})
}
// Run the concat task.
grunt.task.run(['html2js:ui', 'concat'])
})
grunt.registerTask('compileJade', 'Find the jade file and watch them in each app.', function() {
grunt.file
.expand('client/app/*/')
.forEach(function(dir) {
var stats = fs.lstatSync(dir)
, name = dir.split('/').splice(-2, 1)[0]
if (!stats.isDirectory()) {
return
}
// Extend jade task.
grunt.config('jade.$' + name, {
dest: 'assets/templates/' + name + '/',
cwd: 'client/app/' + name + '/templates/',
src: '*.jade',
ext: '.html',
expand: true,
})
})
grunt.task.run(['jade'])
})
grunt.registerTask('loadState', 'Load grunt\'s config from json file and merge them.', loadState)
grunt.registerTask('saveState', 'Save grunt config state to json file.', saveState)
grunt.registerTask('buildAssets', ['bower', 'copy:assets', 'favicons', 'sprite', 'imagemin'])
grunt.registerTask('compileStyle', ['less2Css'])
grunt.registerTask('concatScript', ['concatJS'])
grunt.registerTask('compileLayout', ['compileJade'])
grunt.registerTask('default', ['development'])
grunt.registerTask('test-config', 'Task for test the grunt file.', function() {
grunt.task.run(['jshint:gruntfile'])
})
/**
* If you in development. You can only run development task.
* It will only concat the file, run the unit-test. But it will
* not to compress the files.
*/
grunt.registerTask('development', 'Task for develop environment.', function() {
timer.init(grunt)
grunt.config('environment', 'DEVELOPMENT')
grunt.config('less.options', _.extend(grunt.config('less.options'), {
compress: false,
yuicompress: false,
sourceMap: false,
outputSourceFiles: false,
}))
grunt.task.run(['clean', 'buildAssets', 'compileStyle', 'concatScript', 'compileLayout', 'watch'])
})
/**
* Task for release a new version.
*/
grunt.registerTask('release', 'Task for jshint test and unit test before release a new version.', function() {
grunt.task.run(['clean', 'test-config', 'clean', 'buildAssets', 'compileStyle', 'concatScript', 'uglify', 'karma:release', 'hashmap', 'compileLayout'])
})
/**
* Production task, it is fully task to run the project.
* Watch the file, run the unit-test task, concat and compress files, etc.
*/
grunt.registerTask('production', 'Task for product environment.', function() {
grunt.config('environment', 'PRODUCTION')
grunt.task.run(['test-config', 'clean', 'buildAssets', 'compileStyle', 'concatScript', 'uglify', 'hashmap', 'compileLayout', 'watch'])
})
// Helpers
// ==========================
function makeVersion(filePath) {
var mapFilePath = grunt.config('hashmapFile')
, map
, token
, regexp
, extname
, dir
, name
, version
if (grunt.file.exists(mapFilePath)) {
map = grunt.file.readJSON(mapFilePath)
}
for (token in map) {
regexp = new RegExp(token + '$')
if (regexp.test(filePath)) {
dir = filePath.replace(regexp, '');
extname = /[.]/.exec(filePath) ? /[^.]+$/.exec(filePath) : undefined;
name = token.replace(new RegExp('.' + extname + '$'), '');
version = map[token];
return dir + name + '.' + version + '.' + extname
}
}
return filePath;
}
function findLesses(args, options) {
var modules = []
, existsModules = {}
args.forEach(findLessModule)
return modules
function findLessModule(filePath) {
var name = path
.basename(filePath)
.replace(path.extname(filePath), '')
if (existsModules[filePath]) {
return
}
existsModules[filePath] = true
var module = {
name: name
, moduleName: enquote(name)
, displayName: ucwords(breakup(name, ' '))
, srcFile: filePath
, dependencies: dependenciesForModule(filePath)
}
if (fs.existsSync(module.srcFile)) {
module
.dependencies
.forEach(function(dep) {
var arr = dep.split('.')
arr.length > 1 && findLessModule('client/public/styles/' + arr.join('/') + '.js')
})
modules = modules.concat(module)
}
}
function dependenciesForModule(filePath) {
return []
}
}
function findScripts(args, options) {
options = options || {}
var modules = []
, existsModules = {}
args.forEach(function(filePath) {
findScriptModules(filePath, options)
})
return modules
function findScriptModules(filePath, options) {
var name = path.basename(filePath)
.replace(path.extname(filePath), '')
, moduleName = options.isRoot
? name
: filePath
.split('/')
.splice(-2, 2)
.join('.')
.replace(path.extname(filePath), '')
if (existsModules[filePath]) {
return
}
existsModules[filePath] = true
var module = {
name: name
, displayName: ucwords(breakup(name, ' '))
, moduleName: enquote(moduleName)
, srcFile: filePath
, dependencies: dependenciesForModule(filePath)
}
if (fs.existsSync(module.srcFile)) {
module
.dependencies
.forEach(function(dep) {
var arr = dep.split('.')
arr.length > 1 && findScriptModules('client/public/scripts/' + arr.join('/') + '.js', { isRoot: false })
})
modules = modules.concat(module)
}
}
function dependenciesForModule(filePath) {
if (fs.existsSync(filePath)) {
var contents = grunt.file.read(filePath)
, moduleDeclIndex = contents.indexOf('angular.module(')
, depArrayStart = contents.indexOf('[', moduleDeclIndex)
, depArrayEnd = contents.indexOf(']', depArrayStart)
, depText = contents.substring(depArrayStart + 1, depArrayEnd)
if ('' !== depText) {
return depText.replace(/[\'\s]/g, '').split(',')
}
}
return []
}
}
function enquote(str) {
return '"' + str + '"'
}
function breakup(text, separator) {
return text.replace(/[A-Z]/g, function(match) {
return separator + match
})
}
function ucwords(text) {
return text.replace(/^([a-z])|\s+([a-z])/g, function($1) {
return $1.toUpperCase()
})
}
/**
* As the watch task, grunt will read the origin config,
* so we must save the state of the config to build our
* project.
*/
function saveState() {
var config = JSON.stringify(grunt.config())
grunt.file.write(grunt.config('stateFile'), config)
}
function loadState() {
var config = grunt.file.readJSON(grunt.config('stateFile'))
grunt.config.merge(config)
}
}