Add collaboration feature
[sdc.git] / openecomp-ui / gulpfile.js
1 'use strict';
2
3 let gulp = require('gulp');
4 let gulpHelpers = require('gulp-helpers');
5 let replace = require('gulp-replace');
6 let taskMaker = gulpHelpers.taskMaker(gulp);
7 let runSequence = gulpHelpers.framework('run-sequence');
8 let gulpCssUsage = require('gulp-css-usage').default;
9
10 let prodTask = require('./tools/gulp/tasks/prod');
11 let i18nTask = require('./tools/gulp/tasks/i18n.js');
12
13 let jsonConfig = {
14         "appContextPath" : "/onboarding"
15 };
16
17 try {
18         jsonConfig = require('./src/sdc-app/config/config.json');
19 } catch (e) {
20         console.log('could not load config. using default value instead');
21 }
22
23 const appName = 'onboarding';
24 const dist = 'dist';
25
26 const path = {
27         // inputs
28         json: './src/**/*.json',
29         index: './src/index.html',
30         heat: './src/heat.html',
31         scss: './resources/scss/**/*.scss',
32         i18nBundles: './src/nfvo-utils/i18n/*.json',
33         svgSrc: './resources/images/svg/*.svg',
34         appinf: './webapp-onboarding/**/*.*',
35         jetty: './webapp-onboarding/WEB-INF/jetty-web.xml',
36         healthCheckInput: './external-resources/healthcheck/healthcheck',
37         srcDir: './src/',
38         // output
39         output: dist,
40         css: dist + '/css',
41 //      svg: dist + '/resources/images/svg',
42         appinf_output: dist + '/webapp-onboarding',
43         healthCheckOutput: dist + '/v1.0',
44         // war
45         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)'],
46         heatWar: [dist + '/heat.html', dist + '/heat-validation_en.js', dist + '/**/*.{css,png,svg,eot,ttf,woff,woff2,otf}', dist + '/**/*(config.json)', 'webapp-heat-validation/**'],
47         wardest: dist,
48         // storybook
49         storybookFonts: './.storybook/fonts/*',
50         storybookDist: './.storybook-dist',
51         //storybookResources: './.storybook/resources/onboarding/resources/images/svg',
52         //storybookDistResources: './.storybook-dist/onboarding/resources/images/svg'
53 };
54 // cleans up the output directory
55 taskMaker.defineTask('clean', {taskName: 'clean', src: path.output});
56 // copies for all relevant files to the output directory
57 taskMaker.defineTask('copy', {taskName: 'copy-json', src: path.json, dest: path.output, changed: {extension: '.json'}});
58 taskMaker.defineTask('copy', {taskName: 'copy-index.html', src: path.index, dest: path.output, rename: 'index.html'});
59 taskMaker.defineTask('copy', {taskName: 'copy-heat.html', src: path.heat, dest: path.output, rename: 'heat.html'});
60 //taskMaker.defineTask('copy', {taskName: 'copy-svg', src: path.svgSrc, dest: path.svg});
61 taskMaker.defineTask('copy', {taskName: 'copy-storybook-fonts', src: path.storybookFonts, dest: path.storybookDist});
62 //taskMaker.defineTask('copy', {taskName: 'copy-storybook-resources', src: path.svgSrc, dest: path.storybookResources});
63 //taskMaker.defineTask('copy', {taskName: 'copy-storybook-resources-prod', src: path.svgSrc, dest: path.storybookDistResources});
64 // used for compressing war files
65 taskMaker.defineTask('compress', {taskName: 'compress-war', src: path.war, filename: appName + '.war', dest: path.wardest});
66 taskMaker.defineTask('compress', {taskName: 'compress-heat-war', src: path.heatWar, filename: 'heat-validation.war', dest: path.wardest});
67 // used for watching for changes for test
68 taskMaker.defineTask('watch', {taskName: 'watch-stuff', src: [path.json, path.index, path.heat], tasks: ['copy-stuff']});
69
70
71 //TODO: delete this task after gulp-css-usage support for SCSS files
72 taskMaker.defineTask('sass', {taskName: 'sass', src: path.scss, dest: path.css, config: {outputStyle: 'compressed'}});
73
74 // copy the healthcheck file and replace the version with command line argument
75 gulp.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 });
85
86 // update the app-context for the web-xml file to the value from the config
87 gulp.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                 })
95 });
96 // aggregates all copy tasks
97 gulp.task('copy-stuff', callback => runSequence(['copy-json', 'copy-index.html', 'copy-heat.html', 'app-context'], callback));
98
99 // minimum build for dev
100 gulp.task('dev', callback => runSequence('clean', 'copy-stuff', callback));
101 // build procedure for war file
102 gulp.task('build', callback => runSequence('clean', 'copy-stuff', 'healthcheck', 'prod', ['compress-war', 'compress-heat-war'], callback));
103 // default build is set to 'dev'
104 gulp.task('default', ['dev']);
105 // creating the webpack tasks for the production build
106 gulp.task('prod', () => prodTask({outDir: path.output, i18nBundles : path.i18nBundles})
107         .catch(err => {
108                 if (err && err.stack) {
109                         console.error(err, err.stack);
110                 }
111                 throw new Error('Webpack build FAILED');
112         })
113 );
114
115 /***
116  * T O O L S .   N O T   P A R T   O F    B U I L D
117  */
118
119 // 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
121 gulp.task('gulp-css-usage', () => {
122         return gulp.src('src/**/*.jsx').pipe(gulpCssUsage({css: path.css + '/style.css', babylon: ['objectRestSpread']}));
123 });
124
125 gulp.task('css-usage', () => {
126         runSequence('sass', 'gulp-css-usage');
127 });
128
129
130 gulp.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
136 gulp.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 );