UNPKG

2.3 kBJavaScriptView Raw
1"use strict";
2
3var gulp = require('gulp');
4var browserSync = require('browser-sync').create();
5var common = require('./common');
6var _ = require('lodash');
7var glob = require('glob');
8var utils = require('../utils');
9var fs = require('fs');
10var argv = require('optimist').argv;
11
12function loadAppConfig(pkg, dir, serveStaticMap, watchedFiles) {
13 if (pkg.routes) {
14 _.each(pkg.routes, route => {
15 route = '/web/' + route;
16 serveStaticMap[route] = serveStaticMap[route] || [];
17
18 //serve css directly from app' build folder - this makes CSS injection work and persist on browser reload
19 let path = dir + '/build';
20 serveStaticMap[route].push(path);
21 console.log(dir + ' adds route:', route, path);
22 });
23 }
24
25 if (pkg.watchedFiles) {
26 _.flatten([pkg.watchedFiles]).forEach(path => {
27 path = dir + '/' + path;
28 console.log(dir + ' adds watched path:', path);
29 watchedFiles.push(path);
30 });
31 }
32}
33
34function loadApps(rootDir, config) {
35 let serveStaticMap = {};
36 let watchedFiles = [];
37
38 glob.sync(rootDir + '/*/').forEach(filePath => {
39 filePath = filePath.substring(0, filePath.length - 1);
40 let jsonPath = filePath + '/portal-browser-sync.json';
41 if (utils.exists(jsonPath)) {
42 let pkg;
43 try {
44 pkg = JSON.parse(fs.readFileSync(jsonPath, 'utf8'));
45 } catch (e) {
46 console.error('Failed parsing config file' + jsonPath + ':', e);
47 }
48 loadAppConfig(pkg, filePath, serveStaticMap, watchedFiles);
49 }
50 });
51
52 let serveStatic = [];
53
54 _.forEach(serveStaticMap, (value, key) => {
55 serveStatic.push({route: key, dir: value});
56 });
57
58 config.files = watchedFiles;
59 config.serveStatic = serveStatic;
60}
61
62gulp.task('browsersync', () => {
63 let toProxy = (argv['proxied-host'] || 'localhost') + ':' + (argv['proxied-port'] || 8080);
64
65 let config = {
66 proxy: toProxy,
67 startPath: "/web/portal",
68 open: 'external',
69 ghostMode: false,
70 };
71
72 if (argv.tunnel) config.tunnel = argv.tunnel;
73 if (common.port) config.port = common.port;
74
75 loadApps('..', config);
76
77 browserSync.init(config);
78});