blob: eb755a2be0eaf0820edc81f04ccaded766d84544 [file] [log] [blame]
Michael Landoefa037d2017-02-19 12:57:33 +02001'use strict';
2
AviZi280f8012017-06-09 02:39:56 +03003let gulp = require('gulp');
4let gulpHelpers = require('gulp-helpers');
5let replace = require('gulp-replace');
6let taskMaker = gulpHelpers.taskMaker(gulp);
7let runSequence = gulpHelpers.framework('run-sequence');
AviZi280f8012017-06-09 02:39:56 +03008let gulpCssUsage = require('gulp-css-usage').default;
Avi Zivb8e2faf2017-07-18 19:45:38 +03009
10let prodTask = require('./tools/gulp/tasks/prod');
11let i18nTask = require('./tools/gulp/tasks/i18n.js');
12
AviZi280f8012017-06-09 02:39:56 +030013let jsonConfig = {
14 "appContextPath" : "/onboarding"
15};
Michael Landoefa037d2017-02-19 12:57:33 +020016
Michael Landoefa037d2017-02-19 12:57:33 +020017try {
AviZi280f8012017-06-09 02:39:56 +030018 jsonConfig = require('./src/sdc-app/config/config.json');
Michael Landoefa037d2017-02-19 12:57:33 +020019} catch (e) {
Avi Zivb8e2faf2017-07-18 19:45:38 +030020 console.log('could not load config. using default value instead');
Michael Landoefa037d2017-02-19 12:57:33 +020021}
22
AviZi280f8012017-06-09 02:39:56 +030023const appName = 'onboarding';
24const dist = 'dist';
25
26const path = {
Avi Zivb8e2faf2017-07-18 19:45:38 +030027 // inputs
AviZi280f8012017-06-09 02:39:56 +030028 json: './src/**/*.json',
29 index: './src/index.html',
30 heat: './src/heat.html',
31 scss: './resources/scss/**/*.scss',
Avi Zivb8e2faf2017-07-18 19:45:38 +030032 i18nBundles: './src/nfvo-utils/i18n/*.json',
AviZi280f8012017-06-09 02:39:56 +030033 svgSrc: './resources/images/svg/*.svg',
Avi Zivb8e2faf2017-07-18 19:45:38 +030034 appinf: './webapp-onboarding/**/*.*',
35 jetty: './webapp-onboarding/WEB-INF/jetty-web.xml',
36 srcDir: './src/',
37 // output
38 output: dist,
39 css: dist + '/css',
AviZi280f8012017-06-09 02:39:56 +030040 svg: dist + '/resources/images/svg',
Avi Zivb8e2faf2017-07-18 19:45:38 +030041 appinf_output: dist + '/webapp-onboarding',
42 // war
43 war: [dist + '/index.html', dist + '/punch-outs*.js', dist + '/**/*.{css,png,svg,eot,ttf,woff,woff2,otf}', dist + '/**/*(config.json)', dist + '/webapp-onboarding/**'],
44 heatWar: [dist + '/heat.html', dist + '/heat-validation_en.js', dist + '/**/*.{css,png,svg,eot,ttf,woff,woff2,otf}', dist + '/**/*(config.json)', 'webapp-heat-validation/**'],
AviZi280f8012017-06-09 02:39:56 +030045 wardest: dist,
Avi Zivb8e2faf2017-07-18 19:45:38 +030046 // storybook
AviZi280f8012017-06-09 02:39:56 +030047 storybookFonts: './.storybook/fonts/*',
48 storybookDist: './.storybook-dist',
49 storybookResources: './.storybook/resources/onboarding/resources/images/svg',
50 storybookDistResources: './.storybook-dist/onboarding/resources/images/svg'
51};
Avi Zivb8e2faf2017-07-18 19:45:38 +030052// cleans up the output directory
AviZi280f8012017-06-09 02:39:56 +030053taskMaker.defineTask('clean', {taskName: 'clean', src: path.output});
Avi Zivb8e2faf2017-07-18 19:45:38 +030054// copies for all relevant files to the output directory
AviZi280f8012017-06-09 02:39:56 +030055taskMaker.defineTask('copy', {taskName: 'copy-json', src: path.json, dest: path.output, changed: {extension: '.json'}});
56taskMaker.defineTask('copy', {taskName: 'copy-index.html', src: path.index, dest: path.output, rename: 'index.html'});
57taskMaker.defineTask('copy', {taskName: 'copy-heat.html', src: path.heat, dest: path.output, rename: 'heat.html'});
58taskMaker.defineTask('copy', {taskName: 'copy-svg', src: path.svgSrc, dest: path.svg});
AviZi280f8012017-06-09 02:39:56 +030059taskMaker.defineTask('copy', {taskName: 'copy-storybook-fonts', src: path.storybookFonts, dest: path.storybookDist});
60taskMaker.defineTask('copy', {taskName: 'copy-storybook-resources', src: path.svgSrc, dest: path.storybookResources});
61taskMaker.defineTask('copy', {taskName: 'copy-storybook-resources-prod', src: path.svgSrc, dest: path.storybookDistResources});
Avi Zivb8e2faf2017-07-18 19:45:38 +030062// used for compressing war files
63taskMaker.defineTask('compress', {taskName: 'compress-war', src: path.war, filename: appName + '.war', dest: path.wardest});
64taskMaker.defineTask('compress', {taskName: 'compress-heat-war', src: path.heatWar, filename: 'heat-validation.war', dest: path.wardest});
65// used for watching for changes for test
66taskMaker.defineTask('watch', {taskName: 'watch-stuff', src: [path.json, path.index, path.heat], tasks: ['copy-stuff']});
AviZi280f8012017-06-09 02:39:56 +030067
Avi Zivb8e2faf2017-07-18 19:45:38 +030068
69//TODO: delete this task after gulp-css-usage support for SCSS files
70taskMaker.defineTask('sass', {taskName: 'sass', src: path.scss, dest: path.css, config: {outputStyle: 'compressed'}});
71
72
73// update the app-context for the web-xml file to the value from the config
AviZi280f8012017-06-09 02:39:56 +030074gulp.task('app-context', function(){
75 gulp.src([path.appinf])
76 .pipe(gulp.dest(path.appinf_output))
77 .on('end', function () {
78 gulp.src([path.jetty])
79 .pipe(replace(/<Set name="contextPath">.*<\/Set>/g, '<Set name="contextPath">'+jsonConfig.appContextPath+'</Set>'))
80 .pipe(gulp.dest(path.appinf_output + '/WEB-INF'));
81 })
Michael Landoefa037d2017-02-19 12:57:33 +020082});
Avi Zivb8e2faf2017-07-18 19:45:38 +030083// aggregates all copy tasks
AviZi280f8012017-06-09 02:39:56 +030084gulp.task('copy-stuff', callback => runSequence(['copy-json', 'copy-index.html', 'copy-heat.html', 'copy-svg', 'app-context'], callback));
85
Avi Zivb8e2faf2017-07-18 19:45:38 +030086// minimum build for dev
87gulp.task('dev', callback => runSequence('clean', 'copy-stuff', callback));
88// build procedure for war file
89gulp.task('build', callback => runSequence('clean', 'copy-stuff', 'prod', ['compress-war', 'compress-heat-war'], callback));
90// default build is set to 'dev'
Michael Landoefa037d2017-02-19 12:57:33 +020091gulp.task('default', ['dev']);
Avi Zivb8e2faf2017-07-18 19:45:38 +030092// creating the webpack tasks for the production build
93gulp.task('prod', () => prodTask({outDir: path.output, i18nBundles : path.i18nBundles})
AviZi280f8012017-06-09 02:39:56 +030094 .catch(err => {
95 if (err && err.stack) {
96 console.error(err, err.stack);
Michael Landoefa037d2017-02-19 12:57:33 +020097 }
AviZi280f8012017-06-09 02:39:56 +030098 throw new Error('Webpack build FAILED');
Michael Landoefa037d2017-02-19 12:57:33 +020099 })
AviZi280f8012017-06-09 02:39:56 +0300100);
101
Avi Zivb8e2faf2017-07-18 19:45:38 +0300102/***
103 * T O O L S . N O T P A R T O F B U I L D
104 */
AviZi280f8012017-06-09 02:39:56 +0300105
Avi Zivb8e2faf2017-07-18 19:45:38 +0300106// this is used to manually run on the sass files to check which classes are never used. not run as part of build.
107// can be run as npm task
AviZi280f8012017-06-09 02:39:56 +0300108gulp.task('gulp-css-usage', () => {
109 return gulp.src('src/**/*.jsx').pipe(gulpCssUsage({css: path.css + '/style.css', babylon: ['objectRestSpread']}));
Michael Landoefa037d2017-02-19 12:57:33 +0200110});
111
AviZi280f8012017-06-09 02:39:56 +0300112gulp.task('css-usage', () => {
Michael Landoefa037d2017-02-19 12:57:33 +0200113 runSequence('sass', 'gulp-css-usage');
114});
115
Avi Zivb8e2faf2017-07-18 19:45:38 +0300116
117gulp.task('static-keys-bundle', () => i18nTask({outDir: path.output, srcDir: path.srcDir})
118 .catch(err => {
119 throw new Error('static-keys-bundle FAILED');
120 })
121);
122
123gulp.task('static-keys-bundle-with-report', () => i18nTask({outDir: path.output, srcDir: path.srcDir, i18nBundles : path.i18nBundles })
124 .catch(err => {
125 throw new Error('static-keys-bundle FAILED');
126 })
127);