Created
January 24, 2026 12:29
-
-
Save sibelius/966a8d47d738e8313f604e7edb00f13c to your computer and use it in GitHub Desktop.
simple codemod from console.log to @woovi/logger
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
| import path from 'path'; | |
| export default function transformer(file, api) { | |
| // 🧹 Skip unwanted folders | |
| if ( | |
| file.path.includes('node_modules') || | |
| file.path.includes('dist') || | |
| file.path.includes('build') | |
| ) { | |
| return file.source; | |
| } | |
| const j = api.jscodeshift; | |
| const root = j(file.source); | |
| const consoleToLoggerMap = { | |
| log: 'info', | |
| warn: 'warn', | |
| error: 'error', | |
| debug: 'debug', | |
| }; | |
| // Track if we changed any console.* | |
| let didReplace = false; | |
| // Check if 'logger' is already imported | |
| const hasLoggerImport = root.find(j.ImportDeclaration).some(path => | |
| path.node.specifiers.some(spec => { | |
| const importedName = spec.imported && spec.imported.name; | |
| const localName = spec.local && spec.local.name; | |
| return importedName === 'logger' || localName === 'logger'; | |
| }) | |
| ); | |
| // Replace console.<method>() → logger.<method>() | |
| Object.entries(consoleToLoggerMap).forEach(([consoleMethod, loggerMethod]) => { | |
| const matches = root.find(j.CallExpression, { | |
| callee: { | |
| object: { name: 'console' }, | |
| property: { name: consoleMethod }, | |
| }, | |
| }); | |
| if (matches.size() > 0) { | |
| didReplace = true; | |
| } | |
| matches.forEach(path => { | |
| path.node.callee = j.memberExpression( | |
| j.identifier('logger'), | |
| j.identifier(loggerMethod) | |
| ); | |
| }); | |
| }); | |
| // Add import only if we replaced something and there is no existing import | |
| if (didReplace && !hasLoggerImport) { | |
| const importDecl = j.importDeclaration( | |
| [j.importSpecifier(j.identifier('logger'))], | |
| j.literal('@woovi/logger') | |
| ); | |
| root.get().node.program.body.unshift(importDecl); | |
| } | |
| return root.toSource({ quote: 'single' }); | |
| } | |
| export const parser = 'tsx'; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment