Advertisement
Guest User

Untitled

a guest
May 26th, 2016
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.84 KB | None | 0 0
  1. /*jslint node: true */
  2. 'use strict';
  3.  
  4. var pkg = require('./package.json');
  5. var server_link = "localhost";
  6. var port = "9001";
  7. //Using exclusion patterns slows down Grunt significantly
  8. //instead of creating a set of patterns like '**/*.js' and '!**/node_modules/**'
  9. //this method is used to create a set of inclusive patterns for all subdirectories
  10. //skipping node_modules, bower_components, dist, and any .dirs
  11. //This enables users to create any directory structure they desire.
  12. var createFolderGlobs = function(fileTypePatterns) {
  13. fileTypePatterns = Array.isArray(fileTypePatterns) ? fileTypePatterns : [fileTypePatterns];
  14. var ignore = ['node_modules','bower_components','dist','temp'];
  15. var fs = require('fs');
  16. return fs.readdirSync(process.cwd())
  17. .map(function(file){
  18. if (ignore.indexOf(file) !== -1 ||
  19. file.indexOf('.') === 0 ||
  20. !fs.lstatSync(file).isDirectory()) {
  21. return null;
  22. } else {
  23. return fileTypePatterns.map(function(pattern) {
  24. return file + '/**/' + pattern;
  25. });
  26. }
  27. })
  28. .filter(function(patterns){
  29. return patterns;
  30. })
  31. .concat(fileTypePatterns);
  32. };
  33.  
  34. module.exports = function (grunt) {
  35.  
  36. // load all grunt tasks
  37. require('load-grunt-tasks')(grunt);
  38.  
  39. // Project configuration.
  40. grunt.initConfig({
  41. connect: {
  42. main: {
  43. options: {
  44. port: 9002
  45. },
  46. proxies: [
  47. {
  48. context: '/upload',
  49. host: server_link,
  50. port: port,
  51. headers: {
  52. 'host': server_link,
  53. 'Referer':'http://localhost:9002/#/'
  54. }
  55. },
  56. ]
  57. }
  58. },
  59. watch: {
  60. main: {
  61. options: {
  62. livereload: true,
  63. livereloadOnError: false,
  64. spawn: false
  65. },
  66. files: [createFolderGlobs(['*.js','*.less','*.html']),'!_SpecRunner.html','!.grunt'],
  67. tasks: [] //all the tasks are run dynamically during the watch event handlers
  68. }
  69. },
  70. jshint: {
  71. main: {
  72. options: {
  73. jshintrc: '.jshintrc'
  74. },
  75. src: createFolderGlobs('*.js')
  76. }
  77. },
  78. clean: {
  79. before:{
  80. src:['dist','temp']
  81. },
  82. after: {
  83. src:['temp']
  84. }
  85. },
  86. less: {
  87. production: {
  88. options: {
  89. },
  90. files: {
  91. 'temp/app.css': 'app.less'
  92. }
  93. }
  94. },
  95. ngtemplates: {
  96. main: {
  97. options: {
  98. module: pkg.name,
  99. htmlmin:'<%= htmlmin.main.options %>'
  100. },
  101. src: [createFolderGlobs('*.html'),'!index.html','!_SpecRunner.html'],
  102. dest: 'temp/templates.js'
  103. }
  104. },
  105. copy: {
  106. main: {
  107. files: [
  108. {src: ['img/**'], dest: 'dist/'},
  109. {src: ['bower_components/font-awesome/fonts/**'], dest: 'dist/',filter:'isFile',expand:true},
  110. {src: ['bower_components/bootstrap/fonts/**'], dest: 'dist/',filter:'isFile',expand:true}
  111. //{src: ['bower_components/angular-ui-utils/ui-utils-ieshiv.min.js'], dest: 'dist/'},
  112. //{src: ['bower_components/select2/*.png','bower_components/select2/*.gif'], dest:'dist/css/',flatten:true,expand:true},
  113. //{src: ['bower_components/angular-mocks/angular-mocks.js'], dest: 'dist/'}
  114. ]
  115. }
  116. },
  117. dom_munger:{
  118. read: {
  119. options: {
  120. read:[
  121. {selector:'script[data-concat!="false"]',attribute:'src',writeto:'appjs'},
  122. {selector:'link[rel="stylesheet"][data-concat!="false"]',attribute:'href',writeto:'appcss'}
  123. ]
  124. },
  125. src: 'index.html'
  126. },
  127. update: {
  128. options: {
  129. remove: ['script[data-remove!="false"]','link[data-remove!="false"]'],
  130. append: [
  131. {selector:'body',html:'<script src="app.full.min.js"></script>'},
  132. {selector:'head',html:'<link rel="stylesheet" href="app.full.min.css">'}
  133. ]
  134. },
  135. src:'index.html',
  136. dest: 'dist/index.html'
  137. }
  138. },
  139. cssmin: {
  140. main: {
  141. src:['temp/app.css','<%= dom_munger.data.appcss %>'],
  142. dest:'dist/app.full.min.css'
  143. }
  144. },
  145. concat: {
  146. main: {
  147. src: ['<%= dom_munger.data.appjs %>','<%= ngtemplates.main.dest %>'],
  148. dest: 'temp/app.full.js'
  149. }
  150. },
  151. ngAnnotate: {
  152. main: {
  153. src:'temp/app.full.js',
  154. dest: 'temp/app.full.js'
  155. }
  156. },
  157. uglify: {
  158. main: {
  159. src: 'temp/app.full.js',
  160. dest:'dist/app.full.min.js'
  161. }
  162. },
  163. htmlmin: {
  164. main: {
  165. options: {
  166. collapseBooleanAttributes: true,
  167. collapseWhitespace: true,
  168. removeAttributeQuotes: true,
  169. removeComments: true,
  170. removeEmptyAttributes: true,
  171. removeScriptTypeAttributes: true,
  172. removeStyleLinkTypeAttributes: true
  173. },
  174. files: {
  175. 'dist/index.html': 'dist/index.html'
  176. }
  177. }
  178. },
  179. //Imagemin has issues on Windows.
  180. //To enable imagemin:
  181. // - "npm install grunt-contrib-imagemin"
  182. // - Comment in this section
  183. // - Add the "imagemin" task after the "htmlmin" task in the build task alias
  184. // imagemin: {
  185. // main:{
  186. // files: [{
  187. // expand: true, cwd:'dist/',
  188. // src:['**/{*.png,*.jpg}'],
  189. // dest: 'dist/'
  190. // }]
  191. // }
  192. // },
  193. karma: {
  194. options: {
  195. frameworks: ['jasmine'],
  196. files: [ //this files data is also updated in the watch handlers, if updated change there too
  197. '<%= dom_munger.data.appjs %>',
  198. 'bower_components/angular-mocks/angular-mocks.js',
  199. createFolderGlobs('*-spec.js')
  200. ],
  201. logLevel:'ERROR',
  202. reporters:['mocha'],
  203. autoWatch: false, //watching is handled by grunt-contrib-watch
  204. singleRun: true
  205. },
  206. all_tests: {
  207. browsers: ['PhantomJS','Chrome','Firefox']
  208. },
  209. during_watch: {
  210. browsers: ['PhantomJS']
  211. }
  212. }
  213. });
  214.  
  215. grunt.registerTask('build',['jshint','clean:before','less','dom_munger','ngtemplates','cssmin','concat','ngAnnotate','uglify','copy','htmlmin','clean:after']);
  216. grunt.registerTask('serve', ['dom_munger:read','jshint','connect', 'watch']);
  217. grunt.registerTask('test',['dom_munger:read','karma:all_tests']);
  218.  
  219. grunt.event.on('watch', function(action, filepath) {
  220. //https://github.com/gruntjs/grunt-contrib-watch/issues/156
  221.  
  222. var tasksToRun = [];
  223.  
  224. if (filepath.lastIndexOf('.js') !== -1 && filepath.lastIndexOf('.js') === filepath.length - 3) {
  225.  
  226. //lint the changed js file
  227. grunt.config('jshint.main.src', filepath);
  228. tasksToRun.push('jshint');
  229.  
  230. //find the appropriate unit test for the changed file
  231. var spec = filepath;
  232. if (filepath.lastIndexOf('-spec.js') === -1 || filepath.lastIndexOf('-spec.js') !== filepath.length - 8) {
  233. spec = filepath.substring(0,filepath.length - 3) + '-spec.js';
  234. }
  235.  
  236. //if the spec exists then lets run it
  237. if (grunt.file.exists(spec)) {
  238. var files = [].concat(grunt.config('dom_munger.data.appjs'));
  239. files.push('bower_components/angular-mocks/angular-mocks.js');
  240. files.push(spec);
  241. grunt.config('karma.options.files', files);
  242. tasksToRun.push('karma:during_watch');
  243. }
  244. }
  245.  
  246. //if index.html changed, we need to reread the <script> tags so our next run of karma
  247. //will have the correct environment
  248. if (filepath === 'index.html') {
  249. tasksToRun.push('dom_munger:read');
  250. }
  251.  
  252. grunt.config('watch.main.tasks',tasksToRun);
  253.  
  254. });
  255. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement