forked from SC5/sc5-devicewall
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
314 lines (266 loc) · 9.88 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
"use strict";
var path = require('path'),
extend = require('extend'),
util = require('util'),
gulp = require('gulp'),
browsersync = require('browser-sync'),
bowerFiles = require('main-bower-files'),
$ = require('gulp-load-plugins')(),
templateCache = require('gulp-angular-templatecache'),
eventStream = require('event-stream'),
packagejson = require('./package.json'),
fs = require('fs'),
scssLint = require('gulp-scss-lint'),
server = require('gulp-develop-server'),
find = require('find'),
del = require('del');
var configLocalServer = './config/server';
var configLocalApp = './config/app';
/* Configurations. Note that most of the configuration is stored in
the task context. These are mainly for repeating configuration items */
var config = {
version: packagejson.version,
debug: Boolean($.util.env.debug),
browsersync: Boolean($.util.env.browsersync),
server: {
host: process.env.npm_package_config_server_host || "localhost",
maxSockets: parseInt(process.env.npm_package_config_server_maxSockets) || 100,
port: parseInt(process.env.npm_package_config_server_port) || 8080
}
};
// Package management
/* Install & update Bower dependencies */
gulp.task('install', ['config', 'bower'], function() {
gulp.start('integrate');
});
/* Bump version number for packagejson.json & bower.json */
// TODO Provide means for appending a patch id based on git commit id or md5 hash
gulp.task('bump', function() {
// Fetch whether we're bumping major, minor or patch; default to minor
var env = $.util.env,
type = (env.major) ? 'major' : (env.patch) ? 'patch' : 'minor';
gulp.src(['./bower.json', './package.json'])
.pipe($.bump({ type: type }))
.pipe(gulp.dest('./'));
});
// install will run this task _before_ bower install task
gulp.task('config', function() {
var data; // just a tmp variable
// Server config
var serverConfig = JSON.parse(fs.readFileSync(configLocalServer+'/config.json'));
serverConfig.version = packagejson.version;
serverConfig = extend(true, serverConfig, config.server);
// read local config
if (fs.existsSync(configLocalServer+'/config.local.json')) {
data = JSON.parse(fs.readFileSync(configLocalServer+'/config.local.json'));
serverConfig = extend(true, serverConfig, data);
}
fs.writeFileSync('./config.json', JSON.stringify(serverConfig, null, 2));
// Server test config
var serverTestConfig = JSON.parse(fs.readFileSync(configLocalServer + '/config.test.json'));
// read local config
if (fs.existsSync(configLocalServer + '/config.test.local.json')) {
data = JSON.parse(fs.readFileSync(configLocalServer + '/config.test.local.json'));
serverTestConfig = extend(true, serverTestConfig, data);
}
serverTestConfig = extend(true, serverConfig, serverTestConfig);
fs.writeFileSync('./config.test.json', JSON.stringify(serverTestConfig, null, 2));
// Control panel app config
var clientConfig = JSON.parse(fs.readFileSync(configLocalApp + '/config.json'));
clientConfig.appConfig.version = packagejson.version;
clientConfig.appConfig.socketServer = config.protocol + '://' + serverConfig.host + ':' + serverConfig.port + '/devicewall';
// read local config
if (fs.existsSync(configLocalApp+'/config.local.json')) {
data = JSON.parse(fs.readFileSync(configLocalApp+'/config.local.json'));
clientConfig = extend(true, clientConfig, data);
}
// Write angular configuration
return gulp.src(configLocalApp + '/config.json')
.pipe($.ngConstant({
name: 'configuration',
constants: clientConfig
}))
.pipe(gulp.dest('./src/app'));
});
// Cleanup
gulp.task('clean', function(cb) {
del(['dist/**', 'temp/**'], cb);
});
/* Serve the web site */
gulp.task('serve', $.serve({
root: 'dist',
port: config.server.controlPort
}));
gulp.task('preprocess', ['config'/*, 'scss-lint'*/], function() {
gulp.src(['src/app/**/*.js', 'server*.js'])
.pipe($.jshint())
.pipe($.jshint.reporter('default'));
});
gulp.task('scss-lint', function() {
var lintTask = gulp.src('src/css/**/*.scss')
.pipe(scssLint());
// without --force flag scss-lint fails the task
if (!$.util.env.force) {
lintTask.pipe(scssLint.failReporter());
}
});
gulp.task('javascript', ['preprocess'], function() {
// The non-MD5fied prefix, so that we know which version we are actually
// referring to in case of fixing bugs
var bundleName = util.format('bundle-%s.js', config.version);
// Note: two pipes get combined together by first
// combining components into one bundle, then adding
// app sources, and reordering the items. Note that
// we expect Angular to be the first item in bower.json
// so that component concatenation works
var components = gulp.src(['src/components/socket.io-client/socket.io.js'].concat(bowerFiles()))
// Socket.io does not specify main file in the manifest; add it manually
.pipe($.filter('**/*.js', '!jquery*', '!foundation*'))
.pipe($.plumber())
.pipe($.concat('components.js'));
var templates = gulp.src('src/assets/**/*.html')
.pipe(templateCache('templates.js', { standalone: true, root: 'assets' }));
var app = gulp.src('src/app/**/*.js')
.pipe($.concat('app.js'));
return eventStream.merge(components, app, templates)
.pipe($.order([
'**/components.js',
'**/templates.js',
'**/app.js'
]))
.pipe($.concat(bundleName))
.pipe($.if(!config.debug, $.ngAnnotate()))
.pipe($.if(!config.debug, $.uglify()))
.pipe(gulp.dest('dist'));
});
gulp.task('stylesheets', function() {
// The non-MD5fied prefix, so that we know which version we are actually
// referring to in case of fixing bugs
var bundleName = util.format('styles-%s.css', config.version);
return gulp.src('src/css/styles.scss')
.pipe($.plumber())
// Compile
.pipe($.compass({
project: path.join(__dirname, 'src'),
sass: 'css',
css: '../temp/css'
}))
// Unit test
// Integrate (link, packagejson, concatenate)
.pipe($.concat(bundleName))
// Integrate
.pipe(gulp.dest('dist/css'));
// Integration test
// oh why css lint for css generated by sass? .pipe($.csslint())
//.pipe($.csslint.reporter());
});
gulp.task('assets', function() {
return gulp.src('src/assets/**')
.pipe(gulp.dest('dist/assets'));
// Integration test
});
gulp.task('favicon', function() {
return gulp.src(['src/favicon.ico'])
.pipe(gulp.dest('dist'));
});
gulp.task('integrate', ['javascript', 'stylesheets', 'assets', 'favicon'], function() {
var target = gulp.src('src/index.html');
var sources = gulp.src(['dist/*.js', 'dist/css/*.css'], {read: false});
return target.pipe($.inject(sources, { ignorePath: ['/dist/'], addRootSlash: false }))
.pipe(gulp.dest('./dist'));
});
gulp.task('livereload', function() {
// Only livereload if the HTML (or other static assets) are changed, because
// the HTML will change for any JS or CSS change
gulp.src('dist/**', { read: false })
.pipe($.watch())
.pipe($.livereload(parseInt(process.env.LIVERELOAD_PORT) || 35729));
});
/* Watch, live reload and serve the web site with browsersync */
gulp.task('browsersync', function() {
var bs = browsersync.init('dist/*', {
server: {
baseDir: 'dist'
}
});
});
gulp.task('watch', ['build'], function() {
$.nodemon({script: 'server/server.js', watch: 'server/**/*.js'})
.on('restart', function () {
console.log('express restarted!');
});
return gulp.watch([
'server/**/*.js',
'config/**/*.json',
'src/css/**/*.scss',
'src/app/**/*.js',
'src/assets/**/*',
'src/index.html',
'!src/app/config.js', // do not listen files which are modified during build process
], ['build']);
});
// Tests
gulp.task('webdriver_manager_update', $.protractor.webdriver_update);
gulp.task('test:server', function(cb) {
gulp.src(['server/**/*.js', '!server/test/**/*'])
.pipe($.istanbul()) // Covering files
.pipe($.istanbul.hookRequire())
.on('finish', function () {
gulp.src('server/test/**/*.spec.js')
.pipe($.jasmine())
.pipe($.istanbul.writeReports()) // Creating the reports after tests runned
.on('end', cb);
});
});
gulp.task('test:e2e', ['webdriver_manager_update'], function() {
var testConfig = require('./config.test.json');
var testDataDir = path.dirname(path.resolve(testConfig.devicesJson));
var args = [ '--baseUrl', config.protocol + '://' + testConfig.host + ':' + testConfig.port];
var protractorConf = {
configFile: './protractor.config.js',
args: [args],
debug: Boolean($.util.env.debug)
};
if (!fs.existsSync(testDataDir)) {
fs.mkdirSync(testDataDir);
}
server.listen({path: './server/server.js', env: {"NODE_ENV": "test"}});
gulp.src(['tests/e2e/**/*.js'], { read: false })
.pipe($.protractor.protractor(protractorConf)).on('error', function() {
server.kill();
throw new Error('Selenium e2e tests failed.');
}).on('end', function() {
server.kill();
});
});
gulp.task('test:e2e:ci', function() {
var testConfig = require('./config.test.json');
var testDataDir = path.dirname(path.resolve(testConfig.devicesJson));
var args = [
'--baseUrl',
config.protocol + '://' + testConfig.host + ':' + testConfig.port
];
var protractorConf = {
configFile: './protractor.ci.config.js',
args: [args]
};
if (!fs.existsSync(testDataDir)) {
fs.mkdirSync(testDataDir);
}
server.listen({path: './server/server.js', env: {"NODE_ENV": "test"}});
gulp.src(['tests/e2e/**/*.js'], { read: false })
.pipe($.protractor.protractor(protractorConf)).on('error', function() {
server.kill();
throw new Error('Selenium e2e tests failed.');
}).on('end', function() {
server.kill();
});
});
gulp.task('test', ['test:e2e']);
gulp.task('default', ['integrate']);
gulp.task('build', ['clean'], function() {
gulp.start('integrate');
});
gulp.task('bower', function() {
return $.bower();
});