-
Notifications
You must be signed in to change notification settings - Fork 7
/
gulpfile.js
184 lines (169 loc) · 5.28 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
var gulp = require('gulp');
var gutil = require('gulp-util');
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 derequire = require('gulp-derequire');
var dereserve = require('gulp-dereserve');
var config = {
bundle: {
standalone: 'empower',
srcFile: './index.js',
destDir: './build',
destName: 'empower.js'
},
assert_bundle: {
standalone: 'assert',
require: 'assert',
destDir: './build',
destName: 'assert.js'
},
acorn_es7_plugin_bundle: {
standalone: 'acornEs7Plugin',
require: 'acorn-es7-plugin',
destDir: './build',
destName: 'acorn-es7-plugin.js'
},
escodegen_bundle: {
standalone: 'escodegen',
srcFile: './node_modules/escodegen/escodegen.js',
destDir: './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 BUILDS = ['assert', 'escodegen', 'acorn_es7_plugin'];
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({
ui: 'tdd',
reporter: 'mocha-lcov-reporter',
require: ['babel-core/polyfill']
}))
.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({
ui: 'tdd',
reporter: 'dot',
require: ['babel-core/polyfill']
}))
.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', '{lib,test}/**/*.js'], runMochaSimply);
runMochaSimply();
});
gulp.task('clean_bundle', function () {
del.sync([config.bundle.destDir]);
});
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);
var bundleStream = b.bundle();
return bundleStream
.pipe(source(config.bundle.destName))
.pipe(dereserve())
.pipe(derequire())
.pipe(gulp.dest(config.bundle.destDir));
});
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});
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', BUILDS.map(function (name) { return 'clean_' + name + '_bundle'; }));
gulp.task('build_deps', BUILDS.map(function (name) { return name + '_bundle'; }));
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('clean', ['clean_coverage', 'clean_bundle', 'clean_deps']);
gulp.task('test', ['unit','test_browser','test_amd']);