forked from hygieia/hygieia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
executable file
·287 lines (244 loc) · 7.95 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
'use strict';
// require what we need
var browserSync = require('browser-sync'),
chalk = require('chalk'),
gulp = require('gulp'),
tmplCache = require('gulp-angular-templatecache'),
change = require('gulp-change'),
clean = require('gulp-clean'),
concat = require('gulp-concat'),
consolidate = require('gulp-consolidate'),
filter = require('gulp-filter'),
flatten = require('gulp-flatten'),
gulpIf = require('gulp-if'),
inject = require('gulp-inject'),
less = require('gulp-less'),
minifyHtml = require('gulp-htmlmin'),
minifyCss = require('gulp-minify-css'),
rename = require('gulp-rename'),
replace = require('gulp-replace'),
httpProxy = require('http-proxy'),
glob = require('glob'),
runSequence = require('run-sequence'),
wiredep = require('wiredep'),
argv = require('yargs').argv,
// some gulp config values
hygieia = {
src: 'src/',
dist: 'dist/'
},
// list of where our js files come from
jsFiles = [
'src/{app,components}/**/*.js'
],
// list of theme files for less processing
themeFiles = [
'src/components/themes/*.less'
],
// look for html files
viewFiles = [
'src/{app,components}/**/*.html'
],
widgetStyleFiles = [
'src/{app,components}/**/*.less'
],
// look for local json files
testDataFiles = [
'src/test-data/*'
],
// config values that will be written to the UI and can
// be overwritten by arguments during the build process.
// if left null, they will not be written to the file
config = {
module: 'hygieia-dashboard',
local: null,
api: null,
refresh: 60
};
// override config values
for(var field in config) {
var val = argv[field];
if(val) {
if(val == 'true' || val == 'false') {
val = val !== 'false';
}
config[field] = val;
}
if(config[field] === null) {
delete config[field];
}
}
/*******************************
* MAIN TASKS
*******************************/
gulp.task('default', ['build']);
// moves everything to the build folder
gulp.task('build', function(callback) {
runSequence('clean', ['assets', 'themes', 'fonts', 'js', 'views', 'test-data'], 'html', callback);
});
// run the build task, start up a browser, then
// watch the different file locations and execute
// the relevant tasks
gulp.task('serve', ['build'], function() {
/*
* Location of your backend server
*/
var proxyTarget = config.api || 'http://localhost:8080';
var proxy = httpProxy.createProxyServer({
target: proxyTarget
});
proxy.on('error', function(error, req, res) {
res.writeHead(500, {
'Content-Type': 'text/plain'
});
console.error(chalk.red('[Proxy]'), error);
});
/*
* The proxy middleware is an Express middleware added to BrowserSync to
* handle backend request and proxy them to your backend.
*/
function proxyMiddleware(req, res, next) {
/*
* Proxy the REST API.
*/
if (/^\/api\/.*/.test(req.url)) {
proxy.web(req, res);
} else {
next();
}
}
browserSync.init({
server: {
baseDir: hygieia.dist,
startPath: '/',
middleware: [proxyMiddleware]
}
});
gulp.watch(jsFiles).on('change', function() {
runSequence('js', browserSync.reload);
});
// watch the less files in addition to the themes
gulp.watch(themeFiles.concat(widgetStyleFiles)).on('change', function() {
runSequence('themes', browserSync.reload);
});
gulp.watch(viewFiles).on('change', function() {
runSequence('views', browserSync.reload);
});
gulp.watch(testDataFiles).on('change', function() {
runSequence('test-data');
});
});
/*******************************
* SUPPORTING TASKS
*******************************/
// delete our distribution folder
gulp.task('clean', function() {
return gulp.src(hygieia.dist).pipe(clean());
});
// move everything in the assets folder to distribution
gulp.task('assets', function() {
return gulp
.src([
hygieia.src + 'assets/**/*'
])
.pipe(gulp.dest(hygieia.dist + 'assets'));
});
// loop through and grab all the theme less files,
// process them, and place them in the styles folder.
// the app adds the references to the stylesheet based
// on user preferences so there is no need to inject the
// files in to the UI directly
gulp.task('themes', function() {
// get a list of widget files to import. this will only work if the theme
// file directly has the insert:widgets code. it will not work as part of an
// imported less file
var widgetLessFiles = glob.sync('src/components/widgets/**/*.less', null);
widgetLessFiles = widgetLessFiles.map(function(file) {
return "@import '" + file.replace(hygieia.src, '../../') + "';";
});
return gulp.src(themeFiles)
.pipe(replace('/** insert:widgets **/', widgetLessFiles.join('')))
.pipe(less({
paths: [
hygieia.src + 'components'
]
}))
.on('error', function(err) {
console.error(err.toString());
this.emit('end');
})
.pipe(minifyCss())
.pipe(gulp.dest(hygieia.dist + 'styles'));
});
// move js files over
gulp.task('js', function() {
var stream = gulp.src(jsFiles);
if(!!argv.prod) {
stream = stream.pipe(concat('app/app.js'));
}
return stream.pipe(gulp.dest(hygieia.dist));
});
// move our html views to a template cache folder so each one
// doesn't need to be happen as an ajax request
gulp.task('views', function() {
return gulp
.src(viewFiles)
.pipe(minifyHtml({
collapseWhitespace: true
}))
.pipe(tmplCache('template-cache.js', {
module: config.module
}))
.pipe(gulp.dest(hygieia.dist));
});
// move our fonts folder
gulp.task('fonts', function() {
return gulp
.src([
'bower_components/**/*'
])
.pipe(filter('**/*.{eot,ttf,woff}'))
.pipe(flatten())
.pipe(gulp.dest(hygieia.dist + 'fonts'));
});
// move the source html files and inject the widget javascript
gulp.task('html', function() {
return gulp
// move the root html files to the distribution folder
.src([
hygieia.src + '*.html',
hygieia.src + 'favicon.ico'
])
.pipe(gulp.dest(hygieia.dist))
// now just work with the main index file
.pipe(filter(['index.html']))
// wiredep replaces bower:js with references to all bower dependencies
.pipe(inject(gulp.src(
wiredep({
exclude: [/bootstrap\.js/, /bootstrap\.css/, /bootstrap\.css/, /foundation\.css/]
}).js)
.pipe(concat('bower.js'))
.pipe(gulp.dest(hygieia.dist + 'bower_components')),
{ name: 'bower', ignorePath: hygieia.dist, addRootSlash: false })
)
.pipe(gulp.dest(hygieia.dist))
// replace inject:js with script references to all the files in the following sources
.pipe(inject(gulp.src(!!argv.prod ? ['src/app/app.js'] : jsFiles), { name: 'hygieia', ignorePath: 'src', addRootSlash: false }))
// replace custom placeholders with our configured values
.pipe(replace('[config]', JSON.stringify(config)))
.pipe(replace('[config-module]', config.module))
// make sure the file is rewritten
.pipe(gulp.dest(hygieia.dist));
});
// move test data files, but only if running locally
gulp.task('test-data', function() {
return gulp
.src(testDataFiles)
.pipe(gulpIf(config.local, gulp.dest(hygieia.dist + 'test-data')));
});
gulp.task('chartist', function() {
return gulp
.src(['src/app/chartist/chartist.less'])
.pipe(less())
.pipe(gulp.dest('./'));
});