forked from Cloud-CV/EvalAI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
327 lines (287 loc) · 10.1 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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
var gulp = require('gulp'),
del = require('del'),
_ = require('lodash'),
fs = require('fs'),
path = require('path'),
concat = require('gulp-concat'),
// imagemin = require('gulp-imagemin'),
// htmlmin = require('gulp-htmlmin'),
sass = require('gulp-sass'),
postcss = require('gulp-postcss'),
cleanCSS = require('gulp-clean-css'),
sourcemaps = require('gulp-sourcemaps'),
autoprefixer = require('autoprefixer'),
merge = require('merge-stream'),
rename = require('gulp-rename'),
inject = require('gulp-inject'),
uglify = require('gulp-uglify'),
eslint = require('gulp-eslint'),
connectModRewrite = require('connect-modrewrite'),
connect = require('gulp-connect'),
gulp_if = require('gulp-if'),
replace = require('gulp-replace'),
karmaServer = require('karma').Server;
// development task
var production = false;
var scripts = JSON.parse(fs.readFileSync('frontend/app.scripts.json'));
var styles = JSON.parse(fs.readFileSync('frontend/app.styles.json'));
var configJson = JSON.parse(fs.readFileSync('frontend/src/js/config.json'));
function clean() {
return del(['frontend/dist/']);
};
/*
Concat all js libs
*/
function vendorjs() {
var paths = [];
_.forIn(scripts.chunks, function(chunkScripts, chunkName) {
chunkScripts.forEach(function(script) {
var scriptFileName = scripts.paths[script];
if (!fs.existsSync(__dirname + '/' + scriptFileName)) {
throw console.error('Required path doesn\'t exist: ' + __dirname + '/' + scriptFileName, script)
}
paths.push(scriptFileName);
});
});
return gulp.src(paths)
.pipe(concat('vendor.js'))
.pipe(gulp.dest("frontend/dist/vendors"))
}
/*
Concat all css libs
*/
function vendorcss() {
var paths = [];
_.forIn(styles.chunks, function(chunkStyles, chunkName) {
chunkStyles.forEach(function(style) {
var styleFileName = styles.paths[style];
if (!fs.existsSync(__dirname + '/' + styleFileName)) {
throw console.error('Required path doesn\'t exist: ' + __dirname + '/' + styleFileName, style)
}
paths.push(styleFileName);
});
});
return gulp.src(paths)
.pipe(concat('vendor.css'))
.pipe(gulp.dest("frontend/dist/vendors"))
}
/*
minify and compress custom css files
*/
function css() {
return gulp.src('frontend/src/css/main.scss')
.pipe(sass().on('error', sass.logError))
.pipe(sourcemaps.init())
.pipe(postcss([autoprefixer()]))
.pipe(gulp_if(production, cleanCSS()))
.pipe(gulp_if(production, rename({ suffix: '.min' })))
.pipe(sourcemaps.write())
.pipe(gulp.dest('frontend/dist/css'))
pipe(connect.reload());
}
/*
minify angular scripts
*/
function js() {
var app = gulp.src('frontend/src/js/app.js')
.pipe(concat('app.js'))
.pipe(gulp_if(production, rename({ suffix: '.min' })))
.pipe(gulp_if(production, uglify({ mangle: false })))
.pipe(gulp.dest('frontend/dist/js'));
var configs = gulp.src('frontend/src/js/route-config/*.js')
.pipe(concat('route-config.js'))
.pipe(gulp_if(production, rename({ suffix: '.min' })))
.pipe(gulp_if(production, uglify({ mangle: false })))
.pipe(gulp.dest('frontend/dist/js'));
var controllers = gulp.src('frontend/src/js/controllers/*.js')
.pipe(concat('controllers.js'))
.pipe(gulp_if(production, rename({ suffix: '.min' })))
.pipe(gulp_if(production, uglify({ mangle: false })))
.pipe(gulp.dest('frontend/dist/js'));
var directives = gulp.src('frontend/src/js/directives/*.js')
.pipe(concat('directives.js'))
.pipe(gulp_if(production, rename({ suffix: '.min' })))
.pipe(gulp_if(production, uglify({ mangle: false })))
.pipe(gulp.dest('frontend/dist/js'))
var filters = gulp.src('frontend/src/js/filters/*.js')
.pipe(concat('filters.js'))
.pipe(gulp_if(production, rename({ suffix: '.min' })))
.pipe(gulp_if(production, uglify({ mangle: false })))
.pipe(gulp.dest('frontend/dist/js'));
var services = gulp.src('frontend/src/js/services/*.js')
.pipe(concat('services.js'))
.pipe(gulp_if(production, rename({ suffix: '.min' })))
.pipe(gulp_if(production, uglify({ mangle: false })))
.pipe(gulp.dest('frontend/dist/js'))
return merge(app, configs, controllers, directives, filters, services).
pipe(connect.reload());
}
/*
minify and compress html files
*/
function html() {
return gulp.src('frontend/src/views/web/**/*.html')
// .pipe(gulp_if(production, htmlmin({ collapseWhitespace: true })))
.pipe(gulp.dest('frontend/dist/views/web/'))
.pipe(connect.reload());
}
/*
for image compression
*/
function images() {
return gulp.src('frontend/src/images/**/*')
// .pipe(gulp_if(production, imagemin()))
.pipe(gulp.dest('frontend/dist/images'));
}
/*
Fonts
*/
function fonts() {
var font = gulp.src([
'bower_components/font-awesome/fonts/fontawesome-webfont.*',
'bower_components/materialize/fonts/**/*',
'frontend/src/fonts/*'
])
.pipe(gulp.dest('frontend/dist/fonts/'));
var fontCss = gulp.src([
'bower_components/font-awesome/css/font-awesome.css'
])
.pipe(gulp_if(production, cleanCSS()))
.pipe(gulp_if(production, rename({ suffix: '.min' })))
.pipe(sourcemaps.write())
.pipe(gulp.dest('frontend/dist/css/'));
return merge(font, fontCss);
}
/*
config for prod server
*/
function configProd() {
return gulp.src('frontend/src/js/config.sample.js')
.pipe(replace('moduleName', 'evalai-config'))
.pipe(replace('constantName', Object.keys(configJson.production)))
.pipe(replace('configKey', Object.keys(configJson.production.EnvironmentConfig)))
.pipe(replace('configValue', configJson.production.EnvironmentConfig.API))
.pipe(rename({
basename: 'config'
}))
.pipe(gulp_if(production, rename({ suffix: '.min' })))
.pipe(gulp.dest('frontend/dist/js'))
}
/*
config for staging server
*/
function configStaging() {
return gulp.src('frontend/src/js/config.sample.js')
.pipe(replace('moduleName', 'evalai-config'))
.pipe(replace('constantName', Object.keys(configJson.staging)))
.pipe(replace('configKey', Object.keys(configJson.staging.EnvironmentConfig)))
.pipe(replace('configValue', configJson.staging.EnvironmentConfig.API))
.pipe(rename({
basename: 'config'
}))
.pipe(gulp_if(production, rename({ suffix: '.min' })))
.pipe(gulp.dest('frontend/dist/js/'))
}
/*
config for dev server
*/
function configDev() {
return gulp.src('frontend/src/js/config.sample.js')
.pipe(replace('moduleName', 'evalai-config'))
.pipe(replace('constantName', Object.keys(configJson.local)))
.pipe(replace('configKey', Object.keys(configJson.local.EnvironmentConfig)))
.pipe(replace('configValue', configJson.local.EnvironmentConfig.API))
.pipe(rename({
basename: 'config'
}))
.pipe(gulp_if(production, rename({ suffix: '.min' })))
.pipe(gulp.dest('frontend/dist/js/'))
}
/*
Inject path of css and js files in index.html
*/
function injectpaths() {
var target = gulp.src('frontend/base.html');
var sources = gulp.src([
'frontend/dist/vendors/*.js',
'frontend/dist/js/*.js',
'frontend/dist/vendors/*.css',
'frontend/dist/css/*.css',
], { read: false });
return target
.pipe(inject(sources, { ignorePath: 'frontend', addRootSlash: true }))
.pipe(rename({
basename: "index"
}))
.pipe(gulp.dest('frontend/'));
}
/*
js linting
*/
var lint_path = {
js: ['frontend/src/js/**/*.js', ]
}
function lint() {
return gulp.src(lint_path.js)
.pipe(eslint({}))
.pipe(eslint.format())
.pipe(eslint.results(function(results) {
// Get the count of lint errors
var countError = results.errorCount;
//Get the count of lint warnings
var countWarning = results.warningCount;
if (countError === 0) {
if (countWarning > 0) {
console.warn("Please remove lint warnings in production env.");
}
} else {
connect.serverClose();
console.error("Please remove lint errors to connect the server");
}
}))
}
/*
Start a server for serving frontend
*/
function startServer() {
// initially close the existance server if exists
connect.serverClose();
connect.server({
root: 'frontend/',
port: 8888,
host: '0.0.0.0',
livereload: true,
middleware: function(connect) {
return [
connectModRewrite([
'!\\.html|\\.js|\\.css|\\.ico|\\.png|\\.gif|\\.jpg|\\.woff|.\\.ttf|.\\otf|\\.jpeg|\\.swf.*$ /index.html [NC,L]'
])
];
}
});
}
function watch() {
gulp.watch('frontend/src/js/**/*.js', js);
gulp.watch('frontend/src/css/**/*.scss', css);
gulp.watch('frontend/src/views/web/**/*.html', html);
gulp.watch('frontend/src/images/**/*', images);
gulp.watch('bower_components/materialize/fonts/**/*', fonts);
gulp.watch('bower_components/materialize/fonts/**/*', fonts);
}
var parallelTasks = gulp.parallel(vendorcss, vendorjs, css, js, html, images, fonts);
gulp.task('prod', gulp.series(clean, function(done) {
production = true;
done();
}, parallelTasks, configProd, injectpaths, lint));
gulp.task('staging', gulp.series(clean, function(done) {
production = true;
done();
}, parallelTasks, configStaging, injectpaths, lint));
gulp.task('dev', gulp.series(clean, function(done) {
production = false;
done();
}, parallelTasks, configDev, injectpaths, lint));
gulp.task('dev:runserver', gulp.series(function(done) {
production = false;
done();
}, parallelTasks, configDev, injectpaths, lint, gulp.parallel(watch, startServer)));