-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
153 lines (129 loc) · 3.7 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
'use strict'; // eslint-disable-line strict
require('babel/register');
const babel = require('gulp-babel');
const del = require('del');
const eslint = require('gulp-eslint');
const fs = require('fs');
const gulp = require('gulp');
const gulpif = require('gulp-if');
const isparta = require('isparta');
const istanbul = require('gulp-istanbul');
const mocha = require('gulp-mocha');
const path = require('path');
const pkg = require('./package.json');
const Promise = require('bluebird');
const readmeToMan = require('readme-to-man-page');
const sequence = require('gulp-sequence');
const sourcemaps = require('gulp-sourcemaps');
Promise.promisifyAll(fs);
/*
* Configuration
*/
const config = require('./config');
/*
* Helpers
*/
function mkdirp(dir) {
return fs.statAsync(dir)
.then(stat => {
return stat.isDirectory ? Promise.resolve() : Promise.reject(Error('Could not mkdirp: path already exists'));
})
.catch(err => fs.mkdirAsync(dir))
.catch(err => { if (err.errno !== -17) throw err; /* already created */ });
}
function transform(src, output, transformers, done) {
fs.readFile(src, 'utf8', (readErr, data) => {
if (readErr) {
return done(readErr);
}
let transformed = data;
for (const transformer of transformers) {
transformed = transformer(transformed);
}
fs.writeFile(output, transformed, (writeErr) => {
if (writeErr) {
return done(writeErr);
}
return done();
});
});
}
/*
* Task definitions
*/
/* Meta tasks */
gulp.task('default', sequence('build', ['lint', 'test', 'watch']));
gulp.task('build', sequence('clean', ['buildLib', 'buildTest', 'buildFixture', 'buildMan']));
gulp.task('buildLib', sequence('buildJs', 'buildCoverage'));
/* Implementations */
gulp.task('clean', () => {
return del(config.outputs);
});
gulp.task('buildJs', () => {
return gulp.src(config.js.src)
.pipe(gulpif(config.sourcemap, sourcemaps.init()))
.pipe(babel(config.babel))
.pipe(gulpif(config.sourcemap, sourcemaps.write('.')))
.pipe(gulp.dest(config.js.output));
});
gulp.task('buildCoverage', () => {
return gulp.src(config.js.coverage)
.pipe(istanbul({instrumenter: isparta.Instrumenter}))
.pipe(istanbul.hookRequire())
});
gulp.task('buildTest', () => {
return gulp.src(config.test.src)
.pipe(gulpif(config.sourcemap, sourcemaps.init()))
.pipe(babel(config.babel))
.pipe(gulpif(config.sourcemap, sourcemaps.write('.')))
.pipe(gulp.dest(config.test.output));
});
gulp.task('buildFixture', () => {
return gulp.src(config.fixture.src)
.pipe(gulp.dest(config.fixture.output));
});
gulp.task('buildMan', (done) => {
mkdirp('man').then(() => {
transform(
config.man.src,
config.man.output,
[
(data) => {
return readmeToMan(data, {
name: pkg.name,
version: pkg.version,
description: pkg.description,
section: 1,
manual: `Git Streaker ${pkg.version}`,
});
},
],
done
);
});
});
gulp.task('lint', () => {
return gulp.src(config.js.src)
.pipe(gulpif(config.linting, eslint()))
.pipe(gulpif(config.linting, eslint.format()));
});
gulp.task('test', ['buildCoverage'], () => {
mkdirp(config.build.output);
return gulp.src(config.test.tests, {read: false})
.pipe(mocha({
ui: 'bdd',
reporter: 'spec',
require: [
config.test.bootstrap
]
}))
.pipe(istanbul.writeReports({
dir: config.build.lcov,
reporters: [ 'lcov' ],
}));
});
gulp.task('watch', () => {
gulp.watch(config.js.src, ['buildJs', 'buildCoverage', 'lint']);
gulp.watch(config.test.src, ['buildTest']);
gulp.watch(config.fixture.src, ['buildFixture']);
});