Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 1 | 'use strict'; |
| 2 | |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 3 | let gulp = require('gulp'); |
Avi Ziv | b8e2faf | 2017-07-18 19:45:38 +0300 | [diff] [blame] | 4 | |
svishnev | d9a5fea | 2018-01-23 16:55:40 +0200 | [diff] [blame] | 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; |
Avi Ziv | b8e2faf | 2017-07-18 19:45:38 +0300 | [diff] [blame] | 11 | let prodTask = require('./tools/gulp/tasks/prod'); |
| 12 | let i18nTask = require('./tools/gulp/tasks/i18n.js'); |
| 13 | |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 14 | let jsonConfig = { |
| 15 | "appContextPath" : "/onboarding" |
| 16 | }; |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 17 | |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 18 | try { |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 19 | jsonConfig = require('./src/sdc-app/config/config.json'); |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 20 | } catch (e) { |
Avi Ziv | b8e2faf | 2017-07-18 19:45:38 +0300 | [diff] [blame] | 21 | console.log('could not load config. using default value instead'); |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 22 | } |
| 23 | |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 24 | const appName = 'onboarding'; |
| 25 | const dist = 'dist'; |
| 26 | |
| 27 | const path = { |
Avi Ziv | b8e2faf | 2017-07-18 19:45:38 +0300 | [diff] [blame] | 28 | // inputs |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 29 | json: './src/**/*.json', |
| 30 | index: './src/index.html', |
| 31 | heat: './src/heat.html', |
| 32 | scss: './resources/scss/**/*.scss', |
Avi Ziv | b8e2faf | 2017-07-18 19:45:38 +0300 | [diff] [blame] | 33 | i18nBundles: './src/nfvo-utils/i18n/*.json', |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 34 | svgSrc: './resources/images/svg/*.svg', |
Avi Ziv | b8e2faf | 2017-07-18 19:45:38 +0300 | [diff] [blame] | 35 | appinf: './webapp-onboarding/**/*.*', |
| 36 | jetty: './webapp-onboarding/WEB-INF/jetty-web.xml', |
Avi Ziv | f15292d | 2017-07-31 15:50:46 +0300 | [diff] [blame] | 37 | healthCheckInput: './external-resources/healthcheck/healthcheck', |
Avi Ziv | b8e2faf | 2017-07-18 19:45:38 +0300 | [diff] [blame] | 38 | srcDir: './src/', |
| 39 | // output |
| 40 | output: dist, |
| 41 | css: dist + '/css', |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 42 | // svg: dist + '/resources/images/svg', |
Avi Ziv | b8e2faf | 2017-07-18 19:45:38 +0300 | [diff] [blame] | 43 | appinf_output: dist + '/webapp-onboarding', |
Avi Ziv | 341f43a | 2017-07-30 12:34:05 +0300 | [diff] [blame] | 44 | healthCheckOutput: dist + '/v1.0', |
Avi Ziv | b8e2faf | 2017-07-18 19:45:38 +0300 | [diff] [blame] | 45 | // war |
Avi Ziv | f15292d | 2017-07-31 15:50:46 +0300 | [diff] [blame] | 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)'], |
Avi Ziv | b8e2faf | 2017-07-18 19:45:38 +0300 | [diff] [blame] | 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/**'], |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 48 | wardest: dist, |
Avi Ziv | b8e2faf | 2017-07-18 19:45:38 +0300 | [diff] [blame] | 49 | // storybook |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 50 | storybookFonts: './.storybook/fonts/*', |
| 51 | storybookDist: './.storybook-dist', |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 52 | //storybookResources: './.storybook/resources/onboarding/resources/images/svg', |
| 53 | //storybookDistResources: './.storybook-dist/onboarding/resources/images/svg' |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 54 | }; |
Avi Ziv | b8e2faf | 2017-07-18 19:45:38 +0300 | [diff] [blame] | 55 | // cleans up the output directory |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 56 | |
svishnev | d9a5fea | 2018-01-23 16:55:40 +0200 | [diff] [blame] | 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 | }); |
Avi Ziv | b8e2faf | 2017-07-18 19:45:38 +0300 | [diff] [blame] | 98 | |
| 99 | //TODO: delete this task after gulp-css-usage support for SCSS files |
svishnev | d9a5fea | 2018-01-23 16:55:40 +0200 | [diff] [blame] | 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 | |
Avi Ziv | b8e2faf | 2017-07-18 19:45:38 +0300 | [diff] [blame] | 107 | |
Avi Ziv | 341f43a | 2017-07-30 12:34:05 +0300 | [diff] [blame] | 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 | }); |
Avi Ziv | b8e2faf | 2017-07-18 19:45:38 +0300 | [diff] [blame] | 119 | |
| 120 | // update the app-context for the web-xml file to the value from the config |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 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 | }) |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 129 | }); |
Avi Ziv | b8e2faf | 2017-07-18 19:45:38 +0300 | [diff] [blame] | 130 | // aggregates all copy tasks |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 131 | gulp.task('copy-stuff', callback => runSequence(['copy-json', 'copy-index.html', 'copy-heat.html', 'app-context'], callback)); |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 132 | |
Avi Ziv | b8e2faf | 2017-07-18 19:45:38 +0300 | [diff] [blame] | 133 | // minimum build for dev |
| 134 | gulp.task('dev', callback => runSequence('clean', 'copy-stuff', callback)); |
| 135 | // build procedure for war file |
Avi Ziv | 341f43a | 2017-07-30 12:34:05 +0300 | [diff] [blame] | 136 | gulp.task('build', callback => runSequence('clean', 'copy-stuff', 'healthcheck', 'prod', ['compress-war', 'compress-heat-war'], callback)); |
Avi Ziv | b8e2faf | 2017-07-18 19:45:38 +0300 | [diff] [blame] | 137 | // default build is set to 'dev' |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 138 | gulp.task('default', ['dev']); |
Avi Ziv | b8e2faf | 2017-07-18 19:45:38 +0300 | [diff] [blame] | 139 | // creating the webpack tasks for the production build |
| 140 | gulp.task('prod', () => prodTask({outDir: path.output, i18nBundles : path.i18nBundles}) |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 141 | .catch(err => { |
| 142 | if (err && err.stack) { |
| 143 | console.error(err, err.stack); |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 144 | } |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 145 | throw new Error('Webpack build FAILED'); |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 146 | }) |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 147 | ); |
| 148 | |
Avi Ziv | b8e2faf | 2017-07-18 19:45:38 +0300 | [diff] [blame] | 149 | /*** |
| 150 | * T O O L S . N O T P A R T O F B U I L D |
| 151 | */ |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 152 | |
Avi Ziv | b8e2faf | 2017-07-18 19:45:38 +0300 | [diff] [blame] | 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 |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 155 | gulp.task('gulp-css-usage', () => { |
| 156 | return gulp.src('src/**/*.jsx').pipe(gulpCssUsage({css: path.css + '/style.css', babylon: ['objectRestSpread']})); |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 157 | }); |
| 158 | |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 159 | gulp.task('css-usage', () => { |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 160 | runSequence('sass', 'gulp-css-usage'); |
| 161 | }); |
| 162 | |
Avi Ziv | b8e2faf | 2017-07-18 19:45:38 +0300 | [diff] [blame] | 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 | ); |