Gulpfile.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. var gulp = require('gulp');
  2. var plugin = require('gulp-load-plugins')();
  3. var fs = require('fs');
  4. var exec = require('child_process').exec;
  5. var argv = require('minimist')(process.argv.slice(2));
  6. var path = {
  7. rootdir: './',
  8. lib: ['./lib/**/*.js'],
  9. libdir: './lib/',
  10. test: ['./test/**/*.js'],
  11. testdir: './test/',
  12. build: ['package.json', 'component.json', 'bower.json', 'README.md', 'speakingurl.min.js'],
  13. json: ['package.json', 'component.json', 'bower.json'],
  14. readme: './README.md',
  15. target: './speakingurl.min.js'
  16. };
  17. var banner = ['/**',
  18. ' * <%= pkg.name %>',
  19. ' * @version v<%= pkg.version %>',
  20. ' * @link <%= pkg.homepage %>',
  21. ' * @license <%= pkg.licenses[0].type %>',
  22. ' * @author <%= pkg.author.name %>',
  23. ' */'
  24. ].join('\n');
  25. gulp.task('beautify', function (done) {
  26. gulp.src(path.lib)
  27. .pipe(plugin.jsbeautifier({
  28. config: '.jsbeautifyrc',
  29. mode: 'VERIFY_AND_WRITE'
  30. }))
  31. .pipe(gulp.dest(path.libdir));
  32. gulp.src(path.test)
  33. .pipe(plugin.jsbeautifier({
  34. config: '.jsbeautifyrc',
  35. mode: 'VERIFY_AND_WRITE'
  36. }))
  37. .pipe(gulp.dest(path.testdir));
  38. gulp.src(path.json)
  39. .pipe(plugin.jsbeautifier({
  40. config: '.jsbeautifyrc',
  41. mode: 'VERIFY_AND_WRITE'
  42. }))
  43. .pipe(gulp.dest(path.rootdir));
  44. done();
  45. });
  46. gulp.task('test', function () {
  47. return gulp.src(path.test, {
  48. read: false
  49. })
  50. .pipe(plugin.mocha({
  51. reporter: 'spec',
  52. globals: {
  53. should: require('should')
  54. }
  55. }));
  56. });
  57. gulp.task('jshint', ['beautify'], function () {
  58. return gulp.src(path.lib, path.json)
  59. .pipe(plugin.jshint('.jshintrc'), {
  60. verbose: true
  61. })
  62. .pipe(plugin.jshint.reporter('jshint-stylish'));
  63. });
  64. gulp.task('uglify', ['jshint'], function (done) {
  65. var pkg = JSON.parse(fs.readFileSync('./package.json', 'utf-8'));
  66. return gulp.src(path.lib)
  67. .pipe(plugin.uglify())
  68. .pipe(plugin.header(banner, {
  69. pkg: pkg
  70. }))
  71. .pipe(plugin.rename(path.target))
  72. .pipe(gulp.dest(path.rootdir));
  73. });
  74. gulp.task('bumpup', ['bumpup-version'], function () {
  75. var pkg = JSON.parse(fs.readFileSync('./package.json', 'utf-8'));
  76. // insert newsest version
  77. return gulp.src(path.readme)
  78. .pipe(plugin.replace(
  79. /cdnjs.cloudflare.com\/ajax\/libs\/speakingurl\/\d{1,1}\.\d{1,2}\.\d{1,2}\/speakingurl.min.js/g,
  80. 'cdnjs.cloudflare.com/ajax/libs/speakingurl/' + pkg.version + '/speakingurl.min.js'))
  81. .pipe(plugin.replace(
  82. /cdn.jsdelivr.net\/speakingurl\/\d{1,1}\.\d{1,2}\.\d{1,2}\/speakingurl.min.js/g,
  83. 'cdn.jsdelivr.net/speakingurl/' + pkg.version + '/speakingurl.min.js'))
  84. .pipe(gulp.dest(path.rootdir));
  85. });
  86. gulp.task('bumpup-version', function () {
  87. return gulp.src(path.json)
  88. .pipe(plugin.bump({
  89. type: argv.major ? 'major' : (argv.minor ? 'minor' : 'patch')
  90. }))
  91. .pipe(gulp.dest(path.rootdir));
  92. });
  93. gulp.task('release', function (done) {
  94. var pkg = JSON.parse(fs.readFileSync('./package.json', 'utf-8'));
  95. var tag = 'v' + pkg.version;
  96. var message = 'Release ' + tag;
  97. var execute = [
  98. 'npm rm speakingurl -g',
  99. 'npm install . -g',
  100. 'git add .',
  101. 'git commit -m "Release ' + tag + '"',
  102. 'git tag ' + tag + ' -m "Release ' + tag + '"',
  103. 'git push -u origin master',
  104. 'git push -u origin master --tags',
  105. 'npm publish'
  106. ].join('\n');
  107. exec(execute, done());
  108. });
  109. gulp.task('watch', function () {
  110. gulp.watch([path.json, path.lib], ['jshint', 'test']);
  111. });
  112. gulp.task('default', ['test', 'jshint', 'uglify']);