-
Notifications
You must be signed in to change notification settings - Fork 6
/
gulpfile.js
263 lines (229 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
/**
* IMPORTANT: This file is installed into the app directory as part of the
* npm install fxos-build process. Please do not try to modify this file
* or create additional build steps without checking with the team first.
*
* For any build system feature requests or bugs, please open
* an issue in the fxos-build project: https://github.com/fxos/build/issues
*/
var gulp = require('gulp');
var concat = require('gulp-concat');
var babel = require('gulp-babel');
var rename = require('gulp-rename');
var eslint = require('gulp-eslint');
var zip = require('gulp-zip');
var del = require('del');
var runSequence = require('run-sequence');
var webserver = require('gulp-webserver');
var exec = require('child_process').exec;
var mocha = require('gulp-mocha');
var gls = require('gulp-live-server');
var gsww = require('gulp-sww');
var package = require('./package.json');
const APP_ROOT = './app/';
const TESTS_UNIT_ROOT = './tests/unit/';
const DIST_ROOT = './dist/';
const DIST_APP_ROOT = './dist/app/';
const DIST_TESTS_UNIT_ROOT = './dist/tests/unit/';
var webserverStream;
var foxboxSimulator;
var registrationServerSimulator;
/**
* Runs eslint on all javascript files found in the app and tests dirs.
*/
gulp.task('lint', function() {
// Note: To have the process exit with an error code (1) on lint error, return
// the stream and pipe to failOnError last.
return gulp.src([
'app/**/*.{js,jsx}',
'tests/**/*.js',
'!app/components/**',
'!app/hookSW.js'
])
.pipe(eslint())
.pipe(eslint.format())
.pipe(eslint.failAfterError());
});
/**
* copies necessary files for the Babel amd loader to the app.
*/
gulp.task('loader-polyfill', function() {
return gulp.src(['./node_modules/alameda/alameda.js', 'app/js/bootstrap.js'])
.pipe(concat('initapp.js'))
.pipe(gulp.dest(DIST_APP_ROOT + 'js'));
});
/**
* Copy all non-js directory app source/assets/components.
*/
gulp.task('copy-app', function() {
return gulp.src([
APP_ROOT + '**',
'!' + APP_ROOT + 'js/**/*.js' // do not copy js
])
.pipe(gulp.dest(DIST_APP_ROOT));
});
/**
* converts javascript to es5. this allows us to use harmony classes and modules.
*/
gulp.task('babel-app', function() {
var files = [
APP_ROOT + 'js/**/*.js',
APP_ROOT + 'js/**/*.jsx'
];
try {
return gulp.src(files)
.pipe(babel().on('error', function(e) {
console.log('error running Babel', e);
})
)
.pipe(gulp.dest(DIST_APP_ROOT + 'js/'));
} catch (e) {
console.log('Got error in Babel', e);
}
});
/**
* Process unit test files with Babel.
*/
gulp.task('babel-unit-tests', function() {
try {
return gulp.src([TESTS_UNIT_ROOT + '**/*.js'])
.pipe(
babel().on('error', function(e) {
console.error('Error occurred while running Babel', e);
})
)
.pipe(gulp.dest(DIST_TESTS_UNIT_ROOT));
} catch (e) {
console.error('Error occurred while process unit test files with Babel', e);
}
});
gulp.task('remove-useless', function() {
return del([
DIST_APP_ROOT + 'js/**/*.jsx',
DIST_APP_ROOT + '**/*.md'
]);
});
/**
* Packages the application into a zip.
*/
gulp.task('zip', function() {
return gulp.src(DIST_APP_ROOT)
.pipe(zip('app.zip'))
.pipe(gulp.dest(DIST_ROOT));
});
/**
* Runs travis tests
*/
gulp.task('travis', ['lint', 'loader-polyfill', 'babel-app']);
/**
* Build the app.
*/
gulp.task('build', function(cb) {
runSequence(
['lint'], ['clobber-app'], ['loader-polyfill', 'copy-app'], ['babel-app'],
['remove-useless'], cb
);
});
/**
* Build unit tests.
*/
gulp.task('build-unit-tests', function(cb) {
runSequence('clobber-tests', 'babel-unit-tests', cb);
});
/**
* Watch for changes on the file system, and rebuild if so.
*/
gulp.task('watch', function() {
gulp.watch([APP_ROOT + '**'], ['build']);
});
gulp.task('webserver', function() {
webserverStream = gulp.src('dist/app')
.pipe(webserver({
port: process.env.PORT || 8000,
host: process.env.HOSTNAME || 'localhost',
livereload: false,
directoryListing: false,
open: false
}));
});
gulp.task('stop-webserver', function() {
webserverStream.emit('kill');
});
/**
* The default task when `gulp` is run.
* Adds a listener which will re-build on a file save.
*/
gulp.task('default', function() {
runSequence('build', 'webserver', 'watch');
});
/**
* Remove the distributable files.
*/
gulp.task('clobber-app', function() {
return del('dist/app');
});
gulp.task('clobber-tests', function() {
return del('dist/tests');
});
/**
* Add ServiceWorker support and cache all application
* assets.
*/
gulp.task('offline', ['build'], function() {
gulp.src(['**/*'], {cwd: DIST_APP_ROOT})
.pipe(gsww({
version: package.version,
hookSW: 'hookSW.js'
}))
.pipe(gulp.dest(DIST_APP_ROOT));
});
/**
* Cleans all created files by this gulpfile, and node_modules.
*/
gulp.task('clean', function() {
return del([
'dist/',
'node_modules/'
]);
});
gulp.task('start-simulators', function() {
foxboxSimulator = gls('tests/foxbox-simulator/http-server.js', undefined, false);
foxboxSimulator.start();
registrationServerSimulator = gls('tests/registration-server-simulator/http-server.js', undefined, false);
registrationServerSimulator.start();
});
gulp.task('stop-simulators', function() {
foxboxSimulator.stop();
registrationServerSimulator.stop();
});
gulp.task('run-unit-tests', function (cb) {
runSequence('build-unit-tests', function() {
var Server = require('karma').Server;
(new Server(
{ configFile: __dirname + '/karma.conf.js', singleRun: true }, cb
)).start();
});
});
gulp.task('run-test-integration', function() {
return gulp.src('./tests/{common,integration}/**/*_test.js', { read: false }).pipe(mocha());
});
gulp.task('test-integration', function(cb) {
return runSequence('start-simulators', 'run-test-integration', 'stop-simulators', cb);
});
gulp.task('run-test-e2e', function() {
return gulp.src('./tests/{common,e2e}/**/*_test.js', { read: false }).pipe(mocha());
});
gulp.task('test', function(cb) {
runSequence(
'build',
'run-unit-tests',
'webserver',
'test-integration',
'stop-webserver',
cb
);
});
// TODO: Should be included in 'test' once less manual steps are required
gulp.task('test-e2e', function() {
runSequence('build', 'webserver', 'run-test-e2e', 'stop-webserver');
});