UNPKG

6.25 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3var config_1 = require("./util/config");
4var helpers_1 = require("./util/helpers");
5var logger_1 = require("./logger/logger");
6var watch_1 = require("./watch");
7var open_1 = require("./util/open");
8var notification_server_1 = require("./dev-server/notification-server");
9var http_server_1 = require("./dev-server/http-server");
10var live_reload_1 = require("./dev-server/live-reload");
11var serve_config_1 = require("./dev-server/serve-config");
12var network_1 = require("./util/network");
13var DEV_LOGGER_DEFAULT_PORT = 53703;
14var LIVE_RELOAD_DEFAULT_PORT = 35729;
15var DEV_SERVER_DEFAULT_PORT = 8100;
16var DEV_SERVER_DEFAULT_HOST = '0.0.0.0';
17function serve(context) {
18 helpers_1.setContext(context);
19 var config;
20 var httpServer;
21 var host = getHttpServerHost(context);
22 var notificationPort = getNotificationPort(context);
23 var liveReloadServerPort = getLiveReloadServerPort(context);
24 var hostPort = getHttpServerPort(context);
25 function finish() {
26 if (config) {
27 if (httpServer) {
28 httpServer.listen(config.httpPort, config.host, function () {
29 logger_1.Logger.debug("listening on " + config.httpPort);
30 });
31 }
32 onReady(config, context);
33 }
34 }
35 return network_1.findClosestOpenPorts(host, [notificationPort, liveReloadServerPort, hostPort])
36 .then(function (_a) {
37 var notificationPortFound = _a[0], liveReloadServerPortFound = _a[1], hostPortFound = _a[2];
38 var hostLocation = (host === '0.0.0.0') ? 'localhost' : host;
39 config = {
40 httpPort: hostPortFound,
41 host: host,
42 hostBaseUrl: "http://" + hostLocation + ":" + hostPortFound,
43 rootDir: context.rootDir,
44 wwwDir: context.wwwDir,
45 buildDir: context.buildDir,
46 isCordovaServe: isCordovaServe(context),
47 launchBrowser: launchBrowser(context),
48 launchLab: launchLab(context),
49 browserToLaunch: browserToLaunch(context),
50 useLiveReload: useLiveReload(context),
51 liveReloadPort: liveReloadServerPortFound,
52 notificationPort: notificationPortFound,
53 useServerLogs: useServerLogs(context),
54 useProxy: useProxy(context),
55 notifyOnConsoleLog: sendClientConsoleLogs(context),
56 devapp: false
57 };
58 notification_server_1.createNotificationServer(config);
59 live_reload_1.createLiveReloadServer(config);
60 httpServer = http_server_1.createHttpServer(config);
61 return watch_1.watch(context);
62 })
63 .then(function () {
64 finish();
65 return config;
66 }, function (err) {
67 throw err;
68 })
69 .catch(function (err) {
70 if (err && err.isFatal) {
71 throw err;
72 }
73 else {
74 finish();
75 return config;
76 }
77 });
78}
79exports.serve = serve;
80function onReady(config, context) {
81 if (config.launchBrowser) {
82 var openOptions = [config.hostBaseUrl]
83 .concat(launchLab(context) ? [serve_config_1.IONIC_LAB_URL] : [])
84 .concat(browserOption(context) ? [browserOption(context)] : [])
85 .concat(platformOption(context) ? ['?ionicplatform=', platformOption(context)] : []);
86 open_1.default(openOptions.join(''), browserToLaunch(context), function (error) {
87 if (error) {
88 var errorMessage = error && error.message ? error.message : error.toString();
89 logger_1.Logger.warn("Failed to open the browser: " + errorMessage);
90 }
91 });
92 }
93 logger_1.Logger.info("dev server running: " + config.hostBaseUrl + "/", 'green', true);
94 logger_1.Logger.newLine();
95}
96function getHttpServerPort(context) {
97 var port = config_1.getConfigValue(context, '--port', '-p', 'IONIC_PORT', 'ionic_port', null);
98 if (port) {
99 return parseInt(port, 10);
100 }
101 return DEV_SERVER_DEFAULT_PORT;
102}
103function getHttpServerHost(context) {
104 var host = config_1.getConfigValue(context, '--address', '-h', 'IONIC_ADDRESS', 'ionic_address', null);
105 if (host) {
106 return host;
107 }
108 return DEV_SERVER_DEFAULT_HOST;
109}
110function getLiveReloadServerPort(context) {
111 var port = config_1.getConfigValue(context, '--livereload-port', null, 'IONIC_LIVERELOAD_PORT', 'ionic_livereload_port', null);
112 if (port) {
113 return parseInt(port, 10);
114 }
115 return LIVE_RELOAD_DEFAULT_PORT;
116}
117function getNotificationPort(context) {
118 var port = config_1.getConfigValue(context, '--dev-logger-port', null, 'IONIC_DEV_LOGGER_PORT', 'ionic_dev_logger_port', null);
119 if (port) {
120 return parseInt(port, 10);
121 }
122 return DEV_LOGGER_DEFAULT_PORT;
123}
124exports.getNotificationPort = getNotificationPort;
125function useServerLogs(context) {
126 return config_1.hasConfigValue(context, '--serverlogs', '-s', 'ionic_serverlogs', false);
127}
128function isCordovaServe(context) {
129 return config_1.hasConfigValue(context, '--iscordovaserve', '-z', 'ionic_cordova_serve', false);
130}
131function launchBrowser(context) {
132 return !config_1.hasConfigValue(context, '--nobrowser', '-b', 'ionic_launch_browser', false);
133}
134function browserToLaunch(context) {
135 return config_1.getConfigValue(context, '--browser', '-w', 'IONIC_BROWSER', 'ionic_browser', null);
136}
137function browserOption(context) {
138 return config_1.getConfigValue(context, '--browseroption', '-o', 'IONIC_BROWSEROPTION', 'ionic_browseroption', null);
139}
140function launchLab(context) {
141 return config_1.hasConfigValue(context, '--lab', '-l', 'ionic_lab', false);
142}
143function platformOption(context) {
144 return config_1.getConfigValue(context, '--platform', '-t', 'IONIC_PLATFORM_BROWSER', 'ionic_platform_browser', null);
145}
146function useLiveReload(context) {
147 return !config_1.hasConfigValue(context, '--nolivereload', '-d', 'ionic_livereload', false);
148}
149function useProxy(context) {
150 return !config_1.hasConfigValue(context, '--noproxy', '-x', 'ionic_proxy', false);
151}
152function sendClientConsoleLogs(context) {
153 return config_1.hasConfigValue(context, '--consolelogs', '-c', 'ionic_consolelogs', false);
154}