-
Notifications
You must be signed in to change notification settings - Fork 9
/
gulpfile.js
454 lines (413 loc) · 9.47 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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
const gulp = require('gulp');
const server = require('browser-sync').create();
const del = require('del');
const fs = require('fs');
const main_bower_files = require('main-bower-files');
const uglify = require('gulp-uglify-es').default;
const usage = require('gulp-help-doc');
const yargs = require('yargs-parser');
const yarn = require('gulp-yarn');
const config = require('./gulp/config');
const paths = require('./gulp/paths');
const util = require('./gulp/util');
gulp.task('help', () => {
return usage(gulp);
});
// eslint-disable-next-line
const $ = require('gulp-load-plugins')();
/*** Script ***/
is_coffee = function (file) {
return file.path.indexOf('.coffee') > 0;
};
gulp.task('script', () => {
return gulp
.src(config.script)
.pipe(
$.plumber({
errorHandler: util.onError,
}),
)
.pipe($.if(is_coffee, $.coffee()))
.pipe($.concat('script.js'))
.pipe(
$.babel({
presets: ['@babel/env'],
}),
)
.pipe(uglify())
.pipe(
$.size({
title: 'Minified scripts',
}),
)
.pipe(gulp.dest(`${paths.static.min}/script`));
});
gulp.task('script:dev', () => {
return gulp
.src(config.script)
.pipe(
$.plumber({
errorHandler: util.onError,
}),
)
.pipe($.sourcemaps.init())
.pipe($.if(is_coffee, $.coffee()))
.pipe($.concat('script.js'))
.pipe($.sourcemaps.write())
.pipe(gulp.dest(`${paths.static.dev}/script`));
});
/*** Style ***/
gulp.task('style', () => {
return gulp
.src(config.style)
.pipe(
$.plumber({
errorHandler: util.onError,
}),
)
.pipe($.less())
.pipe(
$.cssnano({
discardComments: {
removeAll: true,
},
zindex: false,
}),
)
.pipe(
$.size({
title: 'Minified styles',
}),
)
.pipe(gulp.dest(`${paths.static.min}/style`));
});
gulp.task('style:dev', () => {
return gulp
.src(config.style)
.pipe(
$.plumber({
errorHandler: util.onError,
}),
)
.pipe($.sourcemaps.init())
.pipe($.less())
.pipe(
$.autoprefixer({
map: true,
}),
)
.pipe($.sourcemaps.write())
.pipe(gulp.dest(`${paths.static.dev}/style`))
.pipe(server.stream());
});
/*** Ext ***/
gulp.task('ext', () => {
return gulp
.src(config.ext)
.pipe(
$.plumber({
errorHandler: util.onError,
}),
)
.pipe($.concat('ext.js'))
.pipe(uglify())
.pipe(
$.size({
title: 'Minified ext libs',
}),
)
.pipe(gulp.dest(`${paths.static.min}/script`));
});
gulp.task('ext:dev', () => {
return gulp
.src(config.ext)
.pipe(
$.plumber({
errorHandler: util.onError,
}),
)
.pipe($.sourcemaps.init())
.pipe($.concat('ext.js'))
.pipe($.sourcemaps.write())
.pipe(gulp.dest(`${paths.static.dev}/script`));
});
gulp.task('pip', () => {
return gulp.src('run.py').pipe(
$.start([
{
cmd: 'python run.py -d',
match: /run.py$/,
},
]),
);
});
gulp.task(
'bower',
(copy_bower_sub = () => {
let cmd;
cmd = 'node_modules/.bin/bower install';
if (/^win/.test(process.platform)) {
cmd = cmd.replace(/\//g, '\\');
}
const start_map = [
{
cmd: cmd,
match: /bower.json$/,
},
];
return gulp.src('bower.json').pipe($.plumber()).pipe($.start(start_map));
}),
);
gulp.task(
'copy_bower_files',
gulp.series('bower', () => {
return gulp
.src(main_bower_files(), {
base: paths.dep.bower_components,
})
.pipe(gulp.dest(paths.static.ext));
}),
);
gulp.task('init', gulp.parallel('pip', 'copy_bower_files'));
gulp.task(
'browserSync',
(python_run = () => {
server.init({
notify: false,
proxy: `${config.host}:${config.port}`,
});
}),
);
gulp.task(
'prerun',
gulp.series('init', gulp.parallel('ext:dev', 'script:dev', 'style:dev')),
);
/**
Start the local server. Available options:
@task {run}
@arg {-o HOST} the host to start the dev_appserver.py
@arg {-p PORT} the port to start the dev_appserver.py
@arg {-a="..."} all following args are passed to dev_appserver.py
*/
gulp.task(
'run',
gulp.parallel('browserSync', () => {
let k_iterator;
let options_str;
const argv = process.argv.slice(2);
const known_options = {
default: {
// eslint-disable-next-line
a: '',
// eslint-disable-next-line
o: '',
// eslint-disable-next-line
p: '',
},
};
const options = yargs(argv);
options_str = '-s';
for (k_iterator in known_options.default) {
if (options[k_iterator]) {
if (k_iterator === 'a') {
options_str += ` --appserver-args "${options[k_iterator]}"`;
} else {
options_str += ` -${k_iterator} ${options[k_iterator]}`;
}
}
}
if (options.p) {
config.port = options.p;
}
if (options.o) {
config.host = options.o;
}
//gulp.start('browser-sync');
return gulp.src('run.py').pipe(
$.start([
{
cmd: `python run.py ${options_str}`,
match: /run.py$/,
},
]),
);
}),
);
/*** Clean ***/
/**
Clean project from temporary files, generated CSS & JS and
compiled Python files.
@task {clean}
*/
gulp.task('clean', () => {
del('./**/*.pyc');
del('./**/*.pyo');
return del('./**/*.~');
});
gulp.task('clean:dev', () => {
del(paths.static.ext);
return del(paths.static.dev);
});
gulp.task('clean:min', () => {
del(paths.static.ext);
return del(paths.static.min);
});
gulp.task('clean:venv', () => {
del(paths.py.lib);
del(paths.py.lib_file);
del(paths.dep.py);
return del(paths.dep.py_guard);
});
/**
Complete reset of project. Run "yarn install" after this procedure.
@task {reset}
*/
gulp.task(
'reset',
gulp.series(
gulp.parallel('clean', 'clean:dev', 'clean:min', 'clean:venv'),
(del_paths = () => {
del(paths.dep.bower_components);
return del(paths.dep.node_modules);
}),
),
);
/**
Clear local datastore, blobstore, etc.
@task {flush}
*/
gulp.task('flush', () => {
return del(paths.temp.storage);
});
/*** Dep ***/
gulp.task('yarn', () => {
return gulp
.src(['./package.json', './yarn.lock'])
.pipe($.plumber())
.pipe(yarn());
});
gulp.task('zip', (done) => {
if (!fs.existsSync(paths.py.lib_file)) {
if (fs.existsSync(paths.py.lib)) {
gulp
.src(`${paths.py.lib}/**`)
.pipe($.plumber())
.pipe($.zip('lib.zip'))
.pipe(gulp.dest(paths.main));
}
}
done();
});
/*** Watch ***/
function reload(done) {
server.reload();
done();
}
gulp.task('ext_watch_rebuild', (callback) => {
return gulp.parallel(
'copy_bower_files',
gulp.series('clean:dev', gulp.parallel('ext:dev', 'style:dev')),
)(callback);
});
gulp.task('watch', () => {
$.watch(`${paths.main}/requirements.txt`, () => {
return gulp.series('pip')();
});
$.watch('package.json', () => {
return gulp.series('yarn')();
});
$.watch('bower.json', () => {
return gulp.series('ext_watch_rebuild')();
});
$.watch('gulp/config.js', () => {
return gulp.series('ext:dev', gulp.parallel('style:dev', 'script:dev'))();
});
$.watch(paths.static.ext, () => {
return gulp.series('ext:dev')();
});
$.watch(`${paths.src.script}/**/*.{coffee,js}`, () => {
return gulp.series('script:dev')();
});
$.watch(
[`${paths.static.dev}/**/*.js`, `${paths.main}/**/*.{html,py}`],
() => {
return gulp.series(reload)();
},
);
return $.watch(`${paths.src.style}/**/*.less`, () => {
return gulp.series('style:dev')();
});
});
/*** Build ***/
/**
Build project to prepare it for a deployment. Minify CSS & JS files
and pack Python dependencies into #{paths.py.lib_file}.
@task {build}
*/
gulp.task(
'build',
gulp.series(
'clean:min',
'init',
'ext',
gulp.parallel('script', 'style', 'zip'),
),
);
/**
Re-build project from scratch. Equivalent to "reset" and "build" tasks.
@task {rebuild}
*/
gulp.task('rebuild', gulp.series('reset', 'build'));
/**
Deploy project to Google App Engine.
@task {deploy}
@arg {dryrun} Run all preparations but do not actually deploy
@arg {[other]} Other arguments are passed through to gcloud app deploy
*/
gulp.task(
'deploy',
gulp.series(
'build',
(gcloud = () => {
let dryrun;
let k_iterator;
let options_str;
const options = yargs(process.argv, {
configuration: {
'boolean-negation': false,
'camel-case-expansion': false,
},
});
delete options._;
options_str = '';
dryrun = '';
for (k_iterator in options) {
if (k_iterator === 'dryrun') {
dryrun = 'echo DRYRUN - would run: ';
} else {
if (options[k_iterator] === true) {
options[k_iterator] = '';
}
options_str += ` ${k_iterator.length > 1 ? '-' : ''}-${k_iterator} ${
options[k_iterator]
}`;
}
}
return gulp.src('run.py').pipe(
$.start([
{
cmd: `${dryrun}gcloud app deploy main/*.yaml${options_str}`,
match: /run.py$/,
},
]),
);
}),
),
);
/**
Start the local server, watch for changes and reload browser automatically.
For available options refer to "run" task.
@task {default}
@order {1}
*/
gulp.task('default', gulp.series('prerun', gulp.parallel('run', 'watch')));