-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
225 lines (200 loc) · 5.66 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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
const gulp = require('gulp');
const gutil = require('gulp-util');
const mocha = require('gulp-mocha');
const istanbul = require('gulp-istanbul');
const eslint = require('gulp-eslint');
const browserify = require('browserify');
const babelify = require('babelify');
const babel = require('babel-core/register');
const source = require('vinyl-source-stream');
const buffer = require('vinyl-buffer');
const exit = require('gulp-exit');
const insert = require('gulp-insert');
const runSequence = require('run-sequence');
const del = require('del');
const fs = require('fs');
const rename = require('gulp-rename');
const uglify = require('gulp-uglify');
const cheerio = require('cheerio');
const tap = require('gulp-tap');
const derequire = require('gulp-derequire');
const isparta = require('isparta');
const pkg = require('./package');
var cp = require('child_process');
var jsdoc = 'node_modules/jsdoc/jsdoc.js';
const product = {
source: {
name: pkg.name + '.js',
lib: 'src/*.js'
},
packaged: {
dir: 'dist',
name: pkg.name + '.js',
minified: pkg.name + '.min.js'
},
bundled: {
dir: 'build',
name: pkg.name + '-bundle.js'
},
license: 'LICENSE'
};
const tests = {
lint: {
files: [
'src/*.js',
'!src/utils.js',
'!src/jsondiff.js',
'gulpfile.js'
]
},
unit: {
files: 'test/unit/spec/*.js',
index: 'test/unit/index.js'
},
integration: {
files: 'test/integration/**/*.js',
index: 'test/integration/index.js'
}
};
const coverage = {
dir: product.packaged.dir + '/coverage'
};
const docs = {
dir: product.packaged.dir + '/docs',
conf: 'jsdoc.json',
files: [
'./src/client.js'
],
publicConstructors: ['NotificationsClient'],
};
gulp.task('lint', function() {
return gulp.src(tests.lint.files)
.pipe(eslint())
.pipe(eslint.format())
.pipe(eslint.failAfterError());
});
gulp.task('istanbul-setup', function() {
return gulp.src([product.source.lib])
.pipe(istanbul({
instrumenter: isparta.Instrumenter,
includeUntested: true
}))
.pipe(istanbul.hookRequire());
});
gulp.task('unit-test', ['istanbul-setup'], function() {
return gulp.src(tests.unit.index, { read: false })
.pipe(mocha({
compilers: { js: babel },
reporter: 'spec'
}))
.pipe(istanbul.writeReports({
dir: coverage.dir,
reporters: ['cobertura', 'lcov', 'text'],
reportOpts: { dir: coverage.dir }
}));
});
gulp.task('integration-test', function() {
return gulp.src(tests.integration.index, { read: false })
.pipe(mocha({ reporter: 'spec', timeout: 5000 }))
.pipe(exit());
});
gulp.task('bundle', function(done) {
browserify({ debug: false, standalone: 'Twilio.Notification.Client', entries: ['./src/index.js'] })
.exclude('ws')
.transform(babelify, {
global: true,
ignore: /\/node_modules\/(?!twilio-transport|twilsock\/)/
})
.bundle()
.pipe(source(product.bundled.name))
.pipe(buffer())
.pipe(derequire())
.pipe(gulp.dest(product.bundled.dir))
.once('end', done)
.once('error', exit);
});
gulp.task('license', function() {
var licenseContents = fs.readFileSync(product.license);
return gulp.src(product.bundled.dir + '/' + product.bundled.name)
.pipe(insert.prepend(
'/* ' + pkg.name + '.js ' + pkg.version + '\n'
+ licenseContents
+ '*/\n\n'
))
.pipe(rename(product.packaged.name))
.pipe(gulp.dest(product.packaged.dir));
});
gulp.task('minify', function() {
return gulp.src(product.packaged.dir + '/' + product.packaged.name)
.pipe(uglify({
output: {
ascii_only: true // eslint-disable-line camelcase
},
preserveComments: 'license'
}).on('error', gutil.log))
.pipe(rename(product.packaged.minified))
.pipe(gulp.dest(product.packaged.dir));
});
gulp.task('build', function(done) {
runSequence('bundle', 'license', done);
});
gulp.task('package', function(done) {
runSequence('minify', done);
});
gulp.task('generate-doc', function(cb) {
cp.exec(['node', jsdoc,
'-d', docs.dir,
'-c', docs.conf,
docs.files.join(' '),
'./README.md',
'-t ./node_modules/ink-docstrap/template'].join(' '), cb);
});
gulp.task('prettify-doc', function() {
return gulp.src(docs.dir + '/*.html')
.pipe(tap(function(file) {
var $ = cheerio.load(file.contents.toString());
var filename = file.path.slice(file.base.length);
var className = filename.split('.html')[0];
var div;
// Prefix public constructors.
if (docs.publicConstructors.indexOf(className) > -1) {
div = $('.container-overview');
var name = $('h4.name', div);
name.html(name.html().replace(/new /, 'new <span style="color: #999">Twilio.Notification.</span>'));
}
// Remove private constructors.
if (docs.privateConstructors.indexOf(className) > -1) {
div = $('.container-overview');
$('h2', div).remove();
$('h4.name', div).remove();
$('div.description', div).remove();
$('h5:contains(Parameters:)', div).remove();
$('table.params', div).remove();
}
file.contents = new Buffer($.html());
return file;
}))
.pipe(gulp.dest(docs.dir));
});
gulp.task('doc', function(done) {
runSequence('generate-doc', 'prettify-doc', done);
});
gulp.task('clean', function() {
return del([
product.packaged.dir + '/' + product.packaged.name,
product.packaged.dir + '/' + product.packaged.minified,
docs.dir,
product.bundled.dir + '/' + product.bundled.name
]);
});
gulp.task('default', function(done) {
runSequence(
'clean',
'lint',
'unit-test',
'build',
'package',
'doc',
done
);
});