-
Notifications
You must be signed in to change notification settings - Fork 1
/
gulpfile.js
200 lines (158 loc) · 5.03 KB
/
gulpfile.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/*
* biojs-io-biom
* https://github.com/molbiodiv/biojs-io-biom
*
* Copyright (c) 2015 Markus J. Ankenbrand
* Licensed under the MIT license.
*/
// browserify build config
var buildDir = "build";
var outputFile = "biom";
// packages
var gulp = require('gulp');
// es6 compilation
var babel = require('gulp-babel');
require('babel-core/register');
// browser builds
var browserify = require('browserify');
var watchify = require('watchify')
var uglify = require('gulp-uglify');
// testing
var mocha = require('gulp-mocha');
// coverage
var istanbul = require('gulp-istanbul');
var isparta = require('isparta');
var runSequence = require('run-sequence');
var coveralls = require('gulp-coveralls');
// code style
var jshint = require('gulp-jshint');
// gulp helper
var source = require('vinyl-source-stream'); // converts node streams into vinyl streams
var gzip = require('gulp-gzip');
var rename = require('gulp-rename');
var chmod = require('gulp-chmod');
var streamify = require('gulp-streamify'); // converts streams into buffers (legacy support for old plugins)
// path tools
var fs = require('fs');
var path = require('path');
var join = path.join;
var mkdirp = require('mkdirp');
var del = require('del');
// auto config
var outputFileMin = join(buildDir,outputFile + ".min.js");
var packageConfig = require('./package.json');
// a failing test breaks the whole build chain
gulp.task('build', ['babel', 'build-browser', 'build-browser-gzip']);
gulp.task('default', ['lint','test', 'build']);
gulp.task('lint', function() {
return gulp.src('./src/*.js')
.pipe(jshint())
.pipe(jshint.reporter('default'));
});
gulp.task('coverage:instrument', function () {
return gulp.src(['src/**/*.js'])
// Covering files
.pipe(istanbul({instrumenter: isparta.Instrumenter}))
// Force `require` to return covered files
.pipe(istanbul.hookRequire());
});
gulp.task('test-unit', function () {
return gulp.src('./test/**/*.js', {read: false})
.pipe(mocha({compilers: 'js:babel-core/register',
reporter: 'spec',
useColors: true}))
});
gulp.task('coverage:report', function(done) {
return gulp.src(['src/**/*.js'], {read: false})
.pipe(istanbul.writeReports({
// Istanbul configuration (see https://github.com/SBoudrias/gulp-istanbul#istanbulwritereportsopt)
// ...
}));
});
gulp.task('test', function(done) {
runSequence('coverage:instrument', 'test-unit', 'coverage:report', 'coveralls', done);
});
gulp.task('coveralls', function () {
if (!process.env.CI) {
return;
}
return gulp.src('./coverage/lcov.info')
.pipe(coveralls());
});
gulp.task('test-watch', function() {
gulp.watch(['./src/**/*.js','./lib/**/*.js', './test/**/*.js'], ['test']);
});
// will remove everything in build
gulp.task('clean', function(cb) {
return del([buildDir], cb);
});
// just makes sure that the build dir exists
gulp.task('init', ['clean'], function() {
mkdirp(buildDir, function (err) {
if (err) console.error(err)
});
});
// browserify debug
gulp.task('build-browser',['init'], function() {
var b = browserify({debug: true,hasExports: true});
exposeBundles(b);
return b.bundle()
.pipe(source(outputFile + ".js"))
.pipe(chmod(0o644))
.pipe(gulp.dest(buildDir));
});
// browserify min
gulp.task('build-browser-min',['init'], function() {
var b = browserify({hasExports: true});
exposeBundles(b);
return b.bundle()
.pipe(source(outputFile + ".min.js"))
.pipe(chmod(0o644))
.pipe(streamify(uglify()))
.pipe(gulp.dest(buildDir));
});
gulp.task('build-browser-gzip', ['build-browser-min'], function() {
return gulp.src(outputFileMin)
.pipe(gzip({append: false, gzipOptions: { level: 9 }}))
.pipe(rename(outputFile + ".min.gz.js"))
.pipe(gulp.dest(buildDir));
});
// exposes the main package
// + checks the config whether it should expose other packages
function exposeBundles(b){
b.add("./" + packageConfig.main, {expose: packageConfig.name });
if(packageConfig.sniper !== undefined && packageConfig.sniper.exposed !== undefined){
for(var i=0; i<packageConfig.sniper.exposed.length; i++){
b.require(packageConfig.sniper.exposed[i]);
}
}
}
// watch task for browserify
// watchify has an internal cache -> subsequent builds are faster
gulp.task('watch', function() {
var util = require('gulp-util')
var b = browserify({debug: true,hasExports: true, cache: {}, packageCache: {} });
b.add("./" + packageConfig.main, {expose: packageConfig.name});
// expose other bundles
exposeBundles(b);
function rebundle(ids){
b.bundle()
.on("error", function(error) {
util.log(util.colors.red("Error: "), error);
})
.pipe(source(outputFile + ".js"))
.pipe(chmod(0o644))
.pipe(gulp.dest(buildDir));
}
var watcher = watchify(b);
watcher.on("update", rebundle)
.on("log", function(message) {
util.log("Refreshed:", message);
});
return rebundle();
});
gulp.task('babel', function () {
return gulp.src("src/biojs-io-biom.js")
.pipe(babel())
.pipe(gulp.dest("lib"));
});