gulpfile.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. var gulp = require('gulp'),
  2. concat = require('gulp-concat'),
  3. uglify = require('gulp-uglify'),
  4. util = require('gulp-util'),
  5. jshint = require('gulp-jshint'),
  6. size = require('gulp-size'),
  7. connect = require('gulp-connect'),
  8. replace = require('gulp-replace'),
  9. htmlv = require('gulp-html-validator'),
  10. inquirer = require('inquirer'),
  11. semver = require('semver'),
  12. exec = require('child_process').exec,
  13. fs = require('fs'),
  14. package = require('./package.json'),
  15. bower = require('./bower.json');
  16. var srcDir = './src/';
  17. /*
  18. * Usage : gulp build --types=Bar,Line,Doughnut
  19. * Output: - A built Chart.js file with Core and types Bar, Line and Doughnut concatenated together
  20. * - A minified version of this code, in Chart.min.js
  21. */
  22. gulp.task('build', function(){
  23. // Default to all of the chart types, with Chart.Core first
  24. var srcFiles = [FileName('Core')],
  25. isCustom = !!(util.env.types),
  26. outputDir = (isCustom) ? 'custom' : '.';
  27. if (isCustom){
  28. util.env.types.split(',').forEach(function(type){ return srcFiles.push(FileName(type))});
  29. }
  30. else{
  31. // Seems gulp-concat remove duplicates - nice!
  32. // So we can use this to sort out dependency order - aka include Core first!
  33. srcFiles.push(srcDir+'*');
  34. }
  35. return gulp.src(srcFiles)
  36. .pipe(concat('Chart.js'))
  37. .pipe(replace('{{ version }}', package.version))
  38. .pipe(gulp.dest(outputDir))
  39. .pipe(uglify({preserveComments:'some'}))
  40. .pipe(concat('Chart.min.js'))
  41. .pipe(gulp.dest(outputDir));
  42. function FileName(moduleName){
  43. return srcDir+'Chart.'+moduleName+'.js';
  44. };
  45. });
  46. /*
  47. * Usage : gulp bump
  48. * Prompts: Version increment to bump
  49. * Output: - New version number written into package.json & bower.json
  50. */
  51. gulp.task('bump', function(complete){
  52. util.log('Current version:', util.colors.cyan(package.version));
  53. var choices = ['major', 'premajor', 'minor', 'preminor', 'patch', 'prepatch', 'prerelease'].map(function(versionType){
  54. return versionType + ' (v' + semver.inc(package.version, versionType) + ')';
  55. });
  56. inquirer.prompt({
  57. type: 'list',
  58. name: 'version',
  59. message: 'What version update would you like?',
  60. choices: choices
  61. }, function(res){
  62. var increment = res.version.split(' ')[0],
  63. newVersion = semver.inc(package.version, increment);
  64. // Set the new versions into the bower/package object
  65. package.version = newVersion;
  66. bower.version = newVersion;
  67. // Write these to their own files, then build the output
  68. fs.writeFileSync('package.json', JSON.stringify(package, null, 2));
  69. fs.writeFileSync('bower.json', JSON.stringify(bower, null, 2));
  70. complete();
  71. });
  72. });
  73. gulp.task('release', ['build'], function(){
  74. exec('git tag -a v' + package.version);
  75. });
  76. gulp.task('jshint', function(){
  77. return gulp.src(srcDir + '*.js')
  78. .pipe(jshint())
  79. .pipe(jshint.reporter('default'));
  80. });
  81. gulp.task('valid', function(){
  82. return gulp.src('samples/*.html')
  83. .pipe(htmlv());
  84. });
  85. gulp.task('library-size', function(){
  86. return gulp.src('Chart.min.js')
  87. .pipe(size({
  88. gzip: true
  89. }));
  90. });
  91. gulp.task('module-sizes', function(){
  92. return gulp.src(srcDir + '*.js')
  93. .pipe(uglify({preserveComments:'some'}))
  94. .pipe(size({
  95. showFiles: true,
  96. gzip: true
  97. }))
  98. });
  99. gulp.task('watch', function(){
  100. gulp.watch('./src/*', ['build']);
  101. });
  102. gulp.task('test', ['jshint', 'valid']);
  103. gulp.task('size', ['library-size', 'module-sizes']);
  104. gulp.task('default', ['build', 'watch']);
  105. gulp.task('server', function(){
  106. connect.server({
  107. port: 8000
  108. });
  109. });
  110. // Convenience task for opening the project straight from the command line
  111. gulp.task('_open', function(){
  112. exec('open http://localhost:8000');
  113. exec('subl .');
  114. });
  115. gulp.task('dev', ['server', 'default']);