gulpfile.js 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. Copyright (C) 2014 Yusuke Suzuki <utatane.tea@gmail.com>
  3. Redistribution and use in source and binary forms, with or without
  4. modification, are permitted provided that the following conditions are met:
  5. * Redistributions of source code must retain the above copyright
  6. notice, this list of conditions and the following disclaimer.
  7. * Redistributions in binary form must reproduce the above copyright
  8. notice, this list of conditions and the following disclaimer in the
  9. documentation and/or other materials provided with the distribution.
  10. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 'AS IS'
  11. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  12. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  13. ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
  14. DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  15. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  16. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  17. ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  18. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  19. THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  20. */
  21. 'use strict';
  22. var gulp = require('gulp'),
  23. mocha = require('gulp-mocha'),
  24. babel = require('gulp-babel'),
  25. git = require('gulp-git'),
  26. bump = require('gulp-bump'),
  27. filter = require('gulp-filter'),
  28. tagVersion = require('gulp-tag-version'),
  29. sourcemaps = require('gulp-sourcemaps'),
  30. plumber = require('gulp-plumber'),
  31. source = require('vinyl-source-stream'),
  32. browserify = require('browserify'),
  33. lazypipe = require('lazypipe'),
  34. eslint = require('gulp-eslint'),
  35. fs = require('fs');
  36. require('babel-register')({
  37. only: /escope\/(src|test)\//
  38. });
  39. var TEST = [ 'test/*.js' ];
  40. var SOURCE = [ 'src/**/*.js' ];
  41. var ESLINT_OPTION = {
  42. rules: {
  43. 'quotes': 0,
  44. 'eqeqeq': 0,
  45. 'no-use-before-define': 0,
  46. 'no-shadow': 0,
  47. 'no-new': 0,
  48. 'no-underscore-dangle': 0,
  49. 'no-multi-spaces': 0,
  50. 'no-native-reassign': 0,
  51. 'no-loop-func': 0,
  52. 'no-lone-blocks': 0
  53. },
  54. ecmaFeatures: {
  55. jsx: false,
  56. modules: true
  57. },
  58. env: {
  59. node: true,
  60. es6: true
  61. }
  62. };
  63. var BABEL_OPTIONS = JSON.parse(fs.readFileSync('.babelrc', { encoding: 'utf8' }));
  64. var build = lazypipe()
  65. .pipe(sourcemaps.init)
  66. .pipe(babel, BABEL_OPTIONS)
  67. .pipe(sourcemaps.write)
  68. .pipe(gulp.dest, 'lib');
  69. gulp.task('build-for-watch', function () {
  70. return gulp.src(SOURCE).pipe(plumber()).pipe(build());
  71. });
  72. gulp.task('build', function () {
  73. return gulp.src(SOURCE).pipe(build());
  74. });
  75. gulp.task('browserify', [ 'build' ], function () {
  76. return browserify({
  77. entries: [ './lib/index.js' ]
  78. })
  79. .bundle()
  80. .pipe(source('bundle.js'))
  81. .pipe(gulp.dest('build'))
  82. });
  83. gulp.task('test', [ 'build' ], function () {
  84. return gulp.src(TEST)
  85. .pipe(mocha({
  86. reporter: 'spec',
  87. timeout: 100000 // 100s
  88. }));
  89. });
  90. gulp.task('watch', [ 'build-for-watch' ], function () {
  91. gulp.watch(SOURCE, [ 'build-for-watch' ]);
  92. });
  93. // Currently, not works for ES6.
  94. gulp.task('lint', function () {
  95. return gulp.src(SOURCE)
  96. .pipe(eslint(ESLINT_OPTION))
  97. .pipe(eslint.formatEach('stylish', process.stderr))
  98. .pipe(eslint.failOnError());
  99. });
  100. /**
  101. * Bumping version number and tagging the repository with it.
  102. * Please read http://semver.org/
  103. *
  104. * You can use the commands
  105. *
  106. * gulp patch # makes v0.1.0 -> v0.1.1
  107. * gulp feature # makes v0.1.1 -> v0.2.0
  108. * gulp release # makes v0.2.1 -> v1.0.0
  109. *
  110. * To bump the version numbers accordingly after you did a patch,
  111. * introduced a feature or made a backwards-incompatible release.
  112. */
  113. function inc(importance) {
  114. // get all the files to bump version in
  115. return gulp.src(['./package.json'])
  116. // bump the version number in those files
  117. .pipe(bump({type: importance}))
  118. // save it back to filesystem
  119. .pipe(gulp.dest('./'))
  120. // commit the changed version number
  121. .pipe(git.commit('Bumps package version'))
  122. // read only one file to get the version number
  123. .pipe(filter('package.json'))
  124. // **tag it in the repository**
  125. .pipe(tagVersion({
  126. prefix: ''
  127. }));
  128. }
  129. gulp.task('patch', [ 'build' ], function () { return inc('patch'); })
  130. gulp.task('minor', [ 'build' ], function () { return inc('minor'); })
  131. gulp.task('major', [ 'build' ], function () { return inc('major'); })
  132. gulp.task('travis', [ 'test' ]);
  133. gulp.task('default', [ 'travis' ]);