-
Notifications
You must be signed in to change notification settings - Fork 1
/
gulpfile.js
160 lines (149 loc) · 4.33 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
'use strict';
const gulp = require('gulp'),
gulpJsdoc2md = require('gulp-jsdoc-to-markdown'),
rename = require('gulp-rename'),
istanbul = require('gulp-istanbul'),
istanbulHarmony = require('istanbul'),
mocha = require('gulp-mocha'),
eslint = require('gulp-eslint'),
git = require('gulp-git');
const files = ['*.js', '!./gulpfile.js', '!node_modules/**', '!test/**'],
docs = ['README.md', 'docs/**/*.md'],
tests = ['test/**/*.js'];
const JobNumber = process.env.TRAVIS_JOB_NUMBER,
runDocs = !JobNumber || /[.]1$/.test(JobNumber);
/**
* Pull git branch locally (solves detached head issue in CI)
*/
gulp.task('gitBranch', (done) => {
let complete = false;
const branch = process.env.TRAVIS_BRANCH;
// Abort(successfully) early if not running in CI
if (!(JobNumber && runDocs && branch)) {
return done();
}
git.checkout(branch, () => {
// Make sure we have full log history.
git.pull('origin', branch, {}, () => {
if (!complete) {
done();
}
complete = true;
});
});
});
/**
* Generate API documentation for all js files, place markup in the correct folder for readthedocs.org
*/
gulp.task('docs', ['gitBranch'], (done) => {
// Abort(successfully) early if running in CI and not job #1
if (!runDocs) {
return done();
}
gulp.src(files)
.pipe(gulpJsdoc2md({}))
.on('error', done)
.pipe(rename((path) => {
path.extname = '.md';
}))
.pipe(gulp.dest('docs/api'))
.on('finish', done);
});
/**
* Run all js files through eslint and report status.
*/
gulp.task('lintCore', () => {
return gulp.src(files)
.pipe(eslint())
.pipe(eslint.format())
.pipe(eslint.failAfterError());
});
/**
* Run all tests through eslint and report status.
*/
gulp.task('lintTests', () => {
return gulp.src(tests)
.pipe(eslint())
.pipe(eslint.format())
.pipe(eslint.failAfterError());
});
/**
* Set git username/email to CI user
*/
gulp.task('gitConfig', (done) => {
// Abort(successfully) early if not running in CI
if (!JobNumber) {
return done();
}
git.exec({
args: 'config user.name "Travis-CI"'
}, () => {
git.exec({
args: 'config user.email "Travis-CI@servercooties.com"'
}, () => {
done();
});
});
});
/**
* Commit generated documentation to be picked up by readthedocs.org
*
* Add CI tag to commit to prevent CI jobs from being created by checking in docs
*/
gulp.task('commitDocs', ['gitConfig'], (done) => {
gulp.src(docs)
.pipe(git.add())
.pipe(git.commit('Automatically push updated documentation [ci skip]'))
.on('error', () => 0)
.on('finish', done);
});
/**
* Commit and push docs to github to be picked up by readthedocs.org
*/
gulp.task('pushDocs', ['gitConfig', 'commitDocs'], (done) => {
//Abort(successfully) early if running in CI and not job #1
if (!runDocs) {
return done();
}
git.addRemote('github', 'https://github.com/RaceProUK/SockBot-Markov.git', (e) => {
if (e) {
done();
} else {
git.push('github', 'HEAD', {
args: ['-q']
}, () => {
done();
});
}
});
});
/**
* Run code coverage instrumented tests
*/
gulp.task('test', ['lintCore', 'lintTests'], (done) => {
gulp.src(files)
// Instrument code files with istanbulHarmony
.pipe(istanbul({
instrumenter: istanbulHarmony.Instrumenter,
includeUntested: true
}))
// hook require function for complete code coverage
.pipe(istanbul.hookRequire())
.on('finish', () => {
// Run all tests
gulp.src(tests)
.pipe(mocha({
reporter: 'dot'
}))
.on('error', done)
// Write code coverage reports
.pipe(istanbul.writeReports())
.on('finish', done);
});
});
// Meta tasks
gulp.task('buildDocs', ['docs'], () => 0);
gulp.task('preBuild', ['buildDocs'], () => 0);
gulp.task('postBuild', ['pushDocs'], () => 0);
gulp.task('default', ['lint'], () => 0);
gulp.task('lint', ['lintCore', 'lintTests'], () => 0);