/
gulpfile.js
271 lines (222 loc) · 6.36 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
'use strict';
var config = require('./gulp.config')();
var del = require('del');
var glob = require('glob');
var path = require('path');
var args = require('yargs').argv;
var exec = require('child_process').exec;
var browserSync = require('browser-sync');
var tslintStylish = require('tslint-stylish');
var gulp = require('gulp');
var $ = require('gulp-load-plugins')({ lazy: true });
/**
* yargs variables can be passed in to alter the behavior, when present.
* Example: gulp typescript-compile
*
* --verbose : Various tasks will produce more output to the console.
*/
/**
* List the available gulp tasks
*/
gulp.task('help', $.taskListing.withFilters(/:/));
gulp.task('default', ['help']);
/**
* Compile TypeScript
*/
gulp.task('typescript-compile', ['vet:typescript'], function () {
log('Compiling TypeScript');
exec('tsc -p src');
});
/**
* Watch and compile TypeScript
*/
gulp.task('typescript-watch', ['typescript-compile'], function () {
return gulp.watch(config.ts.files, ['typescript-compile']);
});
/**
* Run specs once and exit
* @return {Stream}
*/
gulp.task('tests-run', ['typescript-compile'], function () {
startTests(true /*singleRun*/);
});
/**
* Run specs and wait.
* Watch for file changes and re-run tests on each change
*/
gulp.task('tests-watch', ['typescript-watch'], function () {
startTests(false /*singleRun*/);
});
/**
* Run the spec runner
* @return {Stream}
*/
gulp.task('tests-serve', ['specs:inject', 'imports:inject','typescript-watch'], function () {
log('Running the spec runner');
serveSpecRunner();
});
/**
* Copy files needed for running in production to 'dist' folder.
* After this task you can copy the 'dist' folder to another machine, run 'npm install --production'
* and finally 'node server.js' to run the application.
* @return {Stream}
*/
gulp.task('build-production', ['typescript-compile', 'copy:package.json'], function() {
return gulp;
});
gulp.task('copy:package.json', ['clean:generated'], function() {
return gulp
.src('package.json')
.pipe(gulp.dest('dist/'));
});
/**
* vet typescript code
* @return {Stream}
*/
gulp.task('vet:typescript', function () {
log('Analyzing typescript code with TSLint');
return gulp
.src(config.ts.files)
.pipe($.tslint())
.pipe($.tslint.report(tslintStylish, {
emitError: false,
sort: true,
bell: false
}));
});
/**
* Remove generated files
* @return {Stream}
*/
gulp.task('clean:generated', function () {
log('Cleaning generated files: ' + $.util.colors.blue(config.ts.out));
return del(config.ts.out);
});
/**
* Inject all the spec files into the SpecRunner.html
* @return {Stream}
*/
gulp.task('specs:inject', function () {
log('Injecting scripts into the spec runner');
return gulp
.src(config.specRunner)
.pipe(inject(config.js.src, '', config.js.order))
.pipe(inject(config.js.specs, 'specs', ['**/*']))
.pipe(gulp.dest(config.root));
});
/**
* Inject imports into system.js
* @return {Stream}
*/
gulp.task('imports:inject', function(){
log('Injecting imports into system.js');
gulp.src(config.imports.template)
.pipe(injectString(config.js.specs, 'import'))
.pipe($.rename(config.imports.script))
.pipe(gulp.dest(config.root));
});
////////////////
/**
* Log a message or series of messages using chalk's blue color.
* Can pass in a string, object or array.
*/
function log(msg) {
if (typeof (msg) === 'object') {
for (var item in msg) {
if (msg.hasOwnProperty(item)) {
$.util.log($.util.colors.blue(msg[item]));
}
}
} else {
$.util.log($.util.colors.blue(msg));
}
}
/**
* Start the tests using karma.
* @param {boolean} singleRun - True means run once and end (CI), or keep running (dev)
* @return {undefined}
*/
function startTests(singleRun) {
var Server = require('karma').Server;
log('Karma started');
var server = new Server({
configFile: __dirname + '/karma.conf.js',
exclude: config.karma.exclude,
singleRun: !!singleRun
});
server.on('run_complete', function (browser, result) {
log('Karma completed');
});
server.start();
}
/**
* Order a stream
* @param {Stream} src The gulp.src stream
* @param {Array} order Glob array pattern
* @returns {Stream} The ordered stream
*/
function orderSrc(src, order) {
return gulp
.src(src)
.pipe($.if(order, $.order(order)));
}
/**
* Inject files in a sorted sequence at a specified inject label
* @param {Array} src glob pattern for source files
* @param {String} label The label name
* @param {Array} order glob pattern for sort order of the files
* @returns {Stream} The stream
*/
function inject(src, label, order) {
var options = { read: false, addRootSlash: false };
if (label) {
options.name = 'inject:' + label;
}
return $.inject(orderSrc(src, order), options);
}
/**
* Inject files as strings at a specified inject label
* @param {Array} src glob pattern for source files
* @param {String} label The label name
* @returns {Stream} The stream
*/
function injectString(src, label) {
var search = '/// inject:' + label;
var first = '\n System.import(\'';
var last = '\')';
var specNames = [];
src.forEach(function(pattern) {
glob.sync(pattern)
.forEach(function(file) {
var fileName = path.basename(file, path.extname(file));
var specName = path.join(path.dirname(file), fileName);
specNames.push(first + specName + last);
});
});
return $.injectString.after(search, specNames);
}
/**
* Start BrowserSync
* --verbose
*/
function serveSpecRunner() {
if (browserSync.active) {
return;
}
log('Starting BrowserSync on port ' + config.browserSyncPort);
var options = {
port: config.browserSync.port,
server: config.root,
files: config.js.srcSpecs,
logFileChanges: true,
logLevel: config.browserSync.logLevel,
logPrefix: config.browserSync.logPrefix,
notify: true,
reloadDelay: config.browserSync.reloadDelay,
startPath: config.specRunnerFile
};
if (args.verbose) {
options.logLevel = 'debug';
}
browserSync(options);
}