forked from ionic-team/ionic-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserve.js
More file actions
173 lines (155 loc) · 5.27 KB
/
serve.js
File metadata and controls
173 lines (155 loc) · 5.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
'use strict';
var extend = require('../utils/extend');
var npmScripts = require('../utils/npmScripts');
var serveUtils = require('../utils/serve');
var IonicAppLib = require('ionic-app-lib');
var events = IonicAppLib.events;
var Q = require('q');
var Serve = IonicAppLib.serve;
var log = IonicAppLib.logging.logger;
var IonicProject = IonicAppLib.project;
var settings = {
title: 'serve',
name: 'serve',
summary: 'Start a local development server for app dev/testing',
args: {
'[options]': ''
},
options: {
'--consolelogs|-c': {
title: 'Print app console logs to Ionic CLI',
boolean: true
},
'--serverlogs|-s': {
title: 'Print dev server logs to Ionic CLI',
boolean: true
},
'--port|-p': 'Dev server HTTP port (8100 default)',
'--livereload-port|-r': 'Live Reload port (35729 default)',
'--nobrowser|-b': {
title: 'Disable launching a browser',
boolean: true
},
'--nolivereload|-d': {
title: 'Do not start live reload',
boolean: true
},
'--noproxy|-x': {
title: 'Do not add proxies',
boolean: true
},
'--address': 'Use specific address or return with failure',
'--all|-a': {
title: 'Have the server listen on all addresses (0.0.0.0)',
boolean: true
},
'--browser|-w': 'Specifies the browser to use (safari, firefox, chrome)',
'--browseroption|-o': 'Specifies a path to open to (/#/tab/dash)',
'--lab|-l': {
title: 'Test your apps on multiple screen sizes and platform types',
boolean: true
},
'--nogulp': {
title: 'Disable running gulp during serve',
boolean: true
},
'--platform|-t': 'Start serve with a specific platform (ios/android)'
},
isProjectTask: true
};
function run(ionic, argv, rawCliArguments) {
// Support `ionic lab` as a shortcut for `ionic serve --lab`
var runsLab = rawCliArguments[0] === 'lab';
if (runsLab) {
argv.lab = true;
}
/**
* Before running the internal server check to see if npmscripts has
* a ionic specific serve command. If so then run it instead.
*/
return npmScripts.hasIonicScript('serve')
.then(function(hasServeCommand) {
if (hasServeCommand) {
return runAppScriptsServer(argv);
} else {
return runServer(argv);
}
})
.catch(function(error) {
log.error('There was an error serving your Ionic application:', error);
});
}
function runAppScriptsServer(argv) {
var options = extend(argv, {});
options = npmScripts.consolidateOptions(['consolelogs', 'c'], options);
options = npmScripts.consolidateOptions(['serverlogs', 's'], options);
options = npmScripts.consolidateOptions(['port', 'p'], options);
options = npmScripts.consolidateOptions(['livereload-port', 'r'], options);
options = npmScripts.consolidateOptions(['nobrowser', 'b'], options);
options = npmScripts.consolidateOptions(['nolivereload', 'd'], options);
options = npmScripts.consolidateOptions(['noproxy', 'x'], options);
options = npmScripts.consolidateOptions(['browser', 'w'], options);
options = npmScripts.consolidateOptions(['browseroption', 'o'], options);
options = npmScripts.consolidateOptions(['lab', 'l'], options);
options = npmScripts.consolidateOptions(['platform', 't'], options);
options.address = options.address || serveUtils.DEFAULT_ADDRESS;
options.port = options.port || serveUtils.DEFAULT_HTTP_PORT;
options['livereload-port'] = options['livereload-port'] || serveUtils.DEFAULT_LIVE_RELOAD_PORT;
// Check that the server port is available
return serveUtils.findClosestOpenPort(options.address, options.port).then(function(portFound) {
options.port = portFound;
// Check that the liveReload port is available
return serveUtils.findClosestOpenPort(options.address, options['livereload-port']);
})
.then(function(portFound) {
options['livereload-port'] = portFound;
// Execute the serve command from app-scripts
// Also remove platform from the raw args passed
return npmScripts.runIonicScript('serve',
npmScripts.optionsToArray(options)
);
});
}
function runServer(argv) {
var project = null;
var cwd = process.cwd();
var options = {};
var promise = Q();
project = IonicProject.load(cwd);
options = Serve.loadSettings(argv, project);
options.appDirectory = cwd; // called from cli - ionic serve - cwd is this
options.nogulp = argv.nogulp;
// check if CONNECT_LIVE_RELOAD_PORT is defined and use that instead
if (process.env.CONNECT_LIVE_RELOAD_PORT) {
options.liveReloadPort = process.env.CONNECT_LIVE_RELOAD_PORT;
}
if (argv.all || argv.a) {
options.address = '0.0.0.0';
} else if (argv.address) {
options.address = argv.address;
} else {
promise = Serve.getAddress(options);
}
return promise
.then(function() {
return Serve.checkPorts(true, options.port, options.address, options);
})
.then(function() {
if (options.runLivereload) {
return Serve.checkPorts(false, options.liveReloadPort, options.address, options);
}
})
.then(function() {
return Serve.start(options);
})
.then(function() {
return Serve.showFinishedServeMessage(options);
})
.then(function() {
events.on('serverlog', log.info);
events.on('consolelog', log.info);
});
}
module.exports = extend(settings, {
run: run
});