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' }), |
| 45 | proxy('**/*.js.map', { |
| 46 | target: 'http://localhost:8189', |
| 47 | changeOrigin: true, |
| 48 | logLevel: 'debug' |
| 49 | }) |
| 50 | ] |
| 51 | } |
| 52 | }); |
| 53 | }; |
| 54 | |
| 55 | const tasks = () => { |
| 56 | gulp.task('serve', ['bundle-js', 'proxy-server'], serve); |
| 57 | gulp.task('proxy-server', function () { |
| 58 | gulp.src('../../web/gui/src/main/webapp') |
| 59 | .pipe(webserver({ |
| 60 | port: 8189, |
| 61 | path: '/onos/ui/' |
| 62 | })); |
| 63 | }); |
| 64 | }; |
| 65 | |
| 66 | export default tasks(); |
| 67 | |
| 68 | export function reload() { |
| 69 | if (bs) { |
| 70 | bs.reload(); |
| 71 | } |
| 72 | } |
| 73 | |