@@ -8,163 +8,138 @@ const simctl = require("simctl");
88const which = require ( "which" ) ;
99
1010interface IDebugPlatform {
11- getLogProcess ( ) : any ;
12- normalizeLogMessage ( message : string ) : string ;
11+ getLogProcess ( ) : any ;
12+ normalizeLogMessage ( message : string ) : string ;
1313}
1414
1515class AndroidDebugPlatform implements IDebugPlatform {
16- public getLogProcess ( ) : any {
17- try {
18- which . sync ( "adb" ) ;
19- } catch ( e ) {
20- throw new Error (
21- "ADB command not found. Please ensure it is installed and available on your path."
22- ) ;
23- }
24-
25- const numberOfAvailableDevices = this . getNumberOfAvailableDevices ( ) ;
26- if ( numberOfAvailableDevices === 0 ) {
27- throw new Error (
28- "No Android devices found. Re-run this command after starting one."
29- ) ;
30- }
31-
32- // For now there is no ability to specify device for debug like:
33- // code-push debug android "192.168.121.102:5555"
34- // So we have to throw an error in case more than 1 android device was attached
35- // otherwise we will very likely run into an exception while trying to read ‘adb logcat’ from device which codepushified app is not running on.
36- if ( numberOfAvailableDevices > 1 ) {
37- throw new Error (
38- `Found "${ numberOfAvailableDevices } " android devices. Please leave only one device you need to debug.`
39- ) ;
40- }
41-
42- return childProcess . spawn ( "adb" , [ "logcat" ] ) ;
16+ public getLogProcess ( ) : any {
17+ try {
18+ which . sync ( "adb" ) ;
19+ } catch ( e ) {
20+ throw new Error ( "ADB command not found. Please ensure it is installed and available on your path." ) ;
4321 }
4422
45- // The following is an example of what the output looks
46- // like when running the "adb devices" command.
47- //
48- // List of devices attached
49- // emulator-5554 device
50- // 192.168.121.102:5555 device
51- private getNumberOfAvailableDevices ( ) : number {
52- const output = childProcess . execSync ( "adb devices" ) . toString ( ) ;
53- const matches = output . match ( / \b ( d e v i c e ) \b / gim) ;
54- if ( matches != null ) {
55- return matches . length ;
56- }
57- return 0 ;
23+ const numberOfAvailableDevices = this . getNumberOfAvailableDevices ( ) ;
24+ if ( numberOfAvailableDevices === 0 ) {
25+ throw new Error ( "No Android devices found. Re-run this command after starting one." ) ;
5826 }
5927
60- public normalizeLogMessage ( message : string ) : string {
61- // Check to see whether the message includes the source URL
62- // suffix, and if so, strip it. This can occur in Android Cordova apps.
63- const sourceURLIndex : number = message . indexOf ( '", source: file:///' ) ;
64- if ( ~ sourceURLIndex ) {
65- return message . substring ( 0 , sourceURLIndex ) ;
66- } else {
67- return message ;
68- }
28+ // For now there is no ability to specify device for debug like:
29+ // code-push debug android "192.168.121.102:5555"
30+ // So we have to throw an error in case more than 1 android device was attached
31+ // otherwise we will very likely run into an exception while trying to read ‘adb logcat’ from device which codepushified app is not running on.
32+ if ( numberOfAvailableDevices > 1 ) {
33+ throw new Error ( `Found "${ numberOfAvailableDevices } " android devices. Please leave only one device you need to debug.` ) ;
6934 }
35+
36+ return childProcess . spawn ( "adb" , [ "logcat" ] ) ;
37+ }
38+
39+ // The following is an example of what the output looks
40+ // like when running the "adb devices" command.
41+ //
42+ // List of devices attached
43+ // emulator-5554 device
44+ // 192.168.121.102:5555 device
45+ private getNumberOfAvailableDevices ( ) : number {
46+ const output = childProcess . execSync ( "adb devices" ) . toString ( ) ;
47+ const matches = output . match ( / \b ( d e v i c e ) \b / gim) ;
48+ if ( matches != null ) {
49+ return matches . length ;
50+ }
51+ return 0 ;
52+ }
53+
54+ public normalizeLogMessage ( message : string ) : string {
55+ // Check to see whether the message includes the source URL
56+ // suffix, and if so, strip it. This can occur in Android Cordova apps.
57+ const sourceURLIndex : number = message . indexOf ( '", source: file:///' ) ;
58+ if ( ~ sourceURLIndex ) {
59+ return message . substring ( 0 , sourceURLIndex ) ;
60+ } else {
61+ return message ;
62+ }
63+ }
7064}
7165
7266class iOSDebugPlatform implements IDebugPlatform {
73- private getSimulatorID ( ) : string {
74- const output : any = simctl . list ( { devices : true , silent : true } ) ;
75- const simulators : string [ ] = output . json . devices
76- . map ( ( platform : any ) => platform . devices )
77- . reduce ( ( prev : any , next : any ) => prev . concat ( next ) )
78- . filter ( ( device : any ) => device . state === "Booted" )
79- . map ( ( device : any ) => device . id ) ;
80-
81- return simulators [ 0 ] ;
67+ private getSimulatorID ( ) : string {
68+ const output : any = simctl . list ( { devices : true , silent : true } ) ;
69+ const simulators : string [ ] = output . json . devices
70+ . map ( ( platform : any ) => platform . devices )
71+ . reduce ( ( prev : any , next : any ) => prev . concat ( next ) )
72+ . filter ( ( device : any ) => device . state === "Booted" )
73+ . map ( ( device : any ) => device . id ) ;
74+
75+ return simulators [ 0 ] ;
76+ }
77+
78+ public getLogProcess ( ) : any {
79+ if ( process . platform !== "darwin" ) {
80+ throw new Error ( "iOS debug logs can only be viewed on OS X." ) ;
8281 }
8382
84- public getLogProcess ( ) : any {
85- if ( process . platform !== "darwin" ) {
86- throw new Error ( "iOS debug logs can only be viewed on OS X." ) ;
87- }
88-
89- const simulatorID : string = this . getSimulatorID ( ) ;
90- if ( ! simulatorID ) {
91- throw new Error (
92- "No iOS simulators found. Re-run this command after starting one."
93- ) ;
94- }
95-
96- const logFilePath : string = path . join (
97- process . env . HOME ,
98- "Library/Logs/CoreSimulator" ,
99- simulatorID ,
100- "system.log"
101- ) ;
102- return childProcess . spawn ( "tail" , [ "-f" , logFilePath ] ) ;
83+ const simulatorID : string = this . getSimulatorID ( ) ;
84+ if ( ! simulatorID ) {
85+ throw new Error ( "No iOS simulators found. Re-run this command after starting one." ) ;
10386 }
10487
105- public normalizeLogMessage ( message : string ) : string {
106- return message ;
107- }
88+ const logFilePath : string = path . join ( process . env . HOME , "Library/Logs/CoreSimulator" , simulatorID , "system.log" ) ;
89+ return childProcess . spawn ( "tail" , [ "-f" , logFilePath ] ) ;
90+ }
91+
92+ public normalizeLogMessage ( message : string ) : string {
93+ return message ;
94+ }
10895}
10996
11097const logMessagePrefix = "[CodePush] " ;
11198function processLogData ( logData : Buffer ) {
112- const content = logData . toString ( ) ;
113- content
114- . split ( "\n" )
115- . filter ( ( line : string ) => line . indexOf ( logMessagePrefix ) > - 1 )
116- . map ( ( line : string ) => {
117- // Allow the current platform
118- // to normalize the message first.
119- line = this . normalizeLogMessage ( line ) ;
120-
121- // Strip the CodePush-specific, platform agnostic
122- // log message prefix that is added to each entry.
123- const message = line . substring (
124- line . indexOf ( logMessagePrefix ) + logMessagePrefix . length
125- ) ;
126-
127- const timeStamp = moment ( ) . format ( "hh:mm:ss" ) ;
128- return `[${ timeStamp } ] ${ message } ` ;
129- } )
130- . forEach ( ( line : string ) => console . log ( line ) ) ;
99+ const content = logData . toString ( ) ;
100+ content
101+ . split ( "\n" )
102+ . filter ( ( line : string ) => line . indexOf ( logMessagePrefix ) > - 1 )
103+ . map ( ( line : string ) => {
104+ // Allow the current platform
105+ // to normalize the message first.
106+ line = this . normalizeLogMessage ( line ) ;
107+
108+ // Strip the CodePush-specific, platform agnostic
109+ // log message prefix that is added to each entry.
110+ const message = line . substring ( line . indexOf ( logMessagePrefix ) + logMessagePrefix . length ) ;
111+
112+ const timeStamp = moment ( ) . format ( "hh:mm:ss" ) ;
113+ return `[${ timeStamp } ] ${ message } ` ;
114+ } )
115+ . forEach ( ( line : string ) => console . log ( line ) ) ;
131116}
132117
133118const debugPlatforms : any = {
134- android : new AndroidDebugPlatform ( ) ,
135- ios : new iOSDebugPlatform ( ) ,
119+ android : new AndroidDebugPlatform ( ) ,
120+ ios : new iOSDebugPlatform ( ) ,
136121} ;
137122
138- export default function ( command : cli . IDebugCommand ) : Q . Promise < void > {
139- return Q . Promise < void > ( ( resolve , reject ) => {
140- const platform : string = command . platform . toLowerCase ( ) ;
141- const debugPlatform : IDebugPlatform = debugPlatforms [ platform ] ;
142-
143- if ( ! debugPlatform ) {
144- const availablePlatforms = Object . getOwnPropertyNames (
145- debugPlatforms
146- ) ;
147- return reject (
148- new Error (
149- `"${ platform } " is an unsupported platform. Available options are ${ availablePlatforms . join (
150- ", "
151- ) } .`
152- )
153- ) ;
154- }
155-
156- try {
157- const logProcess = debugPlatform . getLogProcess ( ) ;
158- console . log (
159- `Listening for ${ platform } debug logs (Press CTRL+C to exit)`
160- ) ;
161-
162- logProcess . stdout . on ( "data" , processLogData . bind ( debugPlatform ) ) ;
163- logProcess . stderr . on ( "data" , reject ) ;
164-
165- logProcess . on ( "close" , resolve ) ;
166- } catch ( e ) {
167- reject ( e ) ;
168- }
169- } ) ;
123+ export default function ( command : cli . IDebugCommand ) : Promise < void > {
124+ return new Promise < void > ( ( resolve , reject ) => {
125+ const platform : string = command . platform . toLowerCase ( ) ;
126+ const debugPlatform : IDebugPlatform = debugPlatforms [ platform ] ;
127+
128+ if ( ! debugPlatform ) {
129+ const availablePlatforms = Object . getOwnPropertyNames ( debugPlatforms ) ;
130+ return reject ( new Error ( `"${ platform } " is an unsupported platform. Available options are ${ availablePlatforms . join ( ", " ) } .` ) ) ;
131+ }
132+
133+ try {
134+ const logProcess = debugPlatform . getLogProcess ( ) ;
135+ console . log ( `Listening for ${ platform } debug logs (Press CTRL+C to exit)` ) ;
136+
137+ logProcess . stdout . on ( "data" , processLogData . bind ( debugPlatform ) ) ;
138+ logProcess . stderr . on ( "data" , reject ) ;
139+
140+ logProcess . on ( "close" , resolve ) ;
141+ } catch ( e ) {
142+ reject ( e ) ;
143+ }
144+ } ) ;
170145}
0 commit comments