-
Notifications
You must be signed in to change notification settings - Fork 1
/
gulpfile.babel.js
120 lines (101 loc) · 3.79 KB
/
gulpfile.babel.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
import BUILD_CONFIG from './buildConfig';
import gulp from 'gulp';
import loadPlugins from 'gulp-load-plugins';
import del from 'del';
/*******************************************************************************
* Contants
******************************************************************************/
// Tasks (prefixes and types)
const BUILD = 'build:';
const CLEAN = 'clean:';
const INSTRUMENT = 'instrument:';
const LINT = 'lint:';
const RUN = 'run:';
const TEST = 'test:';
const ALL = 'all';
const GULPFILE = 'gulpfile';
const SCRIPTS = 'scripts';
// Gulp + Plugins, etc.
const $ = loadPlugins();
const envCheck = process.env.NODE_ENV === 'production';
/*******************************************************************************
* Lint method(s)
******************************************************************************/
function lintJS(src, cacheKey) {
return gulp.src(src)
.pipe($.cached(cacheKey))
.pipe($.eslint())
.pipe($.eslint.format())
.pipe($.if(envCheck, $.eslint.failOnError()))
.pipe($.remember(cacheKey));
}
/*******************************************************************************
* Test method(s)
******************************************************************************/
function instrumentScripts() {
return gulp.src(BUILD_CONFIG.SRC_SCRIPTS)
.pipe($.babel())
.pipe($.istanbul({
...BUILD_CONFIG.ISTANBUL.INIT
}))
.pipe($.istanbul.hookRequire());
}
function testScripts() {
return gulp.src(BUILD_CONFIG.TST_UNIT)
.pipe($.babel())
.pipe($.mocha(BUILD_CONFIG.MOCHA))
// .pipe($.istanbul.writeReports(BUILD_CONFIG.ISTANBUL.WRITE))
.on('error', () => $.util.log('unit tests failed...'));
}
/*******************************************************************************
* Build method(s)
******************************************************************************/
function buildScripts() {
return gulp.src(BUILD_CONFIG.SRC_SCRIPTS)
.pipe($.changed(BUILD_CONFIG.OUTPUT_SCRIPTS))
.pipe($.if(!envCheck, $.sourcemaps.init()))
.pipe($.babel())
.pipe($.uglify())
.pipe($.if(!envCheck, $.sourcemaps.write()))
.pipe(gulp.dest(BUILD_CONFIG.OUTPUT_SCRIPTS));
}
/*******************************************************************************
* Runner(s)
******************************************************************************/
function runCleanAll() {
return del(BUILD_CONFIG.OUTPUT_DEL);
}
function runCleanScripts() {
return del(BUILD_CONFIG.OUTPUT_SCRIPTS);
}
function runWatch() {
gulp.watch(BUILD_CONFIG.SRC_GULPFILE, gulp.series(`${LINT}${GULPFILE}`));
gulp.watch(BUILD_CONFIG.SRC_SCRIPTS, gulp.series([`${RUN}${SCRIPTS}`]));
gulp.watch(BUILD_CONFIG.TST_UNIT, gulp.series([`${TEST}${SCRIPTS}`]));
}
/*******************************************************************************
* Tasks
*************************************************a*****************************/
// lint-specific tasks
gulp.task(`${LINT}${GULPFILE}`, lintJS.bind(null, BUILD_CONFIG.SRC_GULPFILE, GULPFILE));
gulp.task(`${LINT}${SCRIPTS}`, lintJS.bind(null, BUILD_CONFIG.SRC_SCRIPTS, SCRIPTS));
// test-specific tasks
gulp.task(`${INSTRUMENT}${SCRIPTS}`, instrumentScripts);
gulp.task(`${TEST}${SCRIPTS}`, testScripts);
// build-specific tasks
gulp.task(`${BUILD}${SCRIPTS}`, buildScripts);
// main task runners
gulp.task(`${RUN}${CLEAN}${ALL}`, runCleanAll);
gulp.task(`${RUN}${CLEAN}${SCRIPTS}`, runCleanScripts);
gulp.task(`${RUN}${SCRIPTS}`, gulp.series(
`${RUN}${CLEAN}${SCRIPTS}`,
`${LINT}${SCRIPTS}`,
`${INSTRUMENT}${SCRIPTS}`,
`${TEST}${SCRIPTS}`,
`${BUILD}${SCRIPTS}`
));
// main task runners
gulp.task('default', gulp.series(
`${LINT}${GULPFILE}`,
`${RUN}${SCRIPTS}`,
runWatch));