@@ -6,6 +6,8 @@ const parser = require("@babel/parser");
6
6
const traverse = require ( "@babel/traverse" ) . default ;
7
7
const generator = require ( "@babel/generator" ) . default ;
8
8
9
+
10
+
9
11
const consoleMethods = [
10
12
"assert" ,
11
13
"clear" ,
@@ -29,30 +31,62 @@ const consoleMethods = [
29
31
"warn" ,
30
32
] ;
31
33
34
+ // Retrieve command-line arguments (excluding first two default arguments)
35
+ const rawArgs = process . argv . slice ( 3 ) ;
36
+ // Normalize arguments by removing leading dashes, e.g., "--dry-run" becomes "dry-run"
37
+ const args = rawArgs . map ( arg => arg . replace ( / ^ - - ? / , "" ) ) ;
38
+ // Check if dry-run mode is enabled
39
+ const dryRun = args . includes ( "dry-run" ) ;
40
+ // Check if backup mode is enabled
41
+ const backup = args . includes ( "backup" ) ;
42
+
32
43
function removeConsoleLogsFromFile ( filePath ) {
33
44
let code = fs . readFileSync ( filePath , "utf-8" ) ;
34
- const ast = parser . parse ( code , { sourceType : "module" , plugins : [ "jsx" ] } ) ;
45
+ const ast = parser . parse ( code , {
46
+ sourceType : "module" ,
47
+ plugins : [ "jsx" , "typescript" ] // Add "typescript" here
48
+ } ) ;
35
49
const args = process . argv . slice ( 3 ) ;
36
50
const allowedMethods = args . includes ( "all" )
37
51
? consoleMethods
38
52
: args [ 0 ] ?. split ( "," ) || [ "log" ] ;
39
53
54
+ // Counter for the number of removed console calls in this file
55
+ let removedCount = 0 ;
56
+
40
57
traverse ( ast , {
41
58
CallExpression ( path ) {
42
59
const { callee } = path . node ;
60
+ // Check if the call is a console method and if it is allowed to be removed
43
61
if (
44
62
callee . object ?. name === "console" &&
45
63
allowedMethods . includes ( callee . property ?. name )
46
64
) {
65
+ removedCount ++ ;
47
66
path . remove ( ) ;
48
67
}
49
68
} ,
50
69
} ) ;
51
70
52
- const { code : newCode } = generator ( ast , { retainLines : true } ) ;
53
- fs . writeFileSync ( filePath , newCode , "utf-8" ) ;
71
+ // If any console calls were removed, generate new code
72
+ if ( removedCount > 0 ) {
73
+ const { code : newCode } = generator ( ast , { retainLines : true } ) ;
74
+ if ( dryRun ) {
75
+ // In dry-run mode, just log the changes without modifying the file
76
+ console . log ( `[Dry Run] ${ filePath } would have removed ${ removedCount } console call(s).` ) ;
77
+ } else {
78
+ // If backup flag is enabled, create a backup file before modifying the original file
79
+ if ( backup ) {
80
+ fs . copyFileSync ( filePath , filePath + ".bak" ) ;
81
+ console . log ( `Backup created for: ${ filePath } ` ) ;
82
+ }
83
+ fs . writeFileSync ( filePath , newCode , "utf-8" ) ;
84
+ console . log ( `Processed: ${ filePath } (removed ${ removedCount } console call(s))` ) ;
85
+ }
86
+ }
54
87
}
55
88
89
+ // Recursive function to process all files in the given directory
56
90
function processDirectory ( dirPath ) {
57
91
fs . readdirSync ( dirPath ) . forEach ( ( file ) => {
58
92
const fullPath = path . join ( dirPath , file ) ;
@@ -62,11 +96,17 @@ function processDirectory(dirPath) {
62
96
[ ".js" , ".ts" , ".jsx" , ".tsx" ] . some ( ( ext ) => file . endsWith ( ext ) )
63
97
) {
64
98
removeConsoleLogsFromFile ( fullPath ) ;
65
- console . log ( `Processed: ${ fullPath } ` ) ;
66
99
}
67
100
} ) ;
68
101
}
69
102
103
+ // Get the target directory from command-line arguments (default to current directory)
70
104
const targetDir = process . argv [ 2 ] || "." ;
71
105
processDirectory ( targetDir ) ;
72
- console . log ( "✅ All console.log statements removed!" ) ;
106
+
107
+ // Final log message based on mode used
108
+ if ( dryRun ) {
109
+ console . log ( "✅ Dry run complete! No files were modified." ) ;
110
+ } else {
111
+ console . log ( "✅ All specified console.log statements removed!" ) ;
112
+ }
0 commit comments