This repository has been archived by the owner on May 24, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathgulpfile.js
356 lines (324 loc) · 10.8 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
var gulp = require('gulp'),
wiredep = require('wiredep').stream,
eventStream = require('event-stream'),
gulpLoadPlugins = require('gulp-load-plugins'),
fs = require('fs'),
path = require('path'),
url = require('url'),
uri = require('urijs'),
urljoin = require('url-join'),
s = require('underscore.string'),
stringifyObject = require('stringify-object'),
hawtio = require('hawtio-node-backend'),
argv = require('yargs').argv,
del = require('del');
var plugins = gulpLoadPlugins({});
var pkg = require('./package.json');
var config = {
main: '.',
ts: ['plugins/**/*.ts'],
less: ['plugins/**/*.less'],
templates: ['plugins/**/*.html'],
templateModule: pkg.name + '-templates',
dist: argv.out || './dist/',
js: pkg.name + '.js',
css: pkg.name + '.css',
tsProject: plugins.typescript.createProject({
target: 'ES5',
module: 'commonjs',
declarationFiles: true,
noExternalResolve: false
})
};
gulp.task('bower', function() {
return gulp.src('index.html')
.pipe(wiredep({}))
.pipe(gulp.dest('.'));
});
/** Adjust the reference path of any typescript-built plugin this project depends on */
gulp.task('path-adjust', function() {
return gulp.src('libs/**/includes.d.ts')
.pipe(plugins.replace(/"\.\.\/libs/gm, '"../../../libs'))
.pipe(gulp.dest('libs'));
});
gulp.task('clean-defs', function() {
return del('defs.d.ts');
});
gulp.task('tsc', ['clean-defs'], function() {
var cwd = process.cwd();
var tsResult = gulp.src(config.ts)
.pipe(plugins.sourcemaps.init())
.pipe(plugins.typescript(config.tsProject))
.on('error', plugins.notify.onError({
onLast: true,
message: '<%= error.message %>',
title: 'Typescript compilation error'
}));
return eventStream.merge(
tsResult.js
.pipe(plugins.concat('compiled.js'))
.pipe(plugins.sourcemaps.write())
.pipe(gulp.dest('.')),
tsResult.dts
.pipe(gulp.dest('d.ts')))
.pipe(plugins.filter('**/*.d.ts'))
.pipe(plugins.concatFilenames('defs.d.ts', {
root: cwd,
prepend: '/// <reference path="',
append: '"/>'
}))
.pipe(gulp.dest('.'));
});
gulp.task('less', function () {
return gulp.src(config.less)
.pipe(plugins.less({
paths: [ path.join(__dirname, 'less', 'includes') ]
}))
.on('error', plugins.notify.onError({
onLast: true,
message: '<%= error.message %>',
title: 'less file compilation error'
}))
.pipe(plugins.concat(config.css))
.pipe(gulp.dest(config.dist));
});
gulp.task('template', ['tsc'], function() {
return gulp.src(config.templates)
.pipe(plugins.angularTemplatecache({
filename: 'templates.js',
root: 'plugins/',
standalone: true,
module: config.templateModule,
templateFooter: '}]); hawtioPluginLoader.addModule("' + config.templateModule + '");'
}))
.pipe(gulp.dest('.'));
});
gulp.task('concat', ['template'], function() {
return gulp.src(['compiled.js', 'templates.js'])
.pipe(plugins.concat(config.js))
.pipe(plugins.ngAnnotate())
.pipe(gulp.dest(config.dist));
});
gulp.task('clean', ['concat'], function() {
return del(['templates.js', 'compiled.js', './site/']);
});
gulp.task('watch-less', function() {
plugins.watch(config.less, function() {
gulp.start('less');
});
});
gulp.task('watch', ['build', 'watch-less'], function() {
plugins.watch(['libs/**/*.js', 'libs/**/*.css', 'index.html', config.dist + '/*'], function() {
gulp.start('reload');
});
plugins.watch(['libs/**/*.d.ts', config.ts, config.templates], function() {
gulp.start(['tsc', 'template', 'concat', 'clean']);
});
});
gulp.task('connect', ['watch'], function() {
// lets disable unauthorised TLS issues with kube REST API
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
var kubeBase = process.env.KUBERNETES_MASTER || 'https://localhost:8443';
console.log("==== using KUBERNETES URL: " + kubeBase);
var kube = uri(urljoin(kubeBase, 'api'));
var kubeapis = uri(urljoin(kubeBase, 'apis'));
var oapi = uri(urljoin(kubeBase, 'oapi'));
console.log("Connecting to Kubernetes on: " + kube);
var staticAssets = [{
path: '/',
dir: '.'
}];
var dirs = fs.readdirSync('./libs');
dirs.forEach(function(dir) {
var dir = './libs/' + dir;
console.log("dir: ", dir);
if (fs.statSync(dir).isDirectory()) {
console.log("Adding directory to search path: ", dir);
staticAssets.push({
path: '/',
dir: dir
});
}
});
var localProxies = [];
if (process.env.LOCAL_APP_LIBRARY === "true") {
localProxies.push({
proto: "http",
port: "8588",
hostname: "localhost",
path: '/api/v1/proxy/namespaces/default/services/app-library',
targetPath: "/"
});
console.log("because of $LOCAL_APP_LIBRARY being true we are using a local proxy for /api/v1/proxy/namespaces/default/services/app-library" );
}
if (process.env.LOCAL_FABRIC8_FORGE === "true") {
localProxies.push({
proto: "http",
port: "8080",
hostname: "localhost",
path: '/api/v1/proxy/namespaces/default/services/fabric8-forge',
targetPath: "/"
});
console.log("because of LOCAL_FABRIC8_FORGE being true we are using a local proxy for /api/v1/proxy/namespaces/default/services/fabric8-forge" );
}
if (process.env.LOCAL_GOGS_HOST) {
var gogsPort = process.env.LOCAL_GOGS_PORT || "3000";
//var gogsHostName = process.env.LOCAL_GOGS_HOST + ":" + gogsPort;
var gogsHostName = process.env.LOCAL_GOGS_HOST;
console.log("Using gogs host: " + gogsHostName);
localProxies.push({
proto: "http",
port: gogsPort,
hostname: gogsHostName,
path: '/kubernetes/api/v1/proxy/services/gogs-http-service',
targetPath: "/"
});
console.log("because of LOCAL_GOGS_HOST being set we are using a local proxy for /kubernetes/api/v1/proxy/services/gogs-http-service to point to http://"
+ process.env.LOCAL_GOGS_HOST + ":" + gogsPort);
}
if (process.env.LOCAL_JENKINSHIFT) {
var jenkinshiftPort = process.env.LOCAL_JENKINSHIFT_PORT || "9090";
var jenkinshiftHost = process.env.LOCAL_JENKINSHIFT;
console.log("Using jenkinshift host: " + jenkinshiftHost);
var proxyPath = '/api/v1/proxy/namespaces/default/services/templates/oapi/v1';
console.log("Using jenkinshift host: " + jenkinshiftHost);
localProxies.push({
proto: "http",
port: jenkinshiftPort,
hostname: jenkinshiftHost,
path: proxyPath,
targetPath: "/oapi/v1"
});
localProxies.push({
proto: "http",
port: jenkinshiftPort,
hostname: jenkinshiftHost,
path: "/oapi/v1",
targetPath: "/oapi/v1"
});
console.log("because of LOCAL_JENKINSHIFT being set we are using a local proxy for " + proxyPath + " to point to http://"
+ jenkinshiftHost + ":" + jenkinshiftPort);
}
var defaultProxies = [{
proto: kube.protocol(),
port: kube.port(),
hostname: kube.hostname(),
path: '/kubernetes/api',
targetPath: kube.path()
}, {
proto: kubeapis.protocol(),
port: kubeapis.port(),
hostname: kubeapis.hostname(),
path: '/apis',
targetPath: kubeapis.path()
}, {
proto: oapi.protocol(),
port: oapi.port(),
hostname: oapi.hostname(),
path: '/kubernetes/oapi',
targetPath: oapi.path()
}, {
proto: kube.protocol(),
hostname: kube.hostname(),
port: kube.port(),
path: '/jolokia',
targetPath: '/hawtio/jolokia'
}, {
proto: kube.protocol(),
hostname: kube.hostname(),
port: kube.port(),
path: '/git',
targetPath: '/hawtio/git'
}];
var staticProxies = localProxies.concat(defaultProxies);
hawtio.setConfig({
port: process.env.DEV_PORT || 9000,
staticProxies: staticProxies,
staticAssets: staticAssets,
fallback: 'index.html',
liveReload: {
enabled: true
}
});
var debugLoggingOfProxy = process.env.DEBUG_PROXY === "true";
var useAuthentication = process.env.DISABLE_OAUTH !== "true";
var googleClientId = process.env.GOOGLE_OAUTH_CLIENT_ID;
var googleClientSecret = process.env.GOOGLE_OAUTH_CLIENT_SECRET;
hawtio.use('/osconsole/config.js', function(req, res, next) {
var config = {
api: {
openshift: {
proto: oapi.protocol(),
hostPort: oapi.host(),
prefix: oapi.path()
},
k8s: {
proto: kube.protocol(),
hostPort: kube.host(),
prefix: kube.path()
}
}
};
if (googleClientId && googleClientSecret) {
config.master_uri = kubeBase;
config.google = {
clientId: googleClientId,
clientSecret: googleClientSecret,
authenticationURI: "https://accounts.google.com/o/oauth2/auth",
authorizationURI: "https://accounts.google.com/o/oauth2/auth",
scope: "profile",
redirectURI: "http://localhost:9000"
};
} else if (useAuthentication) {
config.master_uri = kubeBase;
config.openshift = {
oauth_authorize_uri: urljoin(kubeBase, '/oauth/authorize'),
oauth_client_id: 'fabric8'
};
}
var answer = "window.OPENSHIFT_CONFIG = window.HAWTIO_OAUTH_CONFIG = " + stringifyObject(config);
res.set('Content-Type', 'application/javascript');
res.send(answer);
});
hawtio.use('/', function(req, res, next) {
var path = req.originalUrl;
// avoid returning these files, they should get pulled from js
if (s.startsWith(path, '/plugins/') && s.endsWith(path, 'html')) {
console.log("returning 404 for: ", path);
res.statusCode = 404;
res.end();
} else {
if (debugLoggingOfProxy) {
console.log("allowing: ", path);
}
next();
}
});
hawtio.listen(function(server) {
var host = server.address().address;
var port = server.address().port;
console.log("started from gulp file at ", host, ":", port);
});
});
gulp.task('reload', function() {
gulp.src('.')
.pipe(hawtio.reload());
});
gulp.task('build', ['bower', 'path-adjust', 'tsc', 'less', 'template', 'concat', 'clean']);
gulp.task('site', ['clean', 'build'], function() {
gulp.src(['index.html', 'osconsole/config.js.tmpl', 'css/**', 'images/**', 'img/**', 'libs/**', 'dist/**'], {base: '.'}).pipe(gulp.dest('site'));
var dirs = fs.readdirSync('./libs');
dirs.forEach(function(dir) {
var path = './libs/' + dir + "/img";
try {
if (fs.statSync(path).isDirectory()) {
console.log("found image dir: " + path);
var pattern = 'libs/' + dir + "/img/**";
gulp.src([pattern]).pipe(gulp.dest('site/img'));
}
} catch (e) {
// ignore, file does not exist
}
});
});
gulp.task('default', ['connect']);