This repository has been archived by the owner on Dec 27, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
gulpfile.js
272 lines (243 loc) · 7.56 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
var gulp = require('gulp'),
wiredep = require('wiredep').stream,
eventStream = require('event-stream'),
gulpLoadPlugins = require('gulp-load-plugins'),
fs = require('fs'),
path = require('path'),
size = require('gulp-size'),
uri = require('urijs'),
s = require('underscore.string'),
hawtio = require('hawtio-node-backend'),
urljoin = require('url-join'),
argv = require('yargs').argv,
del = require('del');
var plugins = gulpLoadPlugins({});
var pkg = require('./package.json');
var config = {
main: '.',
ts: ['plugins/**/*.ts'],
testTs: ['test-plugins/**/*.ts'],
less: ['plugins/**/*.less'],
templates: ['plugins/**/*.html'],
testTemplates: ['test-plugins/**/*.html'],
templateModule: pkg.name + '-templates',
testTemplateModule: pkg.name + '-test-templates',
dist: argv.out || './dist/',
js: pkg.name + '.js',
testJs: pkg.name + '-test.js',
css: pkg.name + '.css',
tsProject: plugins.typescript.createProject({
target: 'ES5',
module: 'commonjs',
declarationFiles: true,
noExternalResolve: false,
}),
testTsProject: plugins.typescript.createProject({
target: 'ES5',
module: 'commonjs',
declarationFiles: false,
noExternalResolve: false
})
};
var normalSizeOptions = {
showFiles: true
}, gZippedSizeOptions = {
showFiles: true,
gzip: true
};
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('example-tsc', ['tsc'], function() {
var tsResult = gulp.src(config.testTs)
.pipe(plugins.typescript(config.testTsProject))
.on('error', plugins.notify.onError({
onLast: true,
message: '<%= error.message %>',
title: 'Typescript compilation error - test'
}));
return tsResult.js
.pipe(plugins.concat('test-compiled.js'))
.pipe(gulp.dest('.'));
});
gulp.task('example-template', ['example-tsc'], function() {
return gulp.src(config.testTemplates)
.pipe(plugins.angularTemplatecache({
filename: 'test-templates.js',
root: 'test-plugins/',
standalone: true,
module: config.testTemplateModule,
templateFooter: '}]); hawtioPluginLoader.addModule("' + config.testTemplateModule + '");'
}))
.pipe(gulp.dest('.'));
});
gulp.task('example-concat', ['example-template'], function() {
return gulp.src(['test-compiled.js', 'test-templates.js'])
.pipe(plugins.concat(config.testJs))
.pipe(gulp.dest(config.dist));
});
gulp.task('example-clean', ['example-concat'], function() {
return del(['test-templates.js', 'test-compiled.js']);
});
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'
}))
.on('error', function(error) {
argv._.forEach(function(arg) {
if (arg === 'build') {
throw "Compilation error, halting build";
}
});
});
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() {
var gZipSize = size(gZippedSizeOptions);
return gulp.src(['compiled.js', 'templates.js'])
.pipe(plugins.concat(config.js))
.pipe(plugins.ngAnnotate())
.on('error', plugins.notify.onError({
onLast: true,
message: '<%= error.message %>',
title: 'ng-annotate error'
}))
.pipe(gulp.dest(config.dist));
});
gulp.task('clean', ['concat'], function() {
return del(['templates.js', 'compiled.js']);
});
gulp.task('watch', ['build', 'build-example'], 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']);
});
plugins.watch([config.testTs, config.testTemplates], function() {
gulp.start(['example-tsc', 'example-template', 'example-concat', 'example-clean']);
});
plugins.watch(config.less, function(){
gulp.start('less', 'reload');
})
});
gulp.task('connect', ['watch'], function() {
/*
* Example of fetching a URL from the environment, in this case for kubernetes
var kube = uri(process.env.KUBERNETES_MASTER || 'http://localhost:8080');
console.log("Connecting to Kubernetes on: " + kube);
*/
hawtio.setConfig({
port: 2772,
staticProxies: [
/*
// proxy to a service, in this case kubernetes
{
proto: kube.protocol(),
port: kube.port(),
hostname: kube.hostname(),
path: '/services/kubernetes',
targetPath: kube.path()
},
// proxy to a jolokia instance
{
proto: kube.protocol(),
hostname: kube.hostname(),
port: kube.port(),
path: '/jolokia',
targetPath: '/hawtio/jolokia'
}
*/
],
staticAssets: [{
path: '/',
dir: '.'
}],
fallback: 'index.html',
liveReload: {
enabled: true
}
});
/*
* Example middleware that returns a 404 for templates
* as they're already embedded in the js
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 {
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('build-example', ['example-tsc', 'example-template', 'example-concat', 'example-clean']);
gulp.task('default', ['connect']);