-
Notifications
You must be signed in to change notification settings - Fork 0
/
Gulpfile.js
134 lines (109 loc) · 3.23 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
/**
* Gulpfile.js
*
* @author Villem Alango <villem.alango@gmail.com>
*/
/* eslint no-console:0 */
/* globals __dirname */
'use strict';
var JS = '*.js', S = '**';
// Directories we want to process ... eslint or otherwise
var JSDIRS = ['lib', 'tests'];
// eslint rule file name
var LRULES = '.eslintrc';
var lintOptions = {useEslintrc: true};
var gulp = require('gulp')
, path = require('path')
, log = console.log.bind(console, '#')
, runners = []
, sep = path.sep
, homeDir = __dirname
, allJs = [S, JS].join(sep)
;
var linter;
var jsPaths = JSDIRS.map(function js(p) {
return [homeDir, p, allJs].join(sep);
}).concat(homeDir + sep + JS);
function runLinter(fspec) {
return gulp.src(fspec)
.pipe(linter(lintOptions))
.pipe(linter.formatEach());
}
function watchSourcesFor(runner) {
if (runners.push(runner) === 1) {
// Run gulp.watch only on the 1-st call.
gulp.watch(jsPaths, function b(ev) {
var fn, i;
if (ev.type !== 'deleted') {
log('changed:', ev.path); // Give user a hint we've reacted.
for (i = 0; (fn = runners[i]) !== undefined; i += 1) {
console.log(ev.path);
fn(ev.path);
}
}
});
}
}
function watchRulefiles(name, runner) {
var rulePaths = JSDIRS.map(function ru(p) {
return [homeDir, p, '**', name].join(sep);
}).concat([homeDir, name].join(sep));
gulp.watch(rulePaths, function d(ev) {
var p = ev.path;
p = p.substr(0, p.length - name.length - 1);
log('changed:' + name, p);
runner(p === homeDir ? jsPaths : p + sep + allJs);
});
}
function tester(spec) {
var GJC = require('gulp-jsx-coverage')
, jasmine = require('gulp-jasmine')
, GJCoptions = {
istanbul: {
coverageVariable: '__MY_TEST_COVERAGE__', // skiping this result in exception
includeUntested: true,
exclude: /node_modules|tests/
},
coverage: {
reporters: ['lcov'],
directory: 'reports/coverage/node'
}
}
, jasmineOptions = {verbose: true, includeStackTrace: true}
;
GJC.initModuleLoaderHack(GJCoptions);
return gulp.src(spec)
.pipe(jasmine(jasmineOptions))
.on('end', GJC.collectIstanbulCoverage(GJCoptions));
}
// ================== Tasks ===================
function runAndWatch(runner, rules) {
jsPaths.forEach(function f(p) {
runner(p);
});
watchSourcesFor(runner);
watchRulefiles(rules, runner);
}
/*
* Linter task.
*/
gulp.task('lint', function e() {
linter = require('gulp-eslint');
runAndWatch(runLinter, LRULES);
});
gulp.task('test', function t() {
return tester('tests/**/*Spec.js');
});
gulp.task('build', function t() {
// @link(https://github.com/sindresorhus/gulp-jasmine)
// options: verbose:false, includeStackTrace:false, reporter: obj/array
var rename = require('gulp-rename')
, uglify = require('gulp-uglify')
;
return gulp.src('./lib/eventist.js')
.pipe(uglify({preserveComments: 'some'}))
.pipe(rename('eventist.min.js'))
.pipe(gulp.dest('./lib'));
});
// Define the default task as a sequence of the above tasks
gulp.task('default', ['test', 'lint']);