Skip to content

Instantly share code, notes, and snippets.

@blahah
Created June 2, 2025 16:16
Show Gist options
  • Save blahah/ccdf064c6cc3a1a4c38d0c4e8010b8ce to your computer and use it in GitHub Desktop.
Save blahah/ccdf064c6cc3a1a4c38d0c4e8010b8ce to your computer and use it in GitHub Desktop.
Pear app developer console -> STDOUT streaming
/**
* Console stdout streaming for Pear applications
* Duplicates console output to both devtools and terminal stdout/stderr
*/
// Store original console methods
const originalConsole = {
log: console.log,
error: console.error,
warn: console.warn,
info: console.info,
debug: console.debug
};
let streamingEnabled = false;
/**
* Enable console output streaming to stdout
* This overrides console methods to duplicate output to terminal
*/
export function enableConsoleStdout() {
if (streamingEnabled) return;
['log', 'error', 'warn', 'info', 'debug'].forEach(method => {
console[method] = function(...args) {
// Call original console method for devtools
originalConsole[method].apply(console, args);
// Also send to stdout/stderr for terminal visibility
const time = new Date().toLocaleTimeString('en-US', {
hour12: false,
hour: '2-digit',
minute: '2-digit',
second: '2-digit',
fractionalSecondDigits: 3
});
const colors = {
log: '\x1b[37m', // White
info: '\x1b[36m', // Cyan
warn: '\x1b[33m', // Yellow
error: '\x1b[31m', // Red
debug: '\x1b[35m', // Magenta
reset: '\x1b[0m' // Reset
};
const color = colors[method] || colors.log;
const level = method.toUpperCase().padEnd(5);
const message = args.map(arg =>
typeof arg === 'object' ? JSON.stringify(arg) : String(arg)
).join(' ');
const output = `${color}[${time}] [${level}] ${message}${colors.reset}`;
if (method === 'error') {
process.stderr.write(output + '\n');
} else {
process.stdout.write(output + '\n');
}
};
});
streamingEnabled = true;
console.log('Console stdout streaming enabled');
}
/**
* Disable console output streaming
* Restores original console methods
*/
export function disableConsoleStdout() {
if (!streamingEnabled) return;
['log', 'error', 'warn', 'info', 'debug'].forEach(method => {
console[method] = originalConsole[method];
});
streamingEnabled = false;
console.log('Console stdout streaming disabled');
}
/**
* Check if console streaming is enabled
*/
export function isStreamingEnabled() {
return streamingEnabled;
}
/**
* Auto-enable console streaming for Pear development
* Call this early in your app initialization
*/
export function autoEnableForPear() {
// Enable immediately for all environments
// This ensures we see console output in terminal during development
enableConsoleStdout();
}
// Default export for convenience
export default {
enable: enableConsoleStdout,
disable: disableConsoleStdout,
isEnabled: isStreamingEnabled,
autoEnable: autoEnableForPear
};
/** @typedef {import('pear-interface')} */ /* global Pear */
// Before you do anything else
// Enable console output streaming to terminal
import { autoEnableForPear } from './src/debug/consoleStdout.js';
autoEnableForPear();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment