-
Notifications
You must be signed in to change notification settings - Fork 32
feat(serve): support --consolelogs option #100
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
f442afe
feat(serve): support --consolelogs option
tlancina 8bd6155
chore(serve): PR changes
tlancina c600bb9
fix(serve): remove chalk dependency
tlancina e2343b2
remove unnecessary stream file write
tlancina 0ed1dac
Merge branch 'master' into consolelogs
imhoffd File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.