1 | var gulp = require('gulp'),
|
2 | gutil = require('gulp-util'),
|
3 | mocha = require('gulp-mocha'),
|
4 | mochaPhantomJS = require('gulp-mocha-phantomjs'),
|
5 | webserver = require('gulp-webserver'),
|
6 | del = require('del'),
|
7 | source = require('vinyl-source-stream'),
|
8 | through = require('through2'),
|
9 | browserify = require('browserify'),
|
10 | derequire = require('gulp-derequire'),
|
11 | config = {
|
12 | bundle: {
|
13 | standalone: 'powerAssertFormatter',
|
14 | srcFile: './index.js',
|
15 | destDir: './build',
|
16 | destName: 'power-assert-formatter.js'
|
17 | },
|
18 | coverage: {
|
19 | filename: 'coverage.lcov'
|
20 | },
|
21 | test: {
|
22 | base: './test/',
|
23 | pattern: '**/*_test.js',
|
24 | amd: 'test/test-amd.html',
|
25 | browser: 'test/test-browser.html'
|
26 | }
|
27 | };
|
28 |
|
29 | function captureStdout (filespec) {
|
30 | var orig, log = '';
|
31 | function spy (str) {
|
32 | log += str;
|
33 | }
|
34 | function pass (file, encoding, callback) {
|
35 | this.push(file);
|
36 | callback();
|
37 | }
|
38 | return {
|
39 | start: through.obj(pass, function (callback) {
|
40 | orig = process.stdout.write;
|
41 | process.stdout.write = spy;
|
42 | callback();
|
43 | }),
|
44 | finish: through.obj(pass, function (callback) {
|
45 | var file = new gutil.File(filespec);
|
46 | file.contents = new Buffer(log);
|
47 | this.push(file);
|
48 | process.stdout.write = orig;
|
49 | log = '';
|
50 | orig = null;
|
51 | callback();
|
52 | })
|
53 | };
|
54 | }
|
55 |
|
56 | function runMochaWithBlanket() {
|
57 | var blanket = require('./coverage/blanket'),
|
58 | capt = captureStdout({
|
59 | cwd: __dirname,
|
60 | base: __dirname,
|
61 | path: __dirname + '/' + config.coverage.filename
|
62 | });
|
63 | return gulp
|
64 | .src(config.test.base + config.test.pattern, {read: false})
|
65 | .pipe(capt.start)
|
66 | .pipe(mocha({
|
67 | ui: 'tdd',
|
68 | reporter: 'mocha-lcov-reporter'
|
69 | }))
|
70 | .pipe(capt.finish)
|
71 | .pipe(gulp.dest('.'))
|
72 | .on('error', gutil.log);
|
73 | }
|
74 |
|
75 | function runMochaSimply() {
|
76 | return gulp
|
77 | .src(config.test.base + config.test.pattern, {read: false})
|
78 | .pipe(mocha({
|
79 | ui: 'tdd',
|
80 | reporter: 'dot'
|
81 | }))
|
82 | .on('error', gutil.log);
|
83 | }
|
84 |
|
85 | gulp.task('webserver', function() {
|
86 | gulp.src(__dirname)
|
87 | .pipe(webserver({
|
88 | port: 9001,
|
89 | directoryListing: true
|
90 | }));
|
91 | });
|
92 |
|
93 | gulp.task('watch', function () {
|
94 | gulp.watch('index.js', runMochaSimply);
|
95 | gulp.watch('{lib,test}/**/*.js', runMochaSimply);
|
96 | runMochaSimply();
|
97 | });
|
98 |
|
99 | gulp.task('clean_bundle', function (done) {
|
100 | del([config.bundle.destDir], done);
|
101 | });
|
102 |
|
103 | gulp.task('clean_coverage', function (done) {
|
104 | del([config.coverage.filename], done);
|
105 | });
|
106 |
|
107 | gulp.task('bundle', ['clean_bundle'], function() {
|
108 | var bundleStream = browserify({entries: config.bundle.srcFile, standalone: config.bundle.standalone}).bundle();
|
109 | return bundleStream
|
110 | .pipe(source(config.bundle.destName))
|
111 | .pipe(derequire())
|
112 | .pipe(gulp.dest(config.bundle.destDir));
|
113 | });
|
114 |
|
115 | gulp.task('unit', function () {
|
116 | return runMochaSimply();
|
117 | });
|
118 |
|
119 | gulp.task('coverage', ['clean_coverage'], function () {
|
120 | return runMochaWithBlanket();
|
121 | });
|
122 |
|
123 | gulp.task('test_amd', ['bundle'], function () {
|
124 | return gulp
|
125 | .src(config.test.amd)
|
126 | .pipe(mochaPhantomJS({reporter: 'dot'}));
|
127 | });
|
128 |
|
129 | gulp.task('test_browser', ['bundle'], function () {
|
130 | return gulp
|
131 | .src(config.test.browser)
|
132 | .pipe(mochaPhantomJS({reporter: 'dot'}));
|
133 | });
|
134 |
|
135 | gulp.task('clean', ['clean_coverage', 'clean_bundle']);
|
136 |
|
137 | gulp.task('test', ['unit','test_browser','test_amd']);
|