-
Notifications
You must be signed in to change notification settings - Fork 8
/
gulpfile.js
101 lines (84 loc) · 2.59 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
/*eslint strict: [2, "global"], no-var: 0*/
'use strict';
var babel = require('gulp-babel');
var eslint = require('gulp-eslint');
var fs = require('fs');
var gulp = require('gulp');
var gutil = require('gulp-util');
var path = require('path');
var sourcemaps = require('gulp-sourcemaps');
var through = require('through2');
var watch = require('gulp-watch');
var MAIN_FILES = './*.js';
var SRC_FILES = './src/**/*.js';
var DIST_DIR = './dist/';
var isMac = process.platform === 'darwin';
var isWin = process.platform === 'win32';
// options for transpiling es6 to es5
// we need to check OS here because Linux doesn't have CEF with generators
var babelOptions = isMac || isWin ? {
// generators are available in Brackets' shell and also break sourcemaps
blacklist: ['regenerator', 'strict']
} : {};
// provides pipe to log stuff to console when certain task finishes
function logPipe(str) {
return through.obj(function (file, enc, cb) {
cb();
}, function (cb) {
gutil.log(str);
cb();
});
}
// prevents watch from crashing on errors
function swallowError(error) {
gutil.log(gutil.colors.red(error.toString()));
this.emit('end');
}
// helper for transpiling es6 files to es5
function doBabel(globs, singleFile) {
if (singleFile) {
gutil.log(gutil.colors.cyan('Start Babel ' + globs[0]));
}
var task = gulp.src(globs, {base: 'src'})
.pipe(sourcemaps.init())
.pipe(babel(babelOptions))
.on('error', swallowError)
.pipe(sourcemaps.write('.', {
sourceMappingURLPrefix: path.resolve(__dirname, 'dist') + '/'
}))
.pipe(gulp.dest(DIST_DIR));
return singleFile ?
task.pipe(logPipe(gutil.colors.cyan('Finish Babel ' + globs[0]))) :
task;
}
// helper for linting files
function doEslint(globs, singleFile) {
if (singleFile) {
gutil.log(gutil.colors.magenta('Start ESLint ' + globs[0]));
}
var task = gulp.src(globs)
.pipe(eslint())
.pipe(eslint.format());
return singleFile ?
task.pipe(logPipe(gutil.colors.magenta('Finish ESLint ' + globs[0]))) :
task.pipe(eslint.failAfterError());
}
gulp.task('babel', function () {
return doBabel([SRC_FILES], false);
});
gulp.task('eslint', function () {
return doEslint([MAIN_FILES, SRC_FILES], false);
});
gulp.task('watch', function () {
watch(SRC_FILES, function (file) {
var filePath = path.relative(__dirname, file.path);
if (fs.statSync(filePath).isFile()) {
doEslint([filePath], true);
doBabel([filePath], true);
}
});
});
gulp.task('build', ['babel']);
gulp.task('test', ['eslint']);
gulp.task('dev', ['build', 'watch']);
gulp.task('default', ['build', 'test']);