UNPKG

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