-
Notifications
You must be signed in to change notification settings - Fork 3
/
gulpfile.js
209 lines (190 loc) · 6.01 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
'use strict';
var gulp = require('gulp');
var gutil = require('gulp-util');
var jshint = require('gulp-jshint');
var stylish = require('jshint-stylish');
var mocha = require('gulp-mocha');
var mochaPhantomJS = require('gulp-mocha-phantomjs');
var webserver = require('gulp-webserver');
var del = require('del');
var path = require('path');
var source = require('vinyl-source-stream');
var through = require('through2');
var browserify = require('browserify');
var licensify = require('licensify');
var packageJsonVersionify = require('package-json-versionify');
var derequire = require('gulp-derequire');
var dereserve = require('gulp-dereserve');
var config = {
dist: {
destDir: './build',
destName: 'power-assert-formatter.js'
},
jshint: {
src: './lib/**/*.js'
},
bundle: {
standalone: 'powerAssertFormatter',
srcFile: './index.js',
destDir: './local_build',
destName: 'power-assert-formatter.js'
},
assert_bundle: {
standalone: 'assert',
require: 'assert',
destDir: './local_build',
destName: 'assert.js'
},
escodegen_bundle: {
standalone: 'escodegen',
srcFile: './node_modules/escodegen/escodegen.js',
destDir: './local_build',
destName: 'escodegen.js'
},
coverage: {
filename: 'coverage.lcov'
},
test: {
base: './test/',
pattern: '**/*_test.js',
amd: 'test/test-amd.html',
browser: 'test/test-browser.html'
}
};
var LOCAL_BUILDS = ['assert', 'escodegen'];
function captureStdout (filespec) {
var orig, log = '';
function spy (str) {
log += str;
}
function pass (file, encoding, callback) {
this.push(file);
callback();
}
return {
start: through.obj(pass, function (callback) {
orig = process.stdout.write;
process.stdout.write = spy;
callback();
}),
finish: through.obj(pass, function (callback) {
var file = new gutil.File(filespec);
file.contents = new Buffer(log);
this.push(file);
process.stdout.write = orig;
log = '';
orig = null;
callback();
})
};
}
function runMochaWithBlanket() {
var blanket = require('./coverage/blanket');
var capt = captureStdout({
cwd: __dirname,
base: __dirname,
path: __dirname + '/' + config.coverage.filename
});
return gulp
.src(config.test.base + config.test.pattern, {read: false})
.pipe(capt.start)
.pipe(mocha({
timeout: 60000,
ui: 'tdd',
reporter: 'mocha-lcov-reporter'
}))
.pipe(capt.finish)
.pipe(gulp.dest('.'))
.on('error', gutil.log);
}
function runMochaSimply() {
return gulp
.src(config.test.base + config.test.pattern, {read: false})
.pipe(mocha({
timeout: 60000,
ui: 'tdd',
reporter: 'dot'
}))
.on('error', gutil.log);
}
gulp.task('serve', function() {
gulp.src(__dirname)
.pipe(webserver({
port: 9001,
directoryListing: true
}));
});
gulp.task('watch', function () {
gulp.watch('index.js', runMochaSimply);
gulp.watch('{lib,test}/**/*.js', runMochaSimply);
runMochaSimply();
});
gulp.task('clean_bundle', function () {
del.sync([path.join(config.bundle.destDir, config.bundle.destName)]);
});
gulp.task('clean_coverage', function () {
del.sync([config.coverage.filename]);
});
gulp.task('bundle', ['clean_bundle'], function() {
var b = browserify({entries: config.bundle.srcFile, standalone: config.bundle.standalone});
b.plugin(licensify);
b.transform(packageJsonVersionify, {global: true});
var bundleStream = b.bundle();
return bundleStream
.pipe(source(config.bundle.destName))
.pipe(dereserve())
.pipe(derequire())
.pipe(gulp.dest(config.bundle.destDir));
});
gulp.task('unit', function () {
return runMochaSimply();
});
gulp.task('coverage', ['clean_coverage'], function () {
return runMochaWithBlanket();
});
gulp.task('test_amd', ['bundle', 'build_deps'], function () {
return gulp
.src(config.test.amd)
.pipe(mochaPhantomJS({reporter: 'dot'}));
});
gulp.task('test_browser', ['bundle', 'build_deps'], function () {
return gulp
.src(config.test.browser)
.pipe(mochaPhantomJS({reporter: 'dot'}));
});
gulp.task('lint', function() {
return gulp.src(config.jshint.src)
.pipe(jshint())
.pipe(jshint.reporter(stylish));
});
LOCAL_BUILDS.forEach(function (name) {
gulp.task('clean_' + name + '_bundle', function () {
del.sync([path.join(config[name + '_bundle'].destDir, config[name + '_bundle'].destName)]);
});
gulp.task(name + '_bundle', ['clean_' + name + '_bundle'], function() {
var b = browserify({standalone: config[name + '_bundle'].standalone});
b.transform(packageJsonVersionify, {global: true});
if (config[name + '_bundle'].srcFile) {
b.add(config[name + '_bundle'].srcFile);
}
if (config[name + '_bundle'].require) {
b.require(config[name + '_bundle'].require);
}
return b.bundle()
.pipe(source(config[name + '_bundle'].destName))
.pipe(derequire())
.pipe(gulp.dest(config[name + '_bundle'].destDir));
});
});
gulp.task('clean_deps', LOCAL_BUILDS.map(function (name) { return 'clean_' + name + '_bundle'; }));
gulp.task('build_deps', LOCAL_BUILDS.map(function (name) { return name + '_bundle'; }));
gulp.task('clean_dist', function () {
del.sync([config.dist.destDir]);
});
gulp.task('dist', ['clean_dist', 'bundle'], function () {
return gulp
.src(path.join(config.bundle.destDir, config.bundle.destName))
.pipe(gulp.dest(config.dist.destDir));
});
gulp.task('clean', ['clean_dist', 'clean_coverage', 'clean_bundle', 'clean_deps']);
gulp.task('test', ['unit','test_browser','test_amd']);