Project

General

Profile

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

    
17
var srcDir = './src/';
18
/*
19
 *	Usage : gulp build --types=Bar,Line,Doughnut
20
 *	Output: - A built Chart.js file with Core and types Bar, Line and Doughnut concatenated together
21
 *			- A minified version of this code, in Chart.min.js
22
 */
23

    
24
gulp.task('build', function(){
25

    
26
	// Default to all of the chart types, with Chart.Core first
27
	var srcFiles = [FileName('Core')],
28
		isCustom = !!(util.env.types),
29
		outputDir = (isCustom) ? 'custom' : '.';
30
	if (isCustom){
31
		util.env.types.split(',').forEach(function(type){ return srcFiles.push(FileName(type))});
32
	}
33
	else{
34
		// Seems gulp-concat remove duplicates - nice!
35
		// So we can use this to sort out dependency order - aka include Core first!
36
		srcFiles.push(srcDir+'*');
37
	}
38

    
39
	return gulp.src(srcFiles)
40
		.pipe(concat('Chart.js'))
41
		.pipe(replace('{{ version }}', package.version))
42
		.pipe(gulp.dest(outputDir))
43
		.pipe(uglify({preserveComments:'some'}))
44
		.pipe(concat('Chart.min.js'))
45
		.pipe(gulp.dest(outputDir));
46

    
47
	function FileName(moduleName){
48
		return srcDir+'Chart.'+moduleName+'.js';
49
	};
50
});
51

    
52
/*
53
 *	Usage : gulp bump
54
 *	Prompts: Version increment to bump
55
 *	Output: - New version number written into package.json & bower.json
56
 */
57

    
58
gulp.task('bump', function(complete){
59
	util.log('Current version:', util.colors.cyan(package.version));
60
	var choices = ['major', 'premajor', 'minor', 'preminor', 'patch', 'prepatch', 'prerelease'].map(function(versionType){
61
		return versionType + ' (v' + semver.inc(package.version, versionType) + ')';
62
	});
63
	inquirer.prompt({
64
		type: 'list',
65
		name: 'version',
66
		message: 'What version update would you like?',
67
		choices: choices
68
	}, function(res){
69
		var increment = res.version.split(' ')[0],
70
			newVersion = semver.inc(package.version, increment);
71

    
72
		// Set the new versions into the bower/package object
73
		package.version = newVersion;
74
		bower.version = newVersion;
75

    
76
		// Write these to their own files, then build the output
77
		fs.writeFileSync('package.json', JSON.stringify(package, null, 2));
78
		fs.writeFileSync('bower.json', JSON.stringify(bower, null, 2));
79

    
80
		complete();
81
	});
82
});
83

    
84
gulp.task('release', ['build'], function(){
85
	exec('git tag -a v' + package.version);
86
});
87

    
88
gulp.task('jshint', function(){
89
	return gulp.src(srcDir + '*.js')
90
		.pipe(jshint())
91
		.pipe(jshint.reporter('default'));
92
});
93

    
94
gulp.task('valid', function(){
95
	return gulp.src('samples/*.html')
96
    .pipe(htmlv());
97
});
98

    
99
gulp.task('library-size', function(){
100
	return gulp.src('Chart.min.js')
101
		.pipe(size({
102
			gzip: true
103
		}));
104
});
105

    
106
gulp.task('module-sizes', function(){
107
	return gulp.src(srcDir + '*.js')
108
	.pipe(uglify({preserveComments:'some'}))
109
	.pipe(size({
110
		showFiles: true,
111
		gzip: true
112
	}))
113
});
114

    
115
gulp.task('watch', function(){
116
	gulp.watch('./src/*', ['build']);
117
});
118

    
119
gulp.task('test', ['jshint', 'valid']);
120

    
121
gulp.task('size', ['library-size', 'module-sizes']);
122

    
123
gulp.task('default', ['build', 'watch']);
124

    
125
gulp.task('server', function(){
126
	connect.server({
127
		port: 8000
128
	});
129
});
130

    
131
// Convenience task for opening the project straight from the command line
132
gulp.task('_open', function(){
133
	exec('open http://localhost:8000');
134
	exec('subl .');
135
});
136

    
137
gulp.task('dev', ['server', 'default']);
(10-10/11)