Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 1 | import gulp from 'gulp'; |
| 2 | import browserSync from 'browser-sync'; |
| 3 | import fs from 'fs'; |
| 4 | import webserver from 'gulp-webserver'; |
| 5 | import proxy from 'http-proxy-middleware'; |
| 6 | |
| 7 | let external_apps; |
| 8 | let bs = null; |
| 9 | |
| 10 | const files = ['../../web/gui/src/main/webapp/**/*.js']; |
| 11 | const defaultViews = fs.readdirSync('../../web/gui/src/main/webapp/app/view/'); |
| 12 | const viewNameMatcher = new RegExp(/\/onos\/ui\/app\/view\/(.+)\/.+\.(?:js|css|html)/); |
| 13 | |
| 14 | if (process.env.ONOS_EXTERNAL_APP_DIRS) { |
| 15 | let external_apps = process.env.ONOS_EXTERNAL_APP_DIRS.replace(/\s/,'').split(','); |
| 16 | |
| 17 | external_apps = external_apps.reduce(function (dict, app) { |
| 18 | const pieces = app.split(':'); |
| 19 | const appName = pieces[0]; |
| 20 | const appPath = pieces[1]; |
| 21 | dict[appName] = appPath; |
| 22 | return dict; |
| 23 | }, {}); |
| 24 | } |
| 25 | |
| 26 | const checkExternalApp = (url) => { |
| 27 | if(external_apps){ |
| 28 | for(let i = 0; i < Object.keys(external_apps).length; i++){ |
| 29 | const key = Object.keys(external_apps)[i]; |
| 30 | if (url.indexOf(key) !== -1) { |
| 31 | return key; |
| 32 | } |
| 33 | } |
| 34 | } |
| 35 | return false; |
| 36 | }; |
| 37 | |
| 38 | const serve = () => { |
| 39 | bs = browserSync.init({ |
| 40 | proxy: { |
| 41 | target: 'http://localhost:8181', |
| 42 | ws: true, |
| 43 | middleware: [ |
| 44 | proxy(['**/*.js', '!/onos/ui/onos.js'], { target: 'http://localhost:8189' }), |
Steven Burrows | 1270c18 | 2017-09-14 10:14:58 +0100 | [diff] [blame] | 45 | proxy(['**/*.css'], { target: 'http://localhost:8189' }), |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 46 | proxy('**/*.js.map', { |
| 47 | target: 'http://localhost:8189', |
| 48 | changeOrigin: true, |
| 49 | logLevel: 'debug' |
| 50 | }) |
| 51 | ] |
| 52 | } |
| 53 | }); |
| 54 | }; |
| 55 | |
| 56 | const tasks = () => { |
| 57 | gulp.task('serve', ['bundle-js', 'proxy-server'], serve); |
| 58 | gulp.task('proxy-server', function () { |
| 59 | gulp.src('../../web/gui/src/main/webapp') |
| 60 | .pipe(webserver({ |
| 61 | port: 8189, |
| 62 | path: '/onos/ui/' |
| 63 | })); |
| 64 | }); |
| 65 | }; |
| 66 | |
| 67 | export default tasks(); |
| 68 | |
| 69 | export function reload() { |
| 70 | if (bs) { |
| 71 | bs.reload(); |
| 72 | } |
| 73 | } |
| 74 | |