|
1 | 1 | 'use strict'; |
2 | 2 |
|
3 | 3 | var extend = require('../utils/extend'); |
| 4 | +var npmScripts = require('../utils/npmScripts'); |
4 | 5 | var IonicAppLib = require('ionic-app-lib'); |
5 | 6 | var appLibUtils = IonicAppLib.utils; |
6 | 7 | var events = IonicAppLib.events; |
@@ -60,65 +61,68 @@ var settings = { |
60 | 61 | }; |
61 | 62 |
|
62 | 63 | function run(ionic, argv) { |
| 64 | + |
| 65 | + /** |
| 66 | + * Before running the internal server check to see if npmscripts has |
| 67 | + * a ionic specific serve command. If so then run it instead. |
| 68 | + */ |
| 69 | + return npmScripts.hasIonicScript('serve') |
| 70 | + .then(function(hasServeCommand) { |
| 71 | + if (hasServeCommand) { |
| 72 | + return npmScripts.runIonicScript('serve', argv); |
| 73 | + } else { |
| 74 | + return runServer(argv); |
| 75 | + } |
| 76 | + }) |
| 77 | + .catch(function(error) { |
| 78 | + log.error('There was an error serving your Ionic application:', error); |
| 79 | + }); |
| 80 | +} |
| 81 | + |
| 82 | +function runServer(argv) { |
63 | 83 | var project = null; |
64 | 84 | var cwd = process.cwd(); |
| 85 | + var options = {}; |
| 86 | + var promise = Q(); |
65 | 87 |
|
66 | | - try { |
67 | | - project = IonicProject.load(cwd); |
68 | | - } catch (ex) { |
69 | | - log.error('Error occured', ex); |
70 | | - return appLibUtils.fail(ex.message); |
71 | | - } |
72 | | - |
73 | | - var options = Serve.loadSettings(argv, project); |
| 88 | + project = IonicProject.load(cwd); |
74 | 89 |
|
| 90 | + options = Serve.loadSettings(argv, project); |
75 | 91 | options.appDirectory = cwd; // called from cli - ionic serve - cwd is this |
76 | | - |
77 | 92 | options.nogulp = argv.nogulp; |
78 | 93 |
|
79 | 94 | // check if CONNECT_LIVE_RELOAD_PORT is defined and use that instead |
80 | 95 | if (process.env.CONNECT_LIVE_RELOAD_PORT) { |
81 | 96 | options.liveReloadPort = process.env.CONNECT_LIVE_RELOAD_PORT; |
82 | 97 | } |
83 | | - var promise; |
84 | | - |
85 | | - try { |
86 | | - if (argv.all || argv.a) { |
87 | | - options.address = '0.0.0.0'; |
88 | | - promise = Q(); |
89 | | - } else if (argv.address) { |
90 | | - options.address = argv.address; |
91 | | - promise = Q(); |
92 | | - } else { |
93 | | - promise = Serve.getAddress(options); |
94 | | - } |
95 | 98 |
|
96 | | - return promise |
97 | | - .then(function() { |
98 | | - return Serve.checkPorts(true, options.port, options.address, options); |
99 | | - }) |
100 | | - .then(function() { |
101 | | - if (options.runLivereload) { |
102 | | - return Serve.checkPorts(false, options.liveReloadPort, options.address, options); |
103 | | - } |
104 | | - }) |
105 | | - .then(function() { |
106 | | - return Serve.start(options); |
107 | | - }) |
108 | | - .then(function() { |
109 | | - return Serve.showFinishedServeMessage(options); |
110 | | - }) |
111 | | - .then(function() { |
112 | | - events.on('serverlog', log.info); |
113 | | - events.on('consolelog', log.info); |
114 | | - }) |
115 | | - .catch(function(error) { |
116 | | - log.info('There was an error serving your Ionic application:', error); |
117 | | - throw error; |
118 | | - }); |
119 | | - } catch (ex) { |
120 | | - appLibUtils.fail('Error with serve- ' + ex); |
| 99 | + if (argv.all || argv.a) { |
| 100 | + options.address = '0.0.0.0'; |
| 101 | + } else if (argv.address) { |
| 102 | + options.address = argv.address; |
| 103 | + } else { |
| 104 | + promise = Serve.getAddress(options); |
121 | 105 | } |
| 106 | + |
| 107 | + return promise |
| 108 | + .then(function() { |
| 109 | + return Serve.checkPorts(true, options.port, options.address, options); |
| 110 | + }) |
| 111 | + .then(function() { |
| 112 | + if (options.runLivereload) { |
| 113 | + return Serve.checkPorts(false, options.liveReloadPort, options.address, options); |
| 114 | + } |
| 115 | + }) |
| 116 | + .then(function() { |
| 117 | + return Serve.start(options); |
| 118 | + }) |
| 119 | + .then(function() { |
| 120 | + return Serve.showFinishedServeMessage(options); |
| 121 | + }) |
| 122 | + .then(function() { |
| 123 | + events.on('serverlog', log.info); |
| 124 | + events.on('consolelog', log.info); |
| 125 | + }); |
122 | 126 | } |
123 | 127 |
|
124 | 128 | module.exports = extend(settings, { |
|
0 commit comments