11import chalk from 'chalk' ;
22
33import { IonicEnvironment , ServeDetails , ServeOptions } from '../../definitions' ;
4-
5- import { BIND_ALL_ADDRESS , LOCAL_ADDRESSES , selectExternalIP } from '../serve' ;
4+ import { BIND_ALL_ADDRESS , LOCAL_ADDRESSES , findOpenPorts , selectExternalIP } from '../serve' ;
65import { FatalException } from '../errors' ;
7- import { importAppScripts } from './app-scripts' ;
86
97export interface AppScriptsServeOptions extends ServeOptions {
108 platform : string ;
@@ -13,57 +11,35 @@ export interface AppScriptsServeOptions extends ServeOptions {
1311}
1412
1513export async function serve ( { env, options } : { env : IonicEnvironment , options : AppScriptsServeOptions } ) : Promise < ServeDetails > {
14+ const split2 = await import ( 'split2' ) ;
15+ const { registerShutdownFunction } = await import ( '../process' ) ;
16+ const { isHostConnectable } = await import ( '../utils/network' ) ;
1617 const [ externalIP , availableInterfaces ] = await selectExternalIP ( env , options ) ;
1718
18- const appScriptsArgs = await serveOptionsToAppScriptsArgs ( options ) ;
19- process . argv = [ 'node' , 'appscripts' ] . concat ( appScriptsArgs ) ;
19+ const { port } = await findOpenPorts ( env , options . address , options ) ;
20+
21+ const p = await env . shell . spawn ( 'ng' , [ 'serve' , '--host' , options . address , '--port' , String ( port ) , '--progress' , 'false' ] , { cwd : env . project . directory } ) ;
22+
23+ const log = env . log . clone ( { prefix : chalk . dim ( '[ng]' ) , wrap : false } ) ;
24+ const ws = log . createWriteStream ( ) ;
2025
21- const AppScripts = await importAppScripts ( env ) ;
22- const context = AppScripts . generateContext ( ) ;
26+ p . stdout . pipe ( split2 ( ) ) . pipe ( ws ) ;
27+ p . stderr . pipe ( split2 ( ) ) . pipe ( ws ) ;
2328
24- // using app-scripts and livereload is requested
25- // Also remove commandName from the rawArgs passed
26- env . log . info ( `Starting app-scripts server: ${ chalk . bold ( appScriptsArgs . join ( ' ' ) ) } - Ctrl+C to cancel` ) ;
27- const settings = await AppScripts . serve ( context ) ;
29+ registerShutdownFunction ( ( ) => { p . kill ( ) ; } ) ;
2830
29- if ( ! settings ) { // TODO: shouldn've been fixed after app-scripts 1.3.7
30- throw new FatalException (
31- `app-scripts serve unexpectedly failed.` +
32- `settings: ${ settings } ` +
33- `context: ${ context } `
34- ) ;
31+ const connectable = await isHostConnectable ( externalIP , port , 20000 ) ;
32+
33+ if ( ! connectable ) {
34+ throw new FatalException ( `Could not connect to ng server at ${ options . address } :${ String ( port ) } .` ) ;
3535 }
3636
3737 return {
3838 protocol : 'http' ,
3939 localAddress : 'localhost' ,
4040 externalAddress : externalIP ,
4141 externalNetworkInterfaces : availableInterfaces ,
42- port : settings . httpPort ,
42+ port,
4343 externallyAccessible : ! [ BIND_ALL_ADDRESS , ...LOCAL_ADDRESSES ] . includes ( externalIP ) ,
4444 } ;
4545}
46-
47- export async function serveOptionsToAppScriptsArgs ( options : AppScriptsServeOptions ) {
48- const { minimistOptionsToArray } = await import ( '../utils/command' ) ;
49-
50- const minimistArgs = {
51- _ : [ ] ,
52- address : options . address ,
53- port : String ( options . port ) ,
54- livereloadPort : String ( options . livereloadPort ) ,
55- devLoggerPort : String ( options . notificationPort ) ,
56- consolelogs : options . consolelogs ,
57- serverlogs : options . serverlogs ,
58- nobrowser : true ,
59- nolivereload : ! options . livereload ,
60- noproxy : ! options . proxy ,
61- lab : options . lab ,
62- iscordovaserve : options . iscordovaserve ,
63- platform : options . platform ,
64- target : options . target ,
65- env : options . env ,
66- } ;
67-
68- return minimistOptionsToArray ( minimistArgs , { useEquals : false } ) ;
69- }
0 commit comments