1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | var config_1 = require("./util/config");
|
4 | var helpers_1 = require("./util/helpers");
|
5 | var logger_1 = require("./logger/logger");
|
6 | var watch_1 = require("./watch");
|
7 | var open_1 = require("./util/open");
|
8 | var notification_server_1 = require("./dev-server/notification-server");
|
9 | var http_server_1 = require("./dev-server/http-server");
|
10 | var live_reload_1 = require("./dev-server/live-reload");
|
11 | var serve_config_1 = require("./dev-server/serve-config");
|
12 | var network_1 = require("./util/network");
|
13 | var DEV_LOGGER_DEFAULT_PORT = 53703;
|
14 | var LIVE_RELOAD_DEFAULT_PORT = 35729;
|
15 | var DEV_SERVER_DEFAULT_PORT = 8100;
|
16 | var DEV_SERVER_DEFAULT_HOST = '0.0.0.0';
|
17 | function 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 | }
|
79 | exports.serve = serve;
|
80 | function 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 | }
|
96 | function 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 | }
|
103 | function 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 | }
|
110 | function 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 | }
|
117 | function 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 | }
|
124 | exports.getNotificationPort = getNotificationPort;
|
125 | function useServerLogs(context) {
|
126 | return config_1.hasConfigValue(context, '--serverlogs', '-s', 'ionic_serverlogs', false);
|
127 | }
|
128 | function isCordovaServe(context) {
|
129 | return config_1.hasConfigValue(context, '--iscordovaserve', '-z', 'ionic_cordova_serve', false);
|
130 | }
|
131 | function launchBrowser(context) {
|
132 | return !config_1.hasConfigValue(context, '--nobrowser', '-b', 'ionic_launch_browser', false);
|
133 | }
|
134 | function browserToLaunch(context) {
|
135 | return config_1.getConfigValue(context, '--browser', '-w', 'IONIC_BROWSER', 'ionic_browser', null);
|
136 | }
|
137 | function browserOption(context) {
|
138 | return config_1.getConfigValue(context, '--browseroption', '-o', 'IONIC_BROWSEROPTION', 'ionic_browseroption', null);
|
139 | }
|
140 | function launchLab(context) {
|
141 | return config_1.hasConfigValue(context, '--lab', '-l', 'ionic_lab', false);
|
142 | }
|
143 | function platformOption(context) {
|
144 | return config_1.getConfigValue(context, '--platform', '-t', 'IONIC_PLATFORM_BROWSER', 'ionic_platform_browser', null);
|
145 | }
|
146 | function useLiveReload(context) {
|
147 | return !config_1.hasConfigValue(context, '--nolivereload', '-d', 'ionic_livereload', false);
|
148 | }
|
149 | function useProxy(context) {
|
150 | return !config_1.hasConfigValue(context, '--noproxy', '-x', 'ionic_proxy', false);
|
151 | }
|
152 | function sendClientConsoleLogs(context) {
|
153 | return config_1.hasConfigValue(context, '--consolelogs', '-c', 'ionic_consolelogs', false);
|
154 | }
|