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'); |
| 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'); |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 8 | let gulpCssUsage = require('gulp-css-usage').default; |
Avi Ziv | b8e2faf | 2017-07-18 19:45:38 +0300 | [diff] [blame] | 9 | |
| 10 | let prodTask = require('./tools/gulp/tasks/prod'); |
| 11 | let i18nTask = require('./tools/gulp/tasks/i18n.js'); |
| 12 | |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 13 | let jsonConfig = { |
| 14 | "appContextPath" : "/onboarding" |
| 15 | }; |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 16 | |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 17 | try { |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 18 | jsonConfig = require('./src/sdc-app/config/config.json'); |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 19 | } catch (e) { |
Avi Ziv | b8e2faf | 2017-07-18 19:45:38 +0300 | [diff] [blame] | 20 | console.log('could not load config. using default value instead'); |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 21 | } |
| 22 | |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 23 | const appName = 'onboarding'; |
| 24 | const dist = 'dist'; |
| 25 | |
| 26 | const path = { |
Avi Ziv | b8e2faf | 2017-07-18 19:45:38 +0300 | [diff] [blame] | 27 | // inputs |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 28 | json: './src/**/*.json', |
| 29 | index: './src/index.html', |
| 30 | heat: './src/heat.html', |
| 31 | scss: './resources/scss/**/*.scss', |
Avi Ziv | b8e2faf | 2017-07-18 19:45:38 +0300 | [diff] [blame] | 32 | i18nBundles: './src/nfvo-utils/i18n/*.json', |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 33 | svgSrc: './resources/images/svg/*.svg', |
Avi Ziv | b8e2faf | 2017-07-18 19:45:38 +0300 | [diff] [blame] | 34 | appinf: './webapp-onboarding/**/*.*', |
| 35 | jetty: './webapp-onboarding/WEB-INF/jetty-web.xml', |
Avi Ziv | 341f43a | 2017-07-30 12:34:05 +0300 | [diff] [blame^] | 36 | healthCheckInput: './external-resources/healthcheck/v1.0/healthcheck.json', |
Avi Ziv | b8e2faf | 2017-07-18 19:45:38 +0300 | [diff] [blame] | 37 | srcDir: './src/', |
| 38 | // output |
| 39 | output: dist, |
| 40 | css: dist + '/css', |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 41 | svg: dist + '/resources/images/svg', |
Avi Ziv | b8e2faf | 2017-07-18 19:45:38 +0300 | [diff] [blame] | 42 | appinf_output: dist + '/webapp-onboarding', |
Avi Ziv | 341f43a | 2017-07-30 12:34:05 +0300 | [diff] [blame^] | 43 | healthCheckOutput: dist + '/v1.0', |
Avi Ziv | b8e2faf | 2017-07-18 19:45:38 +0300 | [diff] [blame] | 44 | // war |
Avi Ziv | 341f43a | 2017-07-30 12:34:05 +0300 | [diff] [blame^] | 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.json)'], |
Avi Ziv | b8e2faf | 2017-07-18 19:45:38 +0300 | [diff] [blame] | 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/**'], |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 47 | wardest: dist, |
Avi Ziv | b8e2faf | 2017-07-18 19:45:38 +0300 | [diff] [blame] | 48 | // storybook |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 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 | }; |
Avi Ziv | b8e2faf | 2017-07-18 19:45:38 +0300 | [diff] [blame] | 54 | // cleans up the output directory |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 55 | taskMaker.defineTask('clean', {taskName: 'clean', src: path.output}); |
Avi Ziv | b8e2faf | 2017-07-18 19:45:38 +0300 | [diff] [blame] | 56 | // copies for all relevant files to the output directory |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 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}); |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 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}); |
Avi Ziv | b8e2faf | 2017-07-18 19:45:38 +0300 | [diff] [blame] | 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']}); |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 69 | |
Avi Ziv | b8e2faf | 2017-07-18 19:45:38 +0300 | [diff] [blame] | 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 | |
Avi Ziv | 341f43a | 2017-07-30 12:34:05 +0300 | [diff] [blame^] | 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 | }); |
Avi Ziv | b8e2faf | 2017-07-18 19:45:38 +0300 | [diff] [blame] | 85 | |
| 86 | // 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] | 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 | }) |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 95 | }); |
Avi Ziv | b8e2faf | 2017-07-18 19:45:38 +0300 | [diff] [blame] | 96 | // aggregates all copy tasks |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 97 | gulp.task('copy-stuff', callback => runSequence(['copy-json', 'copy-index.html', 'copy-heat.html', 'copy-svg', 'app-context'], callback)); |
| 98 | |
Avi Ziv | b8e2faf | 2017-07-18 19:45:38 +0300 | [diff] [blame] | 99 | // minimum build for dev |
| 100 | gulp.task('dev', callback => runSequence('clean', 'copy-stuff', callback)); |
| 101 | // build procedure for war file |
Avi Ziv | 341f43a | 2017-07-30 12:34:05 +0300 | [diff] [blame^] | 102 | 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] | 103 | // default build is set to 'dev' |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 104 | gulp.task('default', ['dev']); |
Avi Ziv | b8e2faf | 2017-07-18 19:45:38 +0300 | [diff] [blame] | 105 | // creating the webpack tasks for the production build |
| 106 | gulp.task('prod', () => prodTask({outDir: path.output, i18nBundles : path.i18nBundles}) |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 107 | .catch(err => { |
| 108 | if (err && err.stack) { |
| 109 | console.error(err, err.stack); |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 110 | } |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 111 | throw new Error('Webpack build FAILED'); |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 112 | }) |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 113 | ); |
| 114 | |
Avi Ziv | b8e2faf | 2017-07-18 19:45:38 +0300 | [diff] [blame] | 115 | /*** |
| 116 | * T O O L S . N O T P A R T O F B U I L D |
| 117 | */ |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 118 | |
Avi Ziv | b8e2faf | 2017-07-18 19:45:38 +0300 | [diff] [blame] | 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 |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 121 | gulp.task('gulp-css-usage', () => { |
| 122 | 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] | 123 | }); |
| 124 | |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 125 | gulp.task('css-usage', () => { |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 126 | runSequence('sass', 'gulp-css-usage'); |
| 127 | }); |
| 128 | |
Avi Ziv | b8e2faf | 2017-07-18 19:45:38 +0300 | [diff] [blame] | 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 | ); |