-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cordova-serve): support --consolelogs option (#100)
- Loading branch information
Showing
12 changed files
with
209 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,3 +5,4 @@ node_modules | |
!builders/**/schema.d.ts | ||
!schematics/**/schema.d.ts | ||
!schematics/*/files/**/* | ||
.vscode |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
// Script injected by @ionic/angular-toolkit to send console logs back | ||
// to a websocket server so they can be printed to the terminal | ||
window.Ionic = window.Ionic || {}; window.Ionic.ConsoleLogServer = { | ||
start: function(config) { | ||
var self = this; | ||
|
||
this.socket = new WebSocket('ws://' + window.location.hostname + ':' + String(config.wsPort)); | ||
this.msgQueue = []; | ||
|
||
this.socket.onopen = function() { | ||
self.socketReady = true; | ||
|
||
self.socket.onclose = function() { | ||
self.socketReady = false; | ||
console.warn('Console log server closed'); | ||
}; | ||
}; | ||
|
||
this.patchConsole(); | ||
}, | ||
|
||
queueMessageSend: function(msg) { | ||
this.msgQueue.push(msg); | ||
this.drainMessageQueue(); | ||
}, | ||
|
||
drainMessageQueue: function() { | ||
var msg; | ||
while (msg = this.msgQueue.shift()) { | ||
if (this.socketReady) { | ||
try { | ||
this.socket.send(JSON.stringify(msg)); | ||
} catch(e) { | ||
if (!(e instanceof TypeError)) { | ||
console.error('ws error: ' + e); | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
|
||
patchConsole: function() { | ||
var self = this; | ||
|
||
function _patchConsole(consoleType) { | ||
console[consoleType] = (function() { | ||
var orgConsole = console[consoleType]; | ||
return function() { | ||
orgConsole.apply(console, arguments); | ||
var msg = { | ||
category: 'console', | ||
type: consoleType, | ||
data: [] | ||
}; | ||
for (var i = 0; i < arguments.length; i++) { | ||
msg.data.push(arguments[i]); | ||
} | ||
if (msg.data.length) { | ||
self.queueMessageSend(msg); | ||
} | ||
}; | ||
})(); | ||
} | ||
|
||
// https://cordova.apache.org/docs/en/latest/reference/cordova-plugin-console/#supported-methods | ||
var consoleFns = ['log', 'error', 'exception', 'warn', 'info', 'debug', 'assert', 'dir', 'dirxml', 'time', 'timeEnd', 'table']; | ||
for (var i in consoleFns) { | ||
_patchConsole(consoleFns[i]); | ||
} | ||
}, | ||
}; | ||
|
||
Ionic.ConsoleLogServer.start(Ionic.ConsoleLogServerConfig || {}); |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import { terminal } from '@angular-devkit/core'; | ||
import * as util from 'util'; | ||
import * as WebSocket from 'ws'; | ||
|
||
export interface ConsoleLogServerMessage { | ||
category: 'console'; | ||
type: string; | ||
data: any[]; | ||
} | ||
|
||
export interface ConsoleLogServerOptions { | ||
consolelogs: boolean; | ||
consolelogsPort: number; | ||
} | ||
|
||
export function isConsoleLogServerMessage(m: any): m is ConsoleLogServerMessage { | ||
return m | ||
&& typeof m.category === 'string' | ||
&& typeof m.type === 'string' | ||
&& m.data && typeof m.data.length === 'number'; | ||
} | ||
|
||
export async function createConsoleLogServer(host: string, port: number): Promise<WebSocket.Server> { | ||
const wss = new WebSocket.Server({ host, port }); | ||
|
||
wss.on('connection', ws => { | ||
ws.on('message', data => { | ||
let msg; | ||
|
||
try { | ||
data = data.toString(); | ||
msg = JSON.parse(data); | ||
} catch (e) { | ||
process.stderr.write(`Error parsing JSON message from client: "${data}" ${terminal.red(e.stack ? e.stack : e)}\n`); | ||
return; | ||
} | ||
|
||
if (!isConsoleLogServerMessage(msg)) { | ||
const m = util.inspect(msg, { colors: true }); | ||
process.stderr.write(`Bad format in client message: ${m}\n`); | ||
return; | ||
} | ||
|
||
if (msg.category === 'console') { | ||
let status: ((_: string) => string) | undefined; | ||
|
||
if (msg.type === 'info' || msg.type === 'log') { | ||
status = terminal.reset; | ||
} else if (msg.type === 'error') { | ||
status = terminal.red; | ||
} else if (msg.type === 'warn') { | ||
status = terminal.yellow; | ||
} | ||
|
||
// pretty print objects and arrays (no newlines for arrays) | ||
msg.data = msg.data.map(d => JSON.stringify(d, undefined, d && d.length ? '' : ' ')); | ||
|
||
if (status) { | ||
process.stdout.write(`[${status('console.' + msg.type)}]: ${msg.data.join(' ')}\n`); | ||
} else { | ||
process.stdout.write(`[console]: ${msg.data.join(' ')}\n`); | ||
} | ||
} | ||
}); | ||
|
||
ws.on('error', (err: NodeJS.ErrnoException) => { | ||
if (err && err.code !== 'ECONNRESET') { | ||
process.stderr.write(`There was an error with the logging stream: ${JSON.stringify(err)}\n`); | ||
} | ||
}); | ||
}); | ||
|
||
wss.on('error', (err: NodeJS.ErrnoException) => { | ||
process.stderr.write(`There was an error with the logging websocket: ${JSON.stringify(err)}\n`); | ||
}); | ||
|
||
return wss; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters