blob: 331724b75707ff32946f88cb400c0998905a8f2c [file] [log] [blame]
Michael Landoefa037d2017-02-19 12:57:33 +02001'use strict';
2
AviZi280f8012017-06-09 02:39:56 +03003let gulp = require('gulp');
Avi Zivb8e2faf2017-07-18 19:45:38 +03004
svishnevd9a5fea2018-01-23 16:55:40 +02005let replace = require('gulp-replace');
6let del = require('del');
7let zip = require('gulp-zip');
8let gulpSass = require('gulp-sass');
9let runSequence = require('run-sequence');
10let gulpCssUsage = require('gulp-css-usage').default;
Avi Zivb8e2faf2017-07-18 19:45:38 +030011let prodTask = require('./tools/gulp/tasks/prod');
12let i18nTask = require('./tools/gulp/tasks/i18n.js');
13
AviZi280f8012017-06-09 02:39:56 +030014let jsonConfig = {
15 "appContextPath" : "/onboarding"
16};
Michael Landoefa037d2017-02-19 12:57:33 +020017
Michael Landoefa037d2017-02-19 12:57:33 +020018try {
AviZi280f8012017-06-09 02:39:56 +030019 jsonConfig = require('./src/sdc-app/config/config.json');
Michael Landoefa037d2017-02-19 12:57:33 +020020} catch (e) {
Avi Zivb8e2faf2017-07-18 19:45:38 +030021 console.log('could not load config. using default value instead');
Michael Landoefa037d2017-02-19 12:57:33 +020022}
23
AviZi280f8012017-06-09 02:39:56 +030024const appName = 'onboarding';
25const dist = 'dist';
26
27const path = {
Avi Zivb8e2faf2017-07-18 19:45:38 +030028 // inputs
AviZi280f8012017-06-09 02:39:56 +030029 json: './src/**/*.json',
30 index: './src/index.html',
31 heat: './src/heat.html',
32 scss: './resources/scss/**/*.scss',
Avi Zivb8e2faf2017-07-18 19:45:38 +030033 i18nBundles: './src/nfvo-utils/i18n/*.json',
AviZi280f8012017-06-09 02:39:56 +030034 svgSrc: './resources/images/svg/*.svg',
Avi Zivb8e2faf2017-07-18 19:45:38 +030035 appinf: './webapp-onboarding/**/*.*',
36 jetty: './webapp-onboarding/WEB-INF/jetty-web.xml',
Avi Zivf15292d2017-07-31 15:50:46 +030037 healthCheckInput: './external-resources/healthcheck/healthcheck',
Avi Zivb8e2faf2017-07-18 19:45:38 +030038 srcDir: './src/',
39 // output
40 output: dist,
41 css: dist + '/css',
talig8e9c0652017-12-20 14:30:43 +020042// svg: dist + '/resources/images/svg',
Avi Zivb8e2faf2017-07-18 19:45:38 +030043 appinf_output: dist + '/webapp-onboarding',
Avi Ziv341f43a2017-07-30 12:34:05 +030044 healthCheckOutput: dist + '/v1.0',
Avi Zivb8e2faf2017-07-18 19:45:38 +030045 // war
Avi Zivf15292d2017-07-31 15:50:46 +030046 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)'],
Avi Zivb8e2faf2017-07-18 19:45:38 +030047 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 +030048 wardest: dist,
Avi Zivb8e2faf2017-07-18 19:45:38 +030049 // storybook
AviZi280f8012017-06-09 02:39:56 +030050 storybookFonts: './.storybook/fonts/*',
51 storybookDist: './.storybook-dist',
talig8e9c0652017-12-20 14:30:43 +020052 //storybookResources: './.storybook/resources/onboarding/resources/images/svg',
53 //storybookDistResources: './.storybook-dist/onboarding/resources/images/svg'
AviZi280f8012017-06-09 02:39:56 +030054};
Avi Zivb8e2faf2017-07-18 19:45:38 +030055// cleans up the output directory
AviZi280f8012017-06-09 02:39:56 +030056
svishnevd9a5fea2018-01-23 16:55:40 +020057gulp.task('clean', callback => {
58 return del([path.output], callback);
59})
60// copies for all relevant files to the output directory'
61
62gulp.task('copy-json', () => {
63 gulp.src(path.json)
64 .pipe(gulp.dest(path.output));
65});
66
67gulp.task('copy-index.html', () => {
68 gulp.src(path.index)
69 .pipe(gulp.dest(path.output));
70});
71
72gulp.task('copy-heat.html', () => {
73 gulp.src(path.heat)
74 .pipe(gulp.dest(path.output));
75});
76
77gulp.task('copy-storybook-fonts', () => {
78 gulp.src(path.storybookFonts)
79 .pipe(gulp.dest(path.storybookDist));
80});
81
82// used for compressing war files
83
84/**
85 * replaced with gulp
86 */
87gulp.task('compress-war', ()=> {
88 gulp.src(path.war)
89 .pipe(zip(appName + '.war'))
90 .pipe(gulp.dest(path.wardest));
91});
92
93gulp.task('compress-heat-war', ()=> {
94 gulp.src(path.heatWar)
95 .pipe(zip('heat-validation.war'))
96 .pipe(gulp.dest(path.wardest));
97});
Avi Zivb8e2faf2017-07-18 19:45:38 +030098
99//TODO: delete this task after gulp-css-usage support for SCSS files
svishnevd9a5fea2018-01-23 16:55:40 +0200100gulp.task('sass', () => {
101 return gulp.src(path.scss)
102 .pipe(gulpSass({outputStyle: 'compressed'}).on('error', gulpSass.logError))
103 .pipe(gulp.dest(path.css));
104});
105
106
Avi Zivb8e2faf2017-07-18 19:45:38 +0300107
Avi Ziv341f43a2017-07-30 12:34:05 +0300108// copy the healthcheck file and replace the version with command line argument
109gulp.task('healthcheck', function(){
110 let args = process.argv;
111 let versionArg = args.find(arg => arg.startsWith('--version'));
112 let version = versionArg && versionArg.slice(versionArg.indexOf('=') + 1);
113 if (versionArg) {
114 gulp.src(path.healthCheckInput)
115 .pipe(replace('{VERSION}', version))
116 .pipe(gulp.dest(path.healthCheckOutput));
117 }
118});
Avi Zivb8e2faf2017-07-18 19:45:38 +0300119
120// update the app-context for the web-xml file to the value from the config
AviZi280f8012017-06-09 02:39:56 +0300121gulp.task('app-context', function(){
122 gulp.src([path.appinf])
123 .pipe(gulp.dest(path.appinf_output))
124 .on('end', function () {
125 gulp.src([path.jetty])
126 .pipe(replace(/<Set name="contextPath">.*<\/Set>/g, '<Set name="contextPath">'+jsonConfig.appContextPath+'</Set>'))
127 .pipe(gulp.dest(path.appinf_output + '/WEB-INF'));
128 })
Michael Landoefa037d2017-02-19 12:57:33 +0200129});
Avi Zivb8e2faf2017-07-18 19:45:38 +0300130// aggregates all copy tasks
talig8e9c0652017-12-20 14:30:43 +0200131gulp.task('copy-stuff', callback => runSequence(['copy-json', 'copy-index.html', 'copy-heat.html', 'app-context'], callback));
AviZi280f8012017-06-09 02:39:56 +0300132
Avi Zivb8e2faf2017-07-18 19:45:38 +0300133// minimum build for dev
134gulp.task('dev', callback => runSequence('clean', 'copy-stuff', callback));
135// build procedure for war file
Avi Ziv341f43a2017-07-30 12:34:05 +0300136gulp.task('build', callback => runSequence('clean', 'copy-stuff', 'healthcheck', 'prod', ['compress-war', 'compress-heat-war'], callback));
Avi Zivb8e2faf2017-07-18 19:45:38 +0300137// default build is set to 'dev'
Michael Landoefa037d2017-02-19 12:57:33 +0200138gulp.task('default', ['dev']);
Avi Zivb8e2faf2017-07-18 19:45:38 +0300139// creating the webpack tasks for the production build
140gulp.task('prod', () => prodTask({outDir: path.output, i18nBundles : path.i18nBundles})
AviZi280f8012017-06-09 02:39:56 +0300141 .catch(err => {
142 if (err && err.stack) {
143 console.error(err, err.stack);
Michael Landoefa037d2017-02-19 12:57:33 +0200144 }
AviZi280f8012017-06-09 02:39:56 +0300145 throw new Error('Webpack build FAILED');
Michael Landoefa037d2017-02-19 12:57:33 +0200146 })
AviZi280f8012017-06-09 02:39:56 +0300147);
148
Avi Zivb8e2faf2017-07-18 19:45:38 +0300149/***
150 * T O O L S . N O T P A R T O F B U I L D
151 */
AviZi280f8012017-06-09 02:39:56 +0300152
Avi Zivb8e2faf2017-07-18 19:45:38 +0300153// this is used to manually run on the sass files to check which classes are never used. not run as part of build.
154// can be run as npm task
AviZi280f8012017-06-09 02:39:56 +0300155gulp.task('gulp-css-usage', () => {
156 return gulp.src('src/**/*.jsx').pipe(gulpCssUsage({css: path.css + '/style.css', babylon: ['objectRestSpread']}));
Michael Landoefa037d2017-02-19 12:57:33 +0200157});
158
AviZi280f8012017-06-09 02:39:56 +0300159gulp.task('css-usage', () => {
Michael Landoefa037d2017-02-19 12:57:33 +0200160 runSequence('sass', 'gulp-css-usage');
161});
162
Avi Zivb8e2faf2017-07-18 19:45:38 +0300163
164gulp.task('static-keys-bundle', () => i18nTask({outDir: path.output, srcDir: path.srcDir})
165 .catch(err => {
166 throw new Error('static-keys-bundle FAILED');
167 })
168);
169
170gulp.task('static-keys-bundle-with-report', () => i18nTask({outDir: path.output, srcDir: path.srcDir, i18nBundles : path.i18nBundles })
171 .catch(err => {
172 throw new Error('static-keys-bundle FAILED');
173 })
174);