Fix node filter get methods
[sdc.git] / openecomp-ui / gulpfile.js
1 'use strict';
2
3 let gulp = require('gulp');
4
5 let replace = require('gulp-replace');
6 let del = require('del');
7 let zip = require('gulp-zip');
8 let gulpSass = require('gulp-sass');
9 let runSequence = require('run-sequence');
10 let gulpCssUsage = require('gulp-css-usage').default;
11 let prodTask = require('./tools/gulp/tasks/prod');
12 let i18nTask = require('./tools/gulp/tasks/i18n.js');
13
14 let jsonConfig = {
15         "appContextPath" : "/onboarding"
16 };
17
18 try {
19         jsonConfig = require('./src/sdc-app/config/config.json');
20 } catch (e) {
21         console.log('could not load config. using default value instead');
22 }
23
24 const appName = 'onboarding';
25 const dist = 'dist';
26
27 const path = {
28         // inputs
29         json: './src/**/*.json',
30         index: './src/index.html',
31         heat: './src/heat.html',
32         scss: './resources/scss/**/*.scss',
33         i18nBundles: './src/nfvo-utils/i18n/*.json',
34         svgSrc: './resources/images/svg/*.svg',
35         appinf: './webapp-onboarding/**/*.*',
36         jetty: './webapp-onboarding/WEB-INF/jetty-web.xml',
37         healthCheckInput: './external-resources/healthcheck/healthcheck',
38         srcDir: './src/',
39         // output
40         output: dist,
41         css: dist + '/css',
42 //      svg: dist + '/resources/images/svg',
43         appinf_output: dist + '/webapp-onboarding',
44         healthCheckOutput: dist + '/v1.0',
45         // war
46         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)'],
47         heatWar: [dist + '/heat.html', dist + '/heat-validation_en.js', dist + '/**/*.{css,png,svg,eot,ttf,woff,woff2,otf}', dist + '/**/*(config.json)', 'webapp-heat-validation/**'],
48         wardest: dist,
49         // storybook
50         storybookFonts: './.storybook/fonts/*',
51         storybookDist: './.storybook-dist',
52         //storybookResources: './.storybook/resources/onboarding/resources/images/svg',
53         //storybookDistResources: './.storybook-dist/onboarding/resources/images/svg'
54 };
55 // cleans up the output directory
56
57 gulp.task('clean', callback => {
58         return del([path.output], callback);
59 })
60 // copies for all relevant files to the output directory'
61
62 gulp.task('copy-json', () => {
63         gulp.src(path.json)
64                 .pipe(gulp.dest(path.output));
65 });
66
67 gulp.task('copy-index.html', () => {
68         gulp.src(path.index)
69                 .pipe(gulp.dest(path.output));
70 });
71
72 gulp.task('copy-heat.html', () => {
73         gulp.src(path.heat)
74                 .pipe(gulp.dest(path.output));
75 });
76
77 gulp.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  */
87 gulp.task('compress-war', ()=> {
88         gulp.src(path.war)
89                 .pipe(zip(appName + '.war'))
90                 .pipe(gulp.dest(path.wardest));
91 });
92
93 gulp.task('compress-heat-war', ()=> {
94         gulp.src(path.heatWar)
95                 .pipe(zip('heat-validation.war'))
96                 .pipe(gulp.dest(path.wardest));
97 });
98
99 //TODO: delete this task after gulp-css-usage support for SCSS files
100 gulp.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
107
108 // copy the healthcheck file and replace the version with command line argument
109 gulp.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 });
119
120 // update the app-context for the web-xml file to the value from the config
121 gulp.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                 })
129 });
130 // aggregates all copy tasks
131 gulp.task('copy-stuff', callback => runSequence(['copy-json', 'copy-index.html', 'copy-heat.html', 'app-context'], callback));
132
133 // minimum build for dev
134 gulp.task('dev', callback => runSequence('clean', 'copy-stuff', callback));
135 // build procedure for war file
136 gulp.task('build', callback => runSequence('clean', 'copy-stuff', 'healthcheck', 'prod', ['compress-war', 'compress-heat-war'], callback));
137 // default build is set to 'dev'
138 gulp.task('default', ['dev']);
139 // creating the webpack tasks for the production build
140 gulp.task('prod', () => prodTask({outDir: path.output, i18nBundles : path.i18nBundles})
141         .catch(err => {
142                 if (err && err.stack) {
143                         console.error(err, err.stack);
144                 }
145                 throw new Error('Webpack build FAILED');
146         })
147 );
148
149 /***
150  * T O O L S .   N O T   P A R T   O F    B U I L D
151  */
152
153 // 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
155 gulp.task('gulp-css-usage', () => {
156         return gulp.src('src/**/*.jsx').pipe(gulpCssUsage({css: path.css + '/style.css', babylon: ['objectRestSpread']}));
157 });
158
159 gulp.task('css-usage', () => {
160         runSequence('sass', 'gulp-css-usage');
161 });
162
163
164 gulp.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
170 gulp.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 );