gulpfile.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. var gulp = require('gulp')
  2. var jshint = require('gulp-jshint')
  3. var webpack = require("webpack")
  4. var connect = require('gulp-connect')
  5. var mochaPhantomJS = require('gulp-mocha-phantomjs')
  6. var exec = require('child_process').exec
  7. var istanbul = require('gulp-istanbul')
  8. var mocha = require('gulp-mocha')
  9. var coveralls = require('gulp-coveralls')
  10. //
  11. gulp.task('hello', function() {
  12. console.log((function() {
  13. /*
  14. ___ ___ _ _
  15. | \/ | | | (_)
  16. | . . | ___ ___ | | __ _ ___
  17. | |\/| | / _ \ / __|| |/ / | |/ __|
  18. | | | || (_) || (__ | < _ | |\__ \
  19. \_| |_/ \___/ \___||_|\_\(_)| ||___/
  20. _/ |
  21. |__/
  22. */
  23. }).toString().split('\n').slice(2, -2).join('\n') + '\n')
  24. })
  25. // https://github.com/AveVlad/gulp-connect
  26. gulp.task('connect', function() {
  27. /* jshint unused:false */
  28. connect.server({
  29. port: 5050,
  30. middleware: function(connect, opt) {
  31. return [
  32. // https://github.com/senchalabs/connect/#use-middleware
  33. function cors(req, res, next) {
  34. res.setHeader('Access-Control-Allow-Origin', '*')
  35. res.setHeader('Access-Control-Allow-Methods', '*')
  36. next()
  37. }
  38. ]
  39. }
  40. })
  41. })
  42. // https://github.com/spenceralger/gulp-jshint
  43. gulp.task('jshint', function() {
  44. var globs = [
  45. 'src/**/*.js', 'test/test.*.js', 'gulpfile.js', '!**/regexp/parser.js'
  46. ]
  47. return gulp.src(globs)
  48. .pipe(jshint('.jshintrc'))
  49. .pipe(jshint.reporter('jshint-stylish'))
  50. })
  51. // https://webpack.github.io/docs/usage-with-gulp.html
  52. gulp.task("webpack", function( /*callback*/ ) {
  53. webpack({
  54. entry: './src/mock.js',
  55. output: {
  56. path: './dist',
  57. filename: 'mock.js',
  58. library: 'Mock',
  59. libraryTarget: 'umd'
  60. }
  61. }, function(err /*, stats*/ ) {
  62. // console.log(err, stats)
  63. if (err) throw err
  64. })
  65. webpack({
  66. entry: './src/mock.js',
  67. devtool: 'source-map',
  68. output: {
  69. path: './dist',
  70. filename: 'mock-min.js',
  71. library: 'Mock',
  72. libraryTarget: 'umd'
  73. },
  74. plugins: [
  75. new webpack.optimize.UglifyJsPlugin({
  76. minimize: true
  77. })
  78. ]
  79. }, function(err /*, stats*/ ) {
  80. // console.log(err, stats)
  81. if (err) throw err
  82. })
  83. })
  84. // https://github.com/mrhooray/gulp-mocha-phantomjs
  85. gulp.task('mocha', function() {
  86. return gulp.src('test/test.mock.html')
  87. .pipe(mochaPhantomJS({
  88. reporter: 'spec'
  89. }))
  90. })
  91. // https://github.com/floatdrop/gulp-watch
  92. var watchTasks = ['hello', 'madge', 'jshint', 'webpack', 'mocha']
  93. gulp.task('watch', function( /*callback*/ ) {
  94. gulp.watch(['src/**/*.js', 'gulpfile.js', 'test/*'], watchTasks)
  95. })
  96. // https://github.com/pahen/madge
  97. gulp.task('madge', function( /*callback*/ ) {
  98. exec('madge ./src/',
  99. function(error, stdout /*, stderr*/ ) {
  100. if (error) console.log('exec error: ' + error)
  101. console.log('module dependencies:')
  102. console.log(stdout)
  103. }
  104. )
  105. exec('madge --image ./src/dependencies.png ./src/',
  106. function(error /*, stdout, stderr*/ ) {
  107. if (error) console.log('exec error: ' + error)
  108. }
  109. )
  110. })
  111. // TODO
  112. // https://github.com/SBoudrias/gulp-istanbul
  113. gulp.task('istanbul', function(cb) {
  114. gulp.src(['test/test.coveralls.js'])
  115. .pipe(istanbul()) // Covering files
  116. .pipe(istanbul.hookRequire()) // Force `require` to return covered files
  117. .on('finish', function() {
  118. gulp.src(['test/test.coveralls.js'])
  119. .pipe(mocha({}))
  120. .pipe(istanbul.writeReports()) // Creating the reports after tests runned
  121. .on('end', cb)
  122. })
  123. })
  124. gulp.task('istanbulForMochaPhantomJS', function(cb) {
  125. gulp.src(['dist/mock.js'])
  126. .pipe(istanbul()) // Covering files
  127. .pipe(istanbul.hookRequire()) // Force `require` to return covered files
  128. .on('finish', function() {
  129. gulp.src(['test/test.mock.html'])
  130. .pipe(mochaPhantomJS({
  131. reporter: 'spec'
  132. }))
  133. .pipe(istanbul.writeReports()) // Creating the reports after tests runned
  134. .on('end', cb)
  135. })
  136. })
  137. // https://github.com/markdalgleish/gulp-coveralls
  138. gulp.task('coveralls', ['istanbul'], function() {
  139. return gulp.src('coverage/**/lcov.info')
  140. .pipe(coveralls())
  141. })
  142. //
  143. gulp.task('publish', function() {
  144. var child_process = require('child_process')
  145. child_process.exec('ls', function(error, stdout, stderr) {
  146. console.log(error, stdout, stderr)
  147. })
  148. })
  149. gulp.task('default', watchTasks.concat(['watch', 'connect']))
  150. gulp.task('build', ['jshint', 'webpack', 'mocha'])