forked from niondir/iot-dashboard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
283 lines (232 loc) · 8.59 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
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
const gulp = require('gulp');
const gutil = require('gulp-util');
const sequence = require('gulp-sequence');
const open = require('open');
////////////////////
// Main Tasks
////////////////////
if (!process.env.NODE_ENV) {
process.env.NODE_ENV = gutil.env.production ? 'production' : 'development';
}
gutil.log("NODE_ENV = '" + process.env.NODE_ENV + "'");
/**
* Setup everything for a smooth development
*/
gulp.task("dev", sequence(['inject', 'copy'], 'webpack:dev-server'));
/**
* Keeps files up to date that are not covered by Webpack
*/
gulp.task('watch', ["inject:tests", "copy"], function () {
gulp.watch("src/**/*.test.js", ['inject:tests']);
gulp.watch("src/**/*.test.ts", ['inject:tests']);
gulp.watch("plugins/**/*", ['copy:plugins']);
});
/**
* Build everything required for a successful CI build
* TODO: Due to webpack foo we need to build tests first and than compile the client! (try webpack-stream?)
* see: https://github.com/webpack/webpack/issues/2787
* */
gulp.task("build", sequence('test', ['compile', 'lint']));
/**
* Compile all code to /dist
* - no tests, no overhead, just what is needed to generate a runnable application
* */
gulp.task('compile', sequence('copy:plugins', 'webpack:client'));
// TODO: We do not have uiTests yet. All tests are running with node
// There is some ui test code already but it's considered unstable (should we just delete it for now?)
gulp.task('test', ['mocha']);
//////////////////
// Environment
//////////////////
gulp.task('set-dev-node-env', function () {
return process.env.NODE_ENV = 'development';
});
gulp.task('set-prod-node-env', function () {
return process.env.NODE_ENV = 'production';
});
//////////////////
// Testing Tasks
//////////////////
var mocha = require('gulp-mocha');
var istanbul = require('gulp-istanbul');
gulp.task('mocha', ['mocha:tests']);
gulp.task('mocha:tests', ['webpack:tests'], function () {
return gulp.src('dist/tests.bundle.js', {read: false})
// gulp-mocha needs filepaths so you can't have any plugins before it
.pipe(mocha({reporter: 'spec'})) // more details with 'spec', more fun with 'nyan'
.pipe(istanbul.writeReports({
coverageVariable: '__coverage__',
reporters: ['json', 'text-summary', 'lcovonly']
})).pipe(istanbul.writeReports({
coverageVariable: '__coverage__',
reporters: ['html'],
reportOpts: {
html: {dir: 'dist/coverage'}
}
}));
});
/*
gulp.task('mocha:ui-tests', ['inject:tests', 'webpack:ui-tests'], function () {
return gulp
.src('dist/ui-tests.html')
.pipe(mochaPhantomJS({reporter: 'spec', dump: 'ui-tests.log'}));
});*/
//////////////////
// Lint Tasks
// ///////////////
const eslint = require('gulp-eslint');
const tslint = require("gulp-tslint");
// TODO: Add tslint for typescript
gulp.task('lint', ['eslint', 'tslint']);
gulp.task('eslint', function () {
return gulp.src(['src/**/*.js'])
// eslint() attaches the lint output to the "eslint" property
// of the file object so it can be used by other modules.
.pipe(eslint())
// eslint.format() outputs the lint results to the console.
// Alternatively use eslint.formatEach() (see Docs).
.pipe(eslint.format())
// To have the process exit with an error code (1) on
// lint error, return the stream and pipe to failAfterError last.
.pipe(eslint.failAfterError());
});
gulp.task("tslint", () =>
gulp.src("src/**/*.ts")
.pipe(tslint({
formatter: "prose" // prose or verbose
}))
.pipe(tslint.report({
emitError: true,
summarizeFailureOutput: true
}))
);
//////////////////
// Compile Tasks
//////////////////
const webpack = require('webpack');
const ts = require('gulp-typescript');
var tsProject = ts.createProject('./tsconfig.json');
/**
* It's not yet intended to compile the TS code without webpack.
* But if someone wants to use parts of the code as library this might get handy.
*/
gulp.task('compile:ts', [], function () {
var tsResult = tsProject.src()
.pipe(ts(tsProject));
return tsResult.js.pipe(gulp.dest('./lib'));
});
const webpackErrorHandler = function (callback, error, stats) {
if (error) {
//gutil.log("------------------------------------------------");
//gutil.log("error: ", error);
//throw new Error("Failed");
throw new gutil.PluginError('webpack', error);
}
gutil.log('[webpack]', stats.toString());
callback();
};
gulp.task('webpack', sequence('webpack:tests', 'webpack:client'));
gulp.task('webpack:client', ['compile:config'], function (callback) {
var webpackConfig = require('./webpack.client.js');
webpack(webpackConfig, webpackErrorHandler.bind(this, callback));
});
gulp.task('webpack:tests', ['inject:tests', 'compile:config'], function (callback) {
var webpackConfig = require('./webpack.tests.js');
webpack(webpackConfig, webpackErrorHandler.bind(this, callback));
});
gulp.task('webpack:browser-tests', ['inject:tests', 'compile:config'], function (callback) {
var webpackConfig = require('./webpack.browser-tests.js');
webpack(webpackConfig, webpackErrorHandler.bind(this, callback));
});
var jeditor = require("gulp-json-editor");
var git = require('git-rev-sync');
var packageJson = require('./package.json');
gulp.task('compile:config', function () {
return gulp.src("./src/config.json")
.pipe(jeditor(function (json) {
json.version = packageJson.version;
json.revision = git.long();
json.revisionShort = git.short();
json.branch = git.branch();
return json;
}))
.pipe(gulp.dest("./src"));
});
//////////////////////////////
// Inject/Modify files Tasks
//////////////////////////////
var inject = require('gulp-inject');
var sort = require('gulp-sort');
gulp.task('inject', ['inject:tests']);
gulp.task('inject:tests', function () {
// It's not necessary to read the files (will speed up things), we're only after their paths:
var sources = gulp.src(['./src/**/*.test.js', './src/**/*.test.ts'], {read: false})
.pipe(sort());
return gulp.src(['./src/tests.ts', './src/browser-tests.js'])
.pipe(inject(sources, {
relative: true,
starttag: '/* inject:tests */',
endtag: '/* endinject */',
transform: function (filepath, file, i, length) {
return "import './" + filepath + "'";
}
}))
.pipe(gulp.dest('./src'));
});
///////////////
// Clean Tasks
///////////////
var del = require('del');
gulp.task('clean', ['clean:dist', 'clean:lib']);
gulp.task('clean:dist', function () {
return del([
'dist/**/*'
]);
});
gulp.task('clean:lib', function () {
return del([
'lib/**/*'
]);
});
///////////////
// Copy Tasks
///////////////
gulp.task('copy', ['copy:plugins']);
gulp.task('copy:plugins', function () {
gulp.src('./plugins/**/*.*')
.pipe(gulp.dest('./dist/plugins'));
});
//////////////////////
// Webpack Dev-Server
//////////////////////
var WebpackDevServer = require("webpack-dev-server");
gulp.task("webpack:dev-server", ['copy', 'inject', 'compile:ts'], function (callback) {
// Start a webpack-dev-server
var webpackConfig = require('./webpack.client.js');
webpackConfig.entry.app.unshift("webpack-dev-server/client?http://localhost:8080/", "webpack/hot/dev-server");
webpackConfig.entry["browser-tests"].unshift("webpack-dev-server/client?http://localhost:8080/", "webpack/hot/dev-server");
webpackConfig.bail = false;
webpackConfig.devtool = '#cheap-module-source-map';
var compiler = webpack(webpackConfig, function(err, stats) {
if(err) throw new gutil.PluginError("webpack", err);
open("http://localhost:8080");
open("http://localhost:8080/webpack-dev-server/tests.html");
callback();
});
new WebpackDevServer(compiler, {
// server and middleware options
contentBase: './dist',
publicPath: "/",
hot: true
}).listen(8080, "localhost", function (err) {
//if (err) throw new gutil.PluginError("webpack-dev-server", err);
if (err) console.error(err);
// Server listening
gutil.log("[webpack-dev-server]", "http://localhost:8080/webpack-dev-server/index.html");
// keep the server alive or continue?
// callback();
});
});