-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
125 lines (113 loc) · 3.6 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
const exec = require('child_process').exec;
const fs = require('fs');
const googleClosureCompiler = require('google-closure-compiler');
const gulp = require('gulp');
const gulpConnect = require('gulp-connect');
const gulpData = require('gulp-data');
const gulpTemplate = require('gulp-template');
const gulpWatch = require('gulp-watch');
const DEVSERVER_HOST = 'localhost';
const DEVSERVER_PORTS = ['8080', '8081'];
const JS_SRCS = ['./src/**.js'];
const TEMPLATE_SRCS = ['./templates/**.html'];
const STATIC_FILES = ['./static/**/*'];
const SETTINGS_DEFAULT_PATH = './templates/.settings.default.json';
const COMMON_COMPILER_CONFIG = {
compilation_level: 'SIMPLE',
dependency_mode: 'PRUNE',
formatting: 'PRETTY_PRINT',
warning_level: 'VERBOSE',
language_in: 'ECMASCRIPT_2017',
language_out: 'ECMASCRIPT_2015',
externs: [
'../src/externs/closure.js',
],
};
const JS_BINARY_CONFIGS = [
{
entry_point: './src/root-page-controller',
js_output_file: 'index.js',
js: JS_SRCS,
},
{
entry_point: './src/test-case-page-controller',
js_output_file: 'test-case.js',
js: JS_SRCS,
},
{
entry_point: './src/video-creative-page-controller',
js_output_file: 'video.js',
js: [
'../src/common/**.js',
'../src/session-client/**.js',
'./src/**.js',
],
},
];
const jsCompiler = googleClosureCompiler.gulp();
const buildScriptTasks = JS_BINARY_CONFIGS.map((jsBinaryConfig) => {
const config = Object.assign({}, COMMON_COMPILER_CONFIG, jsBinaryConfig);
return () => jsCompiler(config).src().pipe(gulp.dest('./dist'));
});
const buildScripts = gulp.parallel(...buildScriptTasks);
/** Builds the template files in ./templates/. */
function buildTemplates() {
return gulp.src(TEMPLATE_SRCS)
.pipe(gulpData(() => JSON.parse(fs.readFileSync(SETTINGS_DEFAULT_PATH))))
.pipe(gulpTemplate())
.pipe(gulp.dest('./dist/'));
}
/** Copies static files to dist/, */
function copyStaticFiles() {
return gulp.src(STATIC_FILES, {dot: true}).pipe(gulp.dest('./dist/'));
}
/**
* Starts serving a dev server on the given port.
* @param {number} port
*/
function server(port) {
return gulpConnect.server({
root: './dist/',
port: port,
host: DEVSERVER_HOST,
livereload: true,
});
}
const serverTasks = DEVSERVER_PORTS.map((port) => server.bind(this, port));
const servers = gulp.parallel(...serverTasks);
/** Watches the dist/ folder for changes, reloading the page if so. */
function livereload() {
return gulpWatch('./dist/*').pipe(gulpConnect.reload());
}
/** Watches folders for changes, building if needed. */
function watch() {
gulp.watch(JS_SRCS, {}, buildScripts);
gulp.watch(TEMPLATE_SRCS, {}, buildTemplates);
gulp.watch(STATIC_FILES, {}, copyStaticFiles);
}
/** Builds the other package dependencies. */
function buildDependencies(callback) {
exec(
'cd ../ && ' +
'npm run build-validation-verification-script && ' +
'cd ../ && ' +
'npm run build-web-service && ' +
'npm run build-domain-loader',
(err) => {
callback(err);
});
}
/** Copies the other package dependencies into static. */
function copyDependencies(callback) {
exec(
'cp ../bin/omid-validation-verification-script-v1.js static/ &&' +
'cp ../../bin/omweb-v1.js static/ &&' +
'cp ../../bin/omloader-v1.html static/.well-known/omid/',
(err) => {
callback(err);
});
}
exports.buildDeps = gulp.series(buildDependencies, copyDependencies);
exports.build = gulp.series(buildScripts, buildTemplates, copyStaticFiles);
exports.start =
gulp.series(exports.build, gulp.parallel(servers, livereload, watch));