#!/usr/bin/env node
const fs = require('fs');
const path = require('path');
// Files to update
const files = [
'src/server.ts',
'src/transports/https-transport.ts',
'src/auth/oauth-server.ts',
'src/auth/dual-auth.ts',
'src/user-session-manager.ts'
];
// Function to update a single file
function updateFile(filePath) {
const fullPath = path.join(process.cwd(), filePath);
if (!fs.existsSync(fullPath)) {
console.log(`Skipping ${filePath} - file not found`);
return;
}
let content = fs.readFileSync(fullPath, 'utf8');
const originalContent = content;
// Check if debugLog is already imported
const hasDebugLogImport = content.includes("import { debugLog }") || content.includes("from './utils/debug-logger");
if (!hasDebugLogImport && !content.includes("from '../utils/debug-logger")) {
// Add import at the top after other imports
const importRegex = /^(import .* from .*;?\n)+/m;
const match = content.match(importRegex);
if (match) {
const lastImportEnd = match.index + match[0].length;
const relativeImport = filePath.startsWith('src/auth/') ? '../utils/debug-logger.js' :
filePath.startsWith('src/transports/') ? '../utils/debug-logger.js' :
'./utils/debug-logger.js';
content = content.slice(0, lastImportEnd) +
`import { debugLog } from '${relativeImport}';\n` +
content.slice(lastImportEnd);
}
}
// Replace console.error with debug logging patterns
const replacements = [
// API call logging
{
pattern: /console\.error\(`\[API-CALL\] \$\{(.+?)\}`\)/g,
replacement: 'debugLog.logState("API", `API Call: ${$1}`)'
},
// Customer detection logging
{
pattern: /console\.error\(`\[CUSTOMER-DETECTION\] (.+?)`\)/g,
replacement: 'debugLog.logState("CustomerDetection", "$1")'
},
// Service filter logging
{
pattern: /console\.error\(`\[SERVICE-FILTER\] (.+?)`\)/g,
replacement: 'debugLog.logState("ServiceFilter", "$1")'
},
// Budget API logging
{
pattern: /console\.error\(`\[BUDGET-API\] (.+?)`\)/g,
replacement: 'debugLog.logState("BudgetAPI", "$1")'
},
// OAuth/Auth logging
{
pattern: /console\.error\('\[AUTH\] (.+?)'\)/g,
replacement: 'debugLog.logState("Auth", "$1")'
},
{
pattern: /console\.error\('\[OAUTH\] (.+?)'\)/g,
replacement: 'debugLog.logState("OAuth", "$1")'
},
// Transport logging
{
pattern: /console\.error\('\[HTTPS-TRANSPORT\] (.+?)'\)/g,
replacement: 'debugLog.logState("HttpsTransport", "$1")'
},
{
pattern: /console\.error\('\[TRANSPORT-DEBUG\] (.+?)'\)/g,
replacement: 'debugLog.logState("Transport", "$1")'
},
// Dual auth logging
{
pattern: /console\.error\('\[DUAL-AUTH\] (.+?)'\)/g,
replacement: 'debugLog.logState("DualAuth", "$1")'
},
// Session logging
{
pattern: /console\.error\('\[SESSION\] (.+?)'\)/g,
replacement: 'debugLog.logState("Session", "$1")'
},
// MCP logging
{
pattern: /console\.error\('\[MCP\] (.+?)'\)/g,
replacement: 'debugLog.logState("MCP", "$1")'
},
// Debug logging with template literals
{
pattern: /console\.error\(`\[DEBUG\] (.+?)`\)/g,
replacement: 'debugLog.logState("Debug", `$1`)'
},
// Simple console.error messages
{
pattern: /console\.error\('([^[].+?)'\)/g,
replacement: 'debugLog.logState("General", "$1")'
},
// Console.log patterns for MCP requests/responses
{
pattern: /console\.log\('\\n=== MCP REQUEST: (.+?) ==='\)/g,
replacement: 'debugLog.logState("MCP-Request", "$1")'
},
{
pattern: /console\.log\('\\n=== MCP RESPONSE: (.+?) ==='\)/g,
replacement: 'debugLog.logState("MCP-Response", "$1")'
},
// Remove decorative console.log lines
{
pattern: /console\.log\('=+'\);?\n/g,
replacement: ''
},
{
pattern: /console\.log\(`\['='\.\(60\)\]`\);?\n/g,
replacement: ''
}
];
// Apply replacements
replacements.forEach(({ pattern, replacement }) => {
content = content.replace(pattern, replacement);
});
// Handle multi-line console.error with JSON.stringify
content = content.replace(
/console\.error\(`\[(.+?)\] (.+?): \$\{JSON\.stringify\((.+?), null, 2\)\}`\)/g,
'debugLog.logState("$1", "$2", $3)'
);
// Handle console.error with variables
content = content.replace(
/console\.error\(`\[(.+?)\]`?, (.+?)\)/g,
(match, tag, rest) => {
if (tag && !rest.startsWith('"') && !rest.startsWith("'") && !rest.startsWith('`')) {
return `debugLog.logState("${tag}", "Data", ${rest})`;
}
return match;
}
);
// Write back only if changed
if (content !== originalContent) {
fs.writeFileSync(fullPath, content);
console.log(`✅ Updated ${filePath}`);
} else {
console.log(`⏭️ No changes needed for ${filePath}`);
}
}
// Update all files
console.log('Updating logging in files...\n');
files.forEach(updateFile);
console.log('\n✨ Logging update complete!');
console.log('\nNext steps:');
console.log('1. Run "npm run build" to compile TypeScript');
console.log('2. Restart the server to use the new logging');
console.log('3. Check logs/debug-*.log for all debug output');