gulpfile.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /* jshint node:true, camelcase:false */
  2. var gulp = require('gulp');
  3. var karma = require('karma').server;
  4. var merge = require('merge-stream');
  5. var plug = require('gulp-load-plugins')();
  6. var paths = {
  7. js: './toastr.js',
  8. less: './toastr.less',
  9. report: './report',
  10. build: './build'
  11. };
  12. var log = plug.util.log;
  13. /**
  14. * List the available gulp tasks
  15. */
  16. gulp.task('help', plug.taskListing);
  17. /**
  18. * Lint the code, create coverage report, and a visualizer
  19. * @return {Stream}
  20. */
  21. gulp.task('analyze', function () {
  22. log('Analyzing source with JSHint and JSCS');
  23. var jshint = analyzejshint([paths.js]);
  24. var jscs = analyzejscs([paths.js]);
  25. return merge(jshint, jscs);
  26. });
  27. /**
  28. * Minify and bundle the app's JavaScript
  29. * @return {Stream}
  30. */
  31. gulp.task('js', function () {
  32. log('Bundling, minifying, and copying the app\'s JavaScript');
  33. return gulp
  34. .src(paths.js)
  35. .pipe(plug.sourcemaps.init())
  36. .pipe(plug.bytediff.start())
  37. .pipe(plug.uglify({}))
  38. .pipe(plug.bytediff.stop(bytediffFormatter))
  39. .pipe(plug.sourcemaps.write('.'))
  40. .pipe(plug.rename(function (path) {
  41. if (path.extname === '.js') {
  42. path.basename += '.min';
  43. }
  44. }))
  45. .pipe(gulp.dest(paths.build));
  46. });
  47. /**
  48. * Minify and bundle the CSS
  49. * @return {Stream}
  50. */
  51. gulp.task('css', function () {
  52. log('Bundling, minifying, and copying the app\'s CSS');
  53. return gulp.src(paths.less)
  54. .pipe(plug.less())
  55. .pipe(gulp.dest(paths.build))
  56. .pipe(plug.bytediff.start())
  57. .pipe(plug.minifyCss({}))
  58. .pipe(plug.bytediff.stop(bytediffFormatter))
  59. .pipe(plug.rename('toastr.min.css'))
  60. .pipe(gulp.dest(paths.build));
  61. });
  62. /**
  63. * Build js and css
  64. */
  65. gulp.task('default', ['js', 'css'], function () {
  66. log('Analyze, Build CSS and JS');
  67. });
  68. /**
  69. * Remove all files from the build folder
  70. * One way to run clean before all tasks is to run
  71. * from the cmd line: gulp clean && gulp build
  72. * @return {Stream}
  73. */
  74. gulp.task('clean', function (cb) {
  75. log('Cleaning: ' + plug.util.colors.blue(paths.report));
  76. log('Cleaning: ' + plug.util.colors.blue(paths.build));
  77. var delPaths = [paths.build, paths.report];
  78. del(delPaths, cb);
  79. });
  80. /**
  81. * Run specs once and exit
  82. * To start servers and run midway specs as well:
  83. * gulp test --startServers
  84. * @return {Stream}
  85. */
  86. gulp.task('test', function (done) {
  87. startTests(true /*singleRun*/, done);
  88. });
  89. ////////////////
  90. /**
  91. * Execute JSHint on given source files
  92. * @param {Array} sources
  93. * @param {String} overrideRcFile
  94. * @return {Stream}
  95. */
  96. function analyzejshint(sources, overrideRcFile) {
  97. var jshintrcFile = overrideRcFile || './.jshintrc';
  98. log('Running JSHint');
  99. return gulp
  100. .src(sources)
  101. .pipe(plug.jshint(jshintrcFile))
  102. .pipe(plug.jshint.reporter('jshint-stylish'));
  103. }
  104. /**
  105. * Execute JSCS on given source files
  106. * @param {Array} sources
  107. * @return {Stream}
  108. */
  109. function analyzejscs(sources) {
  110. log('Running JSCS');
  111. return gulp
  112. .src(sources)
  113. .pipe(plug.jscs('./.jscsrc'));
  114. }
  115. /**
  116. * Start the tests using karma.
  117. * @param {boolean} singleRun - True means run once and end (CI), or keep running (dev)
  118. * @param {Function} done - Callback to fire when karma is done
  119. * @return {undefined}
  120. */
  121. function startTests(singleRun, done) {
  122. karma.start({
  123. configFile: __dirname + '/karma.conf.js',
  124. singleRun: !!singleRun
  125. }, karmaCompleted);
  126. ////////////////
  127. function karmaCompleted() {
  128. done();
  129. }
  130. }
  131. /**
  132. * Formatter for bytediff to display the size changes after processing
  133. * @param {Object} data - byte data
  134. * @return {String} Difference in bytes, formatted
  135. */
  136. function bytediffFormatter(data) {
  137. var difference = (data.savings > 0) ? ' smaller.' : ' larger.';
  138. return data.fileName + ' went from ' +
  139. (data.startSize / 1000).toFixed(2) + ' kB to ' + (data.endSize / 1000).toFixed(2) + ' kB' +
  140. ' and is ' + formatPercent(1 - data.percent, 2) + '%' + difference;
  141. }
  142. /**
  143. * Format a number as a percentage
  144. * @param {Number} num Number to format as a percent
  145. * @param {Number} precision Precision of the decimal
  146. * @return {Number} Formatted perentage
  147. */
  148. function formatPercent(num, precision) {
  149. return (num * 100).toFixed(precision);
  150. }