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