forked from kpi-wdc/dj
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.babel.js
394 lines (345 loc) · 11.7 KB
/
gulpfile.babel.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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
import del from 'del';
import fs from 'fs';
import gulp from 'gulp';
import gulpLoadPlugins from 'gulp-load-plugins';
import path from 'path';
import runSequence from 'run-sequence';
// HELPER FUNCTIONS
const isFlagPositive = (value) => value !== undefined && value !== 'false';
const isEnvEnabled = name => isFlagPositive(process.env[name]);
function handleError(err) {
console.log(err.toString());
this.emit('end');
}
// BUILD CONFIGURATION
const buildDir = '.tmp';
const buildPublicDir = '.tmp/public';
// BUILD SETTINGS
const production = isEnvEnabled('PRODUCTION');
const minifyCode = production || isEnvEnabled('MINIFY_CODE');
const npmProduction = isEnvEnabled('NPM_CONFIG_PRODUCTION');
const showFilesLog = true;
// LOG SETTINGS
console.log(`Production mode: ${production}`);
console.log(`Minifying code: ${minifyCode}`);
// The following means devDependencies are not installed
console.log(`NPM in production mode: ${npmProduction}`);
// Get gulp plugins
const conf = npmProduction ? {
scope: ['dependencies']
} : undefined;
const plugins = gulpLoadPlugins(conf);
gulp.task('default', ['build']);
gulp.task('build', ['build-css', 'build-js', 'build-translations', 'merge-widget-configs',
'copy-templates-json', 'build-template-images', 'copy-static-files']);
gulp.task('bower-install', ['generate-bower-json'], () =>
plugins.bower({cwd: '.tmp'}, [undefined, {
'forceLatest': true
}]).on('error', handleError)
);
// collect all bower-dependencies in collectedBowerDeps object
const widgetsWithDeps = []
const widgetBowerPackagePrefix = 'widget-';
gulp.task('collect-widgets-with-deps', () =>
gulp.src('assets/widgets/*/bower.json')
.pipe(plugins.jsonEditor(json => {
widgetsWithDeps.push(json.name);
return json;
}))
.on('error', handleError)
);
gulp.task('generate-bower-json', ['collect-widgets-with-deps'], () =>
gulp.src('bower.json')
.pipe(plugins.jsonEditor(json => {
for (let dep in widgetsWithDeps) {
const widgetName = widgetsWithDeps[dep];
json.dependencies[widgetBowerPackagePrefix + widgetName] = `../assets/widgets/${widgetName}/`;
}
json.resolutions = json.dependencies;
return json;
}))
.on('error', handleError)
.pipe(plugins.extend('bower.json'))
.pipe(gulp.dest('.tmp'))
);
gulp.task('build-components', ['bower-install'], () => {
const removeFilter = plugins.filter([
'**/*',
'!**/test/**',
'!**/examples/**',
'!**/grunt/**',
'!**/tests/**',
'!**/*.md',
'!**/*.gzip',
'!**/*.scss',
'!**/*.ts',
'!**/*.coffee',
'!**/package.json',
'!**/grunt.js',
'!**/bower.json'
]);
return gulp.src([
'.tmp/bower_components/**/*',
`!.tmp/bower_components/${widgetBowerPackagePrefix}*/**`
])
.pipe(plugins.cached('build-components'))
.pipe(removeFilter)
.pipe(plugins.if(showFilesLog, plugins.size({showFiles: true, title: 'components'})))
.on('error', handleError)
.pipe(gulp.dest(`${buildPublicDir}/components`));
});
gulp.task('build-css', ['build-less', 'build-components'], () => {
if (!minifyCode) {
return;
}
return gulp.src(`${buildPublicDir}/**/*.css`)
.pipe(plugins.cached('build-css'))
.on('error', handleError)
.pipe(plugins.if(showFilesLog, plugins.size({showFiles: true, title: 'CSS'})))
.pipe(gulp.dest(buildPublicDir));
});
gulp.task('build-less', () =>
gulp.src('assets/css/**/*.less')
.pipe(plugins.cached('build-less'))
.pipe(gulp.dest(`${buildPublicDir}/css`))
.pipe(plugins.lessSourcemap())
.on('error', handleError)
.pipe(plugins.if(showFilesLog, plugins.size({showFiles: true, title: 'LESS -> CSS'})))
.pipe(gulp.dest(`${buildPublicDir}/css`))
);
gulp.task('build-template-cache', () =>
gulp.src('assets/**/*.html')
.pipe(plugins.if(minifyCode, plugins.minifyHtml({
empty: true,
loose: true
})))
.on('error', handleError)
.pipe(gulp.dest(buildPublicDir))
.pipe(plugins.angularTemplatecache('templates.js', {
standalone: true,
module: 'app.templates',
moduleSystem: 'RequireJS'
}))
.pipe(gulp.dest(`${buildPublicDir}/js`))
);
gulp.task('copy-es6-polyfill', () =>
gulp.src('node_modules/gulp-babel/node_modules/babel-core/browser-polyfill.js')
.pipe(plugins.rename('es6-polyfill.js'))
.pipe(plugins.changed(`${buildPublicDir}/js`))
.pipe(gulp.dest(`${buildPublicDir}/js`))
);
gulp.task('build-js', ['build-template-cache', 'build-widgets', 'build-components',
'compile-js', 'annotate-js', 'copy-es6-polyfill'], () => {
if (!minifyCode) {
return;
}
gulp.src(`${buildPublicDir}/**/*.js`)
.pipe(plugins.cached('build-js'))
.pipe(plugins.plumber())
.pipe(plugins.if(minifyCode, plugins.uglify()))
.on('error', handleError)
.pipe(plugins.if(showFilesLog, plugins.size({showFiles: true, title: 'JS'})))
.pipe(gulp.dest(buildPublicDir));
});
gulp.task('compile-js', () =>
gulp.src('assets/js/**/*.js')
.pipe(plugins.cached('compile-js'))
.pipe(plugins.changed(`${buildPublicDir}/js`))
.pipe(plugins.plumber())
.pipe(plugins.sourcemaps.init())
.pipe(plugins.babel())
.on('error', handleError)
.pipe(plugins.sourcemaps.write('.'))
.pipe(gulp.dest(`${buildPublicDir}/js`))
);
gulp.task('annotate-js', ['compile-js'], () =>
gulp.src([`${buildPublicDir}/**/*.js`, `!${buildPublicDir}/widgets/**/*`,
`!${buildPublicDir}/components/**/*`])
.pipe(plugins.cached('annotate-js'))
.pipe(plugins.ngAnnotate())
.on('error', handleError)
.pipe(gulp.dest(buildPublicDir))
);
gulp.task('move-widgets', () =>
// Move everything except JS-code which is handled separately in build-widgets-js
gulp.src(['assets/widgets/**', '!assets/widgets/**/*.js'])
.pipe(plugins.cached('move-widgets'))
.pipe(plugins.changed(`${buildPublicDir}/widgets`))
.pipe(gulp.dest(`${buildPublicDir}/widgets`))
);
gulp.task('build-widgets-js', () =>
gulp.src('assets/widgets/**/*.js')
.pipe(plugins.cached('build-widgets-js'))
.pipe(plugins.changed(`${buildPublicDir}/widgets`))
.pipe(plugins.sourcemaps.init())
.pipe(plugins.plumber())
.pipe(plugins.babel())
.pipe(plugins.ngAnnotate())
.on('error', handleError)
.pipe(plugins.sourcemaps.write('.'))
.pipe(gulp.dest(`${buildPublicDir}/widgets`))
);
gulp.task('build-widgets', ['move-widgets', 'build-widgets-js']);
gulp.task('build-translations', (done) => {
fs.readdir('assets/i18n', (err, files) => {
const supportedLanguages = files.map(file => file.slice(0, -5));
let tasksLeft = supportedLanguages.length;
for (let lang of supportedLanguages) {
gulp.src(`assets/widgets/*/i18n/${lang}.json`)
.pipe(plugins.plumber())
.pipe(plugins.modify({
fileModifier(file, contents) {
const widgetName = file.history[file.history.length - 1].substr(file.base.length).split(path.sep)[0];
return JSON.stringify({
'WIDGET': {
[widgetName.toUpperCase()]: JSON.parse(contents)
}
});
}
}))
.pipe(plugins.addSrc(`assets/i18n/${lang}.json`))
.pipe(plugins.extend(`${lang}.json`))
.on('error', handleError)
.pipe(gulp.dest(`${buildPublicDir}/i18n`))
.on('end', () => {
if (!--tasksLeft) {
done();
}
});
}
});
});
gulp.task('merge-widget-configs', () =>
gulp.src('assets/widgets/**/widget.json')
.pipe(plugins.tap(file => {
const dir = path.basename(path.dirname(file.path));
file.contents = Buffer.concat([
new Buffer(`{"${dir}": `),
file.contents,
new Buffer('}')
]);
}))
.pipe(plugins.extend('widgets.json'))
.on('error', handleError)
.pipe(gulp.dest(`${buildPublicDir}/widgets`))
);
gulp.task('copy-templates-json', () =>
gulp.src('assets/templates/templates.json')
.pipe(plugins.cached('copy-templates-json'))
.pipe(gulp.dest(`${buildPublicDir}/templates`))
);
gulp.task('build-template-images', () =>
gulp.src('assets/templates/**/icon.png')
.pipe(plugins.cached('build-template-images'))
.pipe(gulp.dest(`${buildPublicDir}/templates`))
);
gulp.task('copy-static-files', () =>
gulp.src([
'assets/img/**', 'assets/data/**',
'assets/favicon.ico'], {base: 'assets'})
.pipe(plugins.cached('copy-static-files'))
.pipe(gulp.dest(buildPublicDir))
);
if (!npmProduction) {
gulp.task('test', ['unit-test']);
gulp.task('unit-test', ['copy-es6-polyfill',
'build-template-cache',
'build-components'], function (done) {
const karma = require('karma').server;
const conf = {
configFile: `${__dirname}/karma.conf.js`,
singleRun: true
};
conf.browsers = ['PhantomJS'];
karma.start(conf, done);
});
gulp.task('coveralls', () =>
gulp.src(`${buildDir}/coverage/**/lcov.info`, {base: `${buildDir}/..`})
.pipe(plugins.coveralls())
);
gulp.task('e2e-test', ['e2e-run-test']);
// Downloads the selenium webdriver
gulp.task('webdriver-update', plugins.protractor.webdriver_update);
gulp.task('e2e-run-test', ['webdriver-update', 'build', 'build-e2e-test'], function (cb) {
let called = false;
gulp.src([`${buildDir}/test/e2e/**/*Spec.js`])
.pipe(plugins.protractor.protractor({
configFile: `${__dirname}/protractor.conf.js`
}))
.on('error', e => {
if (isEnvEnabled('CI')) {
fs.exists('protractor.log', exists => {
if (!exists) {
console.log('protractor.log not found!');
console.log('Skipping end-to-end tests...');
if (!called) {
called = true;
cb();
}
}
});
} else {
throw e;
}
})
.on('end', () => {
if (!called) {
called = true;
cb();
}
});
});
gulp.task('build-e2e-test', () =>
gulp.src('test/e2e/**/*.js')
.pipe(plugins.changed(`${buildDir}test/e2e`))
.pipe(plugins.babel())
.on('error', handleError)
.pipe(gulp.dest(`${buildDir}/test/e2e`))
);
gulp.task('docs', () =>
plugins.shell.task([
`node` +
` ${path.join('node_modules', 'angular-jsdoc', 'node_modules', 'jsdoc', 'jsdoc.js')}` +
` -c ${path.join('node_modules', 'angular-jsdoc', 'conf.json')}` + // config file
` -t ${path.join('node_modules', 'angular-jsdoc', 'template')}` + // template file
` -d ${path.join('docs')}` + // output directory
` -r ${path.join('assets', 'js')}` + // source code directory
` README.md`
])().on('error', handleError)
);
const bump = importance =>
// get all the files to bump version in
gulp.src(['./package.json', './bower.json'])
// bump the version number in those files
.pipe(plugins.bump({type: importance}))
// save it back to filesystem
.pipe(gulp.dest('./'))
// commit the changed version number
.pipe(plugins.git.commit('Bumps package version'))
// read only one file to get the version number
.pipe(plugins.filter('package.json'))
// **tag it in the repository**
.pipe(plugins.tagVersion());
gulp.task('patch', () => bump('patch'));
gulp.task('feature', () => bump('minor'));
gulp.task('release', () => bump('major'));
// Rerun the task when a file changes
gulp.task('watch', ['build', 'build-e2e-test'], () =>
gulp.watch(['assets/**', 'test/**', 'bower.json'], ['build', 'build-e2e-test'])
);
// Rerun the task when a file changes
gulp.task('watch-unit-test', ['build'], done => {
const karma = require('karma').server;
const conf = {
configFile: `${__dirname}/karma.conf.js`,
singleRun: false
};
if (process.env.CI) {
conf.browsers = ['PhantomJS'];
}
karma.start(conf, done);
});
}
gulp.task('clean', done =>
del([buildDir], done)
);