blob: 2be71f30f3e8577412cd94dfac4900bf8a4b5eb7 [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',
Avi Ziv341f43a2017-07-30 12:34:05 +030036 healthCheckInput: './external-resources/healthcheck/v1.0/healthcheck.json',
Avi Zivb8e2faf2017-07-18 19:45:38 +030037 srcDir: './src/',
38 // output
39 output: dist,
40 css: dist + '/css',
AviZi280f8012017-06-09 02:39:56 +030041 svg: dist + '/resources/images/svg',
Avi Zivb8e2faf2017-07-18 19:45:38 +030042 appinf_output: dist + '/webapp-onboarding',
Avi Ziv341f43a2017-07-30 12:34:05 +030043 healthCheckOutput: dist + '/v1.0',
Avi Zivb8e2faf2017-07-18 19:45:38 +030044 // war
Avi Ziv341f43a2017-07-30 12:34:05 +030045 war: [dist + '/index.html', dist + '/punch-outs*.js', dist + '/**/*.{css,png,svg,eot,ttf,woff,woff2,otf}', dist + '/**/*(config.json)', dist + '/webapp-onboarding/**', dist + '/**/*(healthcheck.json)'],
Avi Zivb8e2faf2017-07-18 19:45:38 +030046 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 +030047 wardest: dist,
Avi Zivb8e2faf2017-07-18 19:45:38 +030048 // storybook
AviZi280f8012017-06-09 02:39:56 +030049 storybookFonts: './.storybook/fonts/*',
50 storybookDist: './.storybook-dist',
51 storybookResources: './.storybook/resources/onboarding/resources/images/svg',
52 storybookDistResources: './.storybook-dist/onboarding/resources/images/svg'
53};
Avi Zivb8e2faf2017-07-18 19:45:38 +030054// cleans up the output directory
AviZi280f8012017-06-09 02:39:56 +030055taskMaker.defineTask('clean', {taskName: 'clean', src: path.output});
Avi Zivb8e2faf2017-07-18 19:45:38 +030056// copies for all relevant files to the output directory
AviZi280f8012017-06-09 02:39:56 +030057taskMaker.defineTask('copy', {taskName: 'copy-json', src: path.json, dest: path.output, changed: {extension: '.json'}});
58taskMaker.defineTask('copy', {taskName: 'copy-index.html', src: path.index, dest: path.output, rename: 'index.html'});
59taskMaker.defineTask('copy', {taskName: 'copy-heat.html', src: path.heat, dest: path.output, rename: 'heat.html'});
60taskMaker.defineTask('copy', {taskName: 'copy-svg', src: path.svgSrc, dest: path.svg});
AviZi280f8012017-06-09 02:39:56 +030061taskMaker.defineTask('copy', {taskName: 'copy-storybook-fonts', src: path.storybookFonts, dest: path.storybookDist});
62taskMaker.defineTask('copy', {taskName: 'copy-storybook-resources', src: path.svgSrc, dest: path.storybookResources});
63taskMaker.defineTask('copy', {taskName: 'copy-storybook-resources-prod', src: path.svgSrc, dest: path.storybookDistResources});
Avi Zivb8e2faf2017-07-18 19:45:38 +030064// used for compressing war files
65taskMaker.defineTask('compress', {taskName: 'compress-war', src: path.war, filename: appName + '.war', dest: path.wardest});
66taskMaker.defineTask('compress', {taskName: 'compress-heat-war', src: path.heatWar, filename: 'heat-validation.war', dest: path.wardest});
67// used for watching for changes for test
68taskMaker.defineTask('watch', {taskName: 'watch-stuff', src: [path.json, path.index, path.heat], tasks: ['copy-stuff']});
AviZi280f8012017-06-09 02:39:56 +030069
Avi Zivb8e2faf2017-07-18 19:45:38 +030070
71//TODO: delete this task after gulp-css-usage support for SCSS files
72taskMaker.defineTask('sass', {taskName: 'sass', src: path.scss, dest: path.css, config: {outputStyle: 'compressed'}});
73
Avi Ziv341f43a2017-07-30 12:34:05 +030074// copy the healthcheck file and replace the version with command line argument
75gulp.task('healthcheck', function(){
76 let args = process.argv;
77 let versionArg = args.find(arg => arg.startsWith('--version'));
78 let version = versionArg && versionArg.slice(versionArg.indexOf('=') + 1);
79 if (versionArg) {
80 gulp.src(path.healthCheckInput)
81 .pipe(replace('{VERSION}', version))
82 .pipe(gulp.dest(path.healthCheckOutput));
83 }
84});
Avi Zivb8e2faf2017-07-18 19:45:38 +030085
86// update the app-context for the web-xml file to the value from the config
AviZi280f8012017-06-09 02:39:56 +030087gulp.task('app-context', function(){
88 gulp.src([path.appinf])
89 .pipe(gulp.dest(path.appinf_output))
90 .on('end', function () {
91 gulp.src([path.jetty])
92 .pipe(replace(/<Set name="contextPath">.*<\/Set>/g, '<Set name="contextPath">'+jsonConfig.appContextPath+'</Set>'))
93 .pipe(gulp.dest(path.appinf_output + '/WEB-INF'));
94 })
Michael Landoefa037d2017-02-19 12:57:33 +020095});
Avi Zivb8e2faf2017-07-18 19:45:38 +030096// aggregates all copy tasks
AviZi280f8012017-06-09 02:39:56 +030097gulp.task('copy-stuff', callback => runSequence(['copy-json', 'copy-index.html', 'copy-heat.html', 'copy-svg', 'app-context'], callback));
98
Avi Zivb8e2faf2017-07-18 19:45:38 +030099// minimum build for dev
100gulp.task('dev', callback => runSequence('clean', 'copy-stuff', callback));
101// build procedure for war file
Avi Ziv341f43a2017-07-30 12:34:05 +0300102gulp.task('build', callback => runSequence('clean', 'copy-stuff', 'healthcheck', 'prod', ['compress-war', 'compress-heat-war'], callback));
Avi Zivb8e2faf2017-07-18 19:45:38 +0300103// default build is set to 'dev'
Michael Landoefa037d2017-02-19 12:57:33 +0200104gulp.task('default', ['dev']);
Avi Zivb8e2faf2017-07-18 19:45:38 +0300105// creating the webpack tasks for the production build
106gulp.task('prod', () => prodTask({outDir: path.output, i18nBundles : path.i18nBundles})
AviZi280f8012017-06-09 02:39:56 +0300107 .catch(err => {
108 if (err && err.stack) {
109 console.error(err, err.stack);
Michael Landoefa037d2017-02-19 12:57:33 +0200110 }
AviZi280f8012017-06-09 02:39:56 +0300111 throw new Error('Webpack build FAILED');
Michael Landoefa037d2017-02-19 12:57:33 +0200112 })
AviZi280f8012017-06-09 02:39:56 +0300113);
114
Avi Zivb8e2faf2017-07-18 19:45:38 +0300115/***
116 * T O O L S . N O T P A R T O F B U I L D
117 */
AviZi280f8012017-06-09 02:39:56 +0300118
Avi Zivb8e2faf2017-07-18 19:45:38 +0300119// this is used to manually run on the sass files to check which classes are never used. not run as part of build.
120// can be run as npm task
AviZi280f8012017-06-09 02:39:56 +0300121gulp.task('gulp-css-usage', () => {
122 return gulp.src('src/**/*.jsx').pipe(gulpCssUsage({css: path.css + '/style.css', babylon: ['objectRestSpread']}));
Michael Landoefa037d2017-02-19 12:57:33 +0200123});
124
AviZi280f8012017-06-09 02:39:56 +0300125gulp.task('css-usage', () => {
Michael Landoefa037d2017-02-19 12:57:33 +0200126 runSequence('sass', 'gulp-css-usage');
127});
128
Avi Zivb8e2faf2017-07-18 19:45:38 +0300129
130gulp.task('static-keys-bundle', () => i18nTask({outDir: path.output, srcDir: path.srcDir})
131 .catch(err => {
132 throw new Error('static-keys-bundle FAILED');
133 })
134);
135
136gulp.task('static-keys-bundle-with-report', () => i18nTask({outDir: path.output, srcDir: path.srcDir, i18nBundles : path.i18nBundles })
137 .catch(err => {
138 throw new Error('static-keys-bundle FAILED');
139 })
140);