forked from 5calls/5calls-old
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgulpfile.js
More file actions
185 lines (156 loc) · 4.83 KB
/
gulpfile.js
File metadata and controls
185 lines (156 loc) · 4.83 KB
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
185
'use strict';
var gulp = require('gulp')
, sass = require('gulp-sass')
, autoprefixer = require('gulp-autoprefixer')
, imagemin = require('gulp-imagemin')
, util = require('gulp-util')
, browserify = require('browserify')
, es2040 = require('es2040')
, buffer = require('vinyl-buffer')
, source = require('vinyl-source-stream')
, uglify = require('gulp-uglify')
, http_server = require('http-server')
, connect_logger = require('connect-logger')
, spawn = require('child_process').spawn
;
var SRC = {
html: './static/html',
scss: './static/scss',
img: './static/img',
js: './static/js',
extra: './static/rootExtra'
};
var DEST = {
html:'./app/static',
css: './app/static/css',
img: './app/static/img',
js: './app/static/js'
};
gulp.task('html', function() {
gulp.src(SRC.html + '/*.html')
.pipe(gulp.dest(DEST.html));
});
gulp.task('html:watch', function() {
gulp.watch(`${SRC.html}/*.html`, ['html']);
});
gulp.task('html:serve', function (cb) {
var server = new http_server.HttpServer({
root: 'app/static',
before: [connect_logger()]
});
server.listen(8000, function () {
util.log('HTTP server started on port 8000');
cb();
});
});
// Compile Sass into CSS
gulp.task('sass', function() {
gulp.src(`${SRC.scss}/*.scss`)
.pipe(sass({
outputStyle: 'compressed'
}).on('error', sass.logError))
.pipe(autoprefixer('last 2 version'))
.pipe(gulp.dest(DEST.css));
});
gulp.task('sass:watch', function() {
gulp.watch(`${SRC.scss}/**/*.scss`, ['sass']);
});
// Copy/minify image assets
gulp.task('copy-images', function() {
gulp.src(SRC.img + '/**/*.+(png|jpg|jpeg|gif|svg)')
.pipe(imagemin())
.pipe(gulp.dest(DEST.img));
});
gulp.task('copy-images:watch', function() {
gulp.watch(SRC.img + '/**/*.+(png|jpg|jpeg|gif|svg)');
});
// Bundle and transpile javascript
gulp.task('scripts', function() {
var b = browserify({
entries: `${SRC.js}/main.js`,
debug: true,
transform: [es2040]
});
return b.bundle()
.pipe(source('app.js'))
.pipe(buffer())
.pipe(gulp.dest(DEST.js));
});
gulp.task('build-scripts', function() {
var b = browserify({
entries: `${SRC.js}/main.js`,
debug: false,
transform: [es2040]
});
return b.bundle()
.pipe(source('app.js'))
.pipe(buffer())
.pipe(uglify())
.pipe(gulp.dest(DEST.js));
});
gulp.task('scripts:watch', function() {
gulp.watch(`${SRC.js}/**/*.js`, ['scripts']);
});
gulp.task('extra', function() {
gulp.src(SRC.extra + '/*.+(ico|xml|json)')
.pipe(gulp.dest(DEST.html));
});
function runKarmaTests ({singleRun, configFile} = {}) {
return new Promise((resolve, reject) => {
const karmaArguments = ['start'];
if (configFile) {
karmaArguments.push(configFile);
}
if (singleRun) {
karmaArguments.push('--single-run');
}
// Karma has a nice public API, but has issues where it can hang when
// trying to shut down after completing tests, so run it as a separate
// process instead. See:
// https://github.com/karma-runner/karma/issues/1693
// https://github.com/karma-runner/karma/issues/1035
const karma = spawn('./node_modules/.bin/karma', karmaArguments, {
cwd: __dirname,
stdio: 'inherit'
});
karma.on('close', code => {
if (code) {
reject(new util.PluginError('Karma', `JS unit tests failed (code ${code})`));
return;
}
resolve();
});
});
}
gulp.task('test:js-unit', function() {
return runKarmaTests({singleRun: true});
});
gulp.task('test:watch', function() {
return runKarmaTests({singleRun: false});
});
// Designed for running tests in continuous integration. The main difference
// here is that browser tests are run across a gamut of browsers/platforms via
// Sauce Labs instead of just a few locally.
gulp.task('test:ci', ['eslint'], function() {
return runKarmaTests({configFile: 'karma.ci.conf.js'});
});
gulp.task('eslint', function() {
const eslint = require('eslint');
const linter = new eslint.CLIEngine();
const report = linter.executeOnFiles(['./static']);
// customize messages to be a little more helpful/friendly
report.results.forEach(result => {
result.messages.forEach(message => {
if (message.ruleId === 'no-console') {
message.message = 'Please use the `loglevel` module for logging instead of the browser `console` object';
}
});
});
process.stdout.write(linter.getFormatter()(report.results));
if (report.errorCount) {
throw new util.PluginError('ESLint', 'Found problems with JS coding style.');
}
});
gulp.task('test', ['eslint', 'test:js-unit']);
gulp.task('default', ['html', 'html:watch', 'html:serve', 'sass', 'sass:watch', 'copy-images', 'copy-images:watch', 'scripts', 'scripts:watch', 'extra']);
gulp.task('deploy', ['html', 'sass', 'build-scripts', 'extra', 'copy-images']);