blob: d32d0074ccaf923021df831bb423cd71264ca77b [file] [log] [blame]
Matteo Scandolo7cd88ba2015-12-16 14:23:08 -08001(function () {
2 'use strict';
3
4 var fs = require('fs');
5 var path = require('path');
6 var gulp = require('gulp');
7 var less = require('gulp-less');
8 var sourcemaps = require('gulp-sourcemaps');
9 var uglify = require('gulp-uglify');
10 var csso = require('gulp-csso');
11 var jshint = require('gulp-jshint');
12 var stylish = require('jshint-stylish');
13 var jscs = require('gulp-jscs');
14 var mocha = require('gulp-spawn-mocha');
15 var tar = require('gulp-tar');
16 var gzip = require('gulp-gzip');
17 var bumper = require('gulp-bump');
18 var git = require('gulp-git');
19 var shell = require('gulp-shell');
20 var rename = require('gulp-rename');
21 var sequence = require('gulp-sequence');
22 var ngAnnotate = require('gulp-ng-annotate');
23 var rimraf = require('gulp-rimraf');
24 var istanbul = require('gulp-istanbul');
25 var istanbulReport = require('gulp-istanbul-report');
26 var mochaPhantomJS = require('gulp-mocha-phantomjs');
27
28 gulp.task('clean', function () {
29 return gulp.src('./dist/*', { read: false })
30 .pipe(rimraf());
31 });
32
33 gulp.task('less', function () {
34 return gulp.src('./*.less')
35 .pipe(less())
36 .pipe(gulp.dest('./dist'));
37 });
38
39 gulp.task('css-min', function () {
40 return gulp.src('./dist/*.css')
41 .pipe(sourcemaps.init())
42 .pipe(csso())
43 .pipe(rename({ suffix: '.min' }))
44 .pipe(sourcemaps.write('./'))
45 .pipe(gulp.dest('./dist'));
46 });
47
48 gulp.task('lint', function () {
49 return gulp.src('**/*.js')
50 .pipe(jshint())
51 .pipe(jshint.reporter(stylish));
52 });
53
54 gulp.task('style', function () {
55 return gulp.src('**/*.js')
56 .pipe(jscs());
57 });
58
59 gulp.task('cover', function () {
60 return gulp.src('angular-chart.js')
61 .pipe(istanbul({ coverageVariable: '__coverage__' }))
62 .pipe(rename('coverage.js'))
63 .pipe(gulp.dest('test/fixtures'));
64 });
65
66 gulp.task('unit', function () {
67 return gulp.src('test/index.html', { read: false })
68 .pipe(mochaPhantomJS({
69 phantomjs: {
70 hooks: 'mocha-phantomjs-istanbul',
71 coverageFile: 'coverage/coverage.json'
72 },
73 reporter: 'spec'
74 }));
75 });
76
77 gulp.task('integration', function () {
78 return gulp.src(path.join('test', 'test.integration.js'), { read: false })
79 .pipe(mocha({ reporter: 'list', timeout: 20000, require: 'test/support/setup.js' }));
80 });
81
82 gulp.task('report', function () {
83 return gulp.src('coverage/coverage.json')
84 .pipe(istanbulReport({ reporters: ['lcov'] }));
85 });
86
87 gulp.task('bump-patch', bump('patch'));
88 gulp.task('bump-minor', bump('minor'));
89 gulp.task('bump-major', bump('major'));
90
91 gulp.task('bower', function () {
92 return gulp.src('./angular-chart.js')
93 .pipe(ngAnnotate({ single_quotes: true }))
94 .pipe(gulp.dest('./dist'));
95 });
96
97 gulp.task('js', ['lint', 'style', 'bower'], function () {
98 return gulp.src('./angular-chart.js')
99 .pipe(rename('angular-chart.min.js'))
100 .pipe(ngAnnotate({ single_quotes: true }))
101 .pipe(sourcemaps.init())
102 .pipe(uglify())
103 .pipe(sourcemaps.write('./'))
104 .pipe(gulp.dest('./dist'));
105 });
106
107 gulp.task('build', function () {
108 return gulp.src(['dist/*', '!./dist/*.tar.gz'])
109 .pipe(tar('angular-chart.js.tar'))
110 .pipe(gzip({ gzipOptions: { level: 9 } }))
111 .pipe(gulp.dest('dist/'));
112 });
113
114 gulp.task('update', function (cb) {
115 fs.readFile('./examples/charts.template.html', 'utf8', function (err, file) {
116 if (err) return cb(err);
117 file = file.replace('<!-- version -->', version());
118 fs.writeFile('./examples/charts.html', file, cb);
119 });
120 });
121
122 gulp.task('git-commit', function () {
123 var v = version();
124 gulp.src(['./dist/*', './package.json', './bower.json', './examples/charts.html', './test/fixtures/coverage.js'])
125 .pipe(git.add())
126 .pipe(git.commit(v))
127 ;
128 });
129
130 gulp.task('git-push', function (cb) {
131 var v = version();
132 git.push('origin', 'master', function (err) {
133 if (err) return cb(err);
134 git.tag(v, v, function (err) {
135 if (err) return cb(err);
136 git.push('origin', 'master', {args: '--tags' }, cb);
137 });
138 });
139 });
140
141 gulp.task('npm', shell.task([
142 'npm publish'
143 ]));
144
145 gulp.task('watch', function () {
146 gulp.watch('./*.js', ['js']);
147 gulp.watch('./*.less', ['less']);
148 return true;
149 });
150
151 function bump (level) {
152 return function () {
153 return gulp.src(['./package.json', './bower.json'])
154 .pipe(bumper({type: level}))
155 .pipe(gulp.dest('./'));
156 };
157 }
158
159 function version () {
160 return JSON.parse(fs.readFileSync('package.json', 'utf8')).version;
161 }
162
163 gulp.task('default', sequence('check', 'assets'));
164 gulp.task('assets', sequence('clean', ['less', 'js'], 'css-min', 'build'));
165 gulp.task('test', sequence('cover', 'unit', 'integration', 'report'));
166 gulp.task('check', sequence(['lint', 'style'], 'test'));
167 gulp.task('deploy-patch', sequence('default', 'bump-patch', 'update', 'git-commit', 'git-push', 'npm'));
168 gulp.task('deploy-minor', sequence('default', 'bump-minor', 'update', 'git-commit', 'git-push', 'npm'));
169 gulp.task('deploy-major', sequence('default', 'bump-patch', 'update', 'git-commit', 'git-push', 'npm'));
170
171})();