forked from online-go/online-go.com
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGulpfile.js
240 lines (210 loc) · 7.57 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
'use strict';
const spawn = require('child_process').spawn;
const fs = require('fs');
const gulp = require('gulp');
const execSync = require('child_process').execSync;
const livereload = require('gulp-livereload');
const stylus = require('gulp-stylus');
const gutil = require("gulp-util");
const sourcemaps = require('gulp-sourcemaps');
const rename = require('gulp-rename');
const pump = require('pump');
const autoprefixer = require('autoprefixer');
const postcss = require('gulp-postcss');
const cssnano = require('cssnano');
const inline_svg = require('postcss-inline-svg');
const gulpTsLint = require('gulp-tslint');
const tslint = require('tslint');
let ts_sources = ['src/**/*.ts', 'src/**/*.tsx'];
gulp.task('default', ['dev-server', "livereload-server", "background_webpack", "build_styl", "watch_styl", "watch_dist_js", "watch_html", "watch_tslint"]);
gulp.task('watch_dist_js', () => { gulp.watch(['dist/*.js'], livereload.reload); });
gulp.task('watch_html', () => { gulp.watch(['src/*.html'], livereload.reload); });
gulp.task('watch_styl', () => { gulp.watch(['src/**/*.styl', 'src/*.styl'], ['build_styl']); });
gulp.task('build_styl', build_styl);
gulp.task('min_styl', min_styl);
gulp.task('livereload-server', () => { livereload.listen(35701); });
gulp.task('background_webpack', background_webpack);
gulp.task('watch_tslint', () => { gulp.watch([ts_sources], ['tslint']); });
gulp.task('dev-server', dev_server);
gulp.task('tslint', lint);
let lint_debounce = null;
function lint(done) {
if (lint_debounce) {
done();
return;
}
lint_debounce = setTimeout(()=>{
lint_debounce = null;
gulp.src(ts_sources, {base: '.'})
.pipe(gulpTsLint({
formatter: "prose"
}))
.pipe(gulpTsLint.report({
emitError: false,
reportLimit: 0,
summarizeFailureOutput: false
}))
}, 50)
done();
}
function build_styl(done) {
pump([gulp.src('./src/ogs.styl'),
sourcemaps.init(),
stylus({
compress: false,
}),
postcss([
autoprefixer({
browsers: ["> 1%", "last 2 versions", "Firefox ESR"],
cascade: false
}),
inline_svg(),
//cssnano(),
]),
sourcemaps.write('.'),
gulp.dest('./dist'),
],
(err) => {
if (err) {
console.error(err);
} else {
livereload.reload('ogs.css');
}
done();
}
);
}
function min_styl(done) {
let version = execSync('git describe --long || echo "min"');
version = ("" + version).replace(/\s/g, '')
console.log(version);
console.info(`Building ogs.${version}.css`);
pump([gulp.src('./src/ogs.styl'),
sourcemaps.init(),
stylus({
compress: false,
}),
postcss([
autoprefixer({
browsers: ["> 1%", "last 2 versions", "Firefox ESR"],
cascade: false
}),
inline_svg(),
cssnano(),
]),
rename({suffix: '.' + version}),
sourcemaps.write('.'),
gulp.dest('./dist'),
],
(err) => {
if (err) {
console.error(err);
} else {
livereload.reload('ogs.css');
}
done();
}
);
}
function background_webpack(done) {
function launch() {
console.log('Launching webpack')
let env = process.env;
let webpack = spawn('node', ['node_modules/webpack/bin/webpack.js', '--watch', '--progress', '--colors'])
webpack.stdout.on('data', o=>process.stdout.write(o))
webpack.stderr.on('data', o=>process.stderr.write(o))
webpack.on('exit', ()=>{
launch();
})
}
launch();
done()
}
function dev_server(done) {
let port = 8080;
let express = require('express');
let body_parser = require('body-parser');
let http = require('http');
var proxy = require('express-http-proxy');
let devserver = express();
devserver.use(body_parser.json())
devserver.use(body_parser.text())
http.createServer(devserver)
.listen(port, null, function() {
console.info(`#############################################`);
console.info(`## Development server started on port ${port} ##`);
console.info(`## http://dev.beta.online-go.com:${port} ##`);
console.info(`#############################################`);
});
devserver.use(express.static('dist'))
devserver.use(express.static('assets'))
let beta_proxy = (prefix) => proxy('beta.online-go.com', {
https: true,
forwardPath: function(req, res) {
let path = prefix + require('url').parse(req.url).path;
console.log('-->', path);
return path;
}
});
devserver.use('/api', beta_proxy('/api'));
devserver.use('/merchant', beta_proxy('/merchant'));
devserver.use('/sso', beta_proxy('/sso'));
devserver.use('/oauth2', beta_proxy('/oauth2'));
devserver.use('/complete', beta_proxy('/complete'));
devserver.use('/disconnect', beta_proxy('/disconnect'));
devserver.get('/locale/*', (req, res) => {
let options = {
hostname: 'storage.googleapis.com',
port: 80,
path: '/ogs-site-files/dev' + req.path,
method: 'GET',
};
let req2 = http.request(options, (res2) => {
res2.setEncoding('utf8');
let data = "";
res2.on('data', (chunk) => {
data += chunk.toString();
});
res2.on('end', () => {
res.setHeader('content-type', 'application/javascript');
res.setHeader("Content-Length", data.length);
res.send(200, data);
});
});
req2.on('error', (e) => {
res.send(500, e.message);
});
req2.end();
});
devserver.get('*', (req, res) => {
console.info(`GET ${req.path}`);
let _index = fs.readFileSync('src/index.html', {encoding: 'utf-8'});
let index = _index.replace(/[{][{]\s*(\w+)\s*[}][}]/g, (_,parameter) => {
switch (parameter) {
case 'CDN_SERVICE': return `//localhost:${port}/`;
case 'LIVE_RELOAD': return `<script src="//${req.hostname}:35701/livereload.js"></script>`;
case 'MIN': return '';
case 'OG_TITLE': return '';
case 'OG_URL': return '';
case 'OG_IMAGE': return '';
case 'OG_DESCRIPTION': return '';
case 'RELEASE': return '';
case 'VERSION': return '';
case 'LANGUAGE_VERSION': return '';
case 'VERSION_DOTJS': return 'js';
case 'VERSION_DOTCSS': return 'css';
case 'LANGUAGE_VERSION_DOTJS': return 'js';
case 'EXTRA_CONFIG':
return `<script>window['websocket_host'] = "https://beta.online-go.com";</script>`
;
}
return '{{' + parameter + '}}';
});
res.setHeader("Content-Type", "text/html; charset=utf-8");
res.setHeader('Cache-Control', 'no-cache, no-store, must-revalidate');
res.setHeader('Pragma', 'no-cache');
res.setHeader('Expires', '0');
res.setHeader("Content-Length", index.length);
res.status(200).send(index);
});
}