This repository has been archived by the owner on Jul 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgulpfile.js
191 lines (166 loc) · 4.35 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
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
186
187
188
189
190
'use strict';
// Load environment variables (later used by envify)
require('dotenv').config({ path: 'config/.env' });
// Dependencies
var gulp = require('gulp');
var buffer = require('vinyl-buffer');
var source = require('vinyl-source-stream');
var browserify = require('browserify');
var nunjucksify = require('nunjucksify');
var envify = require('envify');
var cache = require('gulp-cached');
var jshint = require('gulp-jshint');
var nodemon = require('gulp-nodemon');
var uglify = require('gulp-uglify');
var minify = require('gulp-minify-css');
var concat = require('gulp-concat');
var sourcemaps = require('gulp-sourcemaps');
var gutil = require('gulp-util');
var path = require('path');
var del = require('del');
// Dependencies required for building the Primus client library
var Primus = require('primus');
var Emit = require('primus-emit');
var EventEmitter = require('events');
// Clients
var CLIENTS = ['corner-judge', 'jury-president'];
// Globs
var GLOBS = {
css: '**/*.css',
js: '**/*.js',
njk: '**/*.njk',
};
// Sets of paths
var SETS = {
lint: [
'app.js',
'gulpfile.js',
path.join('app', GLOBS.js),
path.join('clients', GLOBS.js),
('!' + path.join('clients', 'vendor', GLOBS.js)),
path.join('tests', GLOBS.js)
],
client: [
path.join('config/.env'),
path.join('config/config.json'),
path.join('clients/shared', GLOBS.js),
path.join('templates/precompiled', GLOBS.njk)
]
};
// Determine the arguments with which to start the server
var args = process.argv.indexOf('--force') !== -1 ? ['--force'] : [];
/**
* Clear the datastores.
*/
gulp.task('reset', function () {
return del('data/**');
});
/**
* Lint all scripts.
* Use gulp-cached to re-lint only files that have changed.
*/
gulp.task('scripts:lint', function() {
return gulp.src(SETS.lint)
.pipe(cache('scripts:lint'))
.pipe(jshint({
lookup: false, devel: true, browser: true, node: true,
bitwise: true, curly: true, eqeqeq: true, funcscope: true,
latedef: 'nofunc', nocomma: true, undef: true, unused: false
}))
.pipe(jshint.reporter('default'));
});
/**
* Generate the Primus client library.
*/
gulp.task('primus', function () {
var server = new EventEmitter();
// Instanciate Primus and configure its plugins (must match steps performed in `app/tournament.js`)
var primus = new Primus(server, { transformer: 'sockjs' });
primus.plugin('emit', Emit);
primus.remove('primus.js');
// Save the library
primus.save('clients/vendor/primus.js');
});
/**
* Build a client script with Browserify.
* Register one task per client.
*/
CLIENTS.forEach(function (client) {
gulp.task(client + ':js', function () {
return browserify({
entries: path.join('clients', client, 'root.js'),
debug: true
})
.transform(envify)
.transform(nunjucksify, { extension: '.njk' })
.bundle()
.pipe(source(client + '.js'))
.pipe(buffer())
.pipe(sourcemaps.init({ loadMaps: true }))
.pipe(uglify())
.on('error', gutil.log)
.pipe(sourcemaps.write('./'))
.pipe(gulp.dest('public/js'));
});
});
/**
* Build a client stylesheet.
* Register one task per client.
*/
CLIENTS.forEach(function (client) {
gulp.task(client + ':css', function () {
return gulp.src([
'styles/main.css',
'styles/' + client + '.css'
])
.pipe(concat(client + '.css'))
.pipe(sourcemaps.init({ loadMaps: true }))
.pipe(minify())
.on('error', gutil.log)
.pipe(sourcemaps.write('./'))
.pipe(gulp.dest('public/css'));
});
});
/**
* Start the development server.
* Reload when the relevant files have changed.
*/
gulp.task('server', ['build'], function () {
nodemon({
script: 'app.js',
args: args,
watch: [
'app',
'app.js',
'config/.env',
'config/config.json'
]
});
});
/**
* Watch for changes.
*/
gulp.task('watch', ['server'], function () {
// Watch and rebuild each client's scritps
CLIENTS.forEach(function (client) {
gulp.watch(SETS.client.concat([
path.join('clients', client, GLOBS.js),
]), [client + ':js']);
gulp.watch([
'styles/main.css',
'styles/' + client + '.css'
], [client + ':css']);
});
// Lint any changed JS files
gulp.watch(SETS.lint, ['scripts:lint']);
});
/**
* ============
* MAIN TASKS
* ============
*/
gulp.task('default', ['build', 'scripts:lint', 'server', 'watch']);
gulp.task('build', CLIENTS.reduce(function (arr, client) {
arr.push(client + ':css', client + ':js');
return arr;
}, []));