@@ -7,32 +7,46 @@ const traverse = require("@babel/traverse").default;
7
7
const generator = require ( "@babel/generator" ) . default ;
8
8
9
9
const consoleMethods = [
10
- "assert" , "clear" , "count" , "countReset" , "debug" , "dir" , "dirxml" ,
11
- "error" , "group" , "groupCollapsed" , "groupEnd" , "info" , "log" ,
12
- "table" , "time" , "timeEnd" , "timeLog" , "timeStamp" , "trace" , "warn"
10
+ "assert" ,
11
+ "clear" ,
12
+ "count" ,
13
+ "countReset" ,
14
+ "debug" ,
15
+ "dir" ,
16
+ "dirxml" ,
17
+ "error" ,
18
+ "group" ,
19
+ "groupCollapsed" ,
20
+ "groupEnd" ,
21
+ "info" ,
22
+ "log" ,
23
+ "table" ,
24
+ "time" ,
25
+ "timeEnd" ,
26
+ "timeLog" ,
27
+ "timeStamp" ,
28
+ "trace" ,
29
+ "warn" ,
13
30
] ;
14
31
15
- const args = process . argv . slice ( 3 ) ;
16
- const removeAllLogs = args . includes ( "all" ) ;
17
- const allowedMethods = removeAllLogs
18
- ? consoleMethods
19
- : args [ 0 ] ?. split ( "," ) || [ "log" ] ;
20
-
21
32
function removeConsoleLogsFromFile ( filePath ) {
22
33
let code = fs . readFileSync ( filePath , "utf-8" ) ;
23
-
24
34
const ast = parser . parse ( code , { sourceType : "module" , plugins : [ "jsx" ] } ) ;
35
+ const args = process . argv . slice ( 3 ) ;
36
+ const allowedMethods = args . includes ( "all" )
37
+ ? consoleMethods
38
+ : args [ 0 ] ?. split ( "," ) || [ "log" ] ;
25
39
26
40
traverse ( ast , {
27
41
CallExpression ( path ) {
28
42
const { callee } = path . node ;
29
43
if (
30
- callee . object ?. name === "console" &&
44
+ callee . object ?. name === "console" &&
31
45
allowedMethods . includes ( callee . property ?. name )
32
46
) {
33
47
path . remove ( ) ;
34
48
}
35
- }
49
+ } ,
36
50
} ) ;
37
51
38
52
const { code : newCode } = generator ( ast , { retainLines : true } ) ;
@@ -44,7 +58,9 @@ function processDirectory(dirPath) {
44
58
const fullPath = path . join ( dirPath , file ) ;
45
59
if ( fs . statSync ( fullPath ) . isDirectory ( ) ) {
46
60
processDirectory ( fullPath ) ;
47
- } else if ( [ ".js" , ".ts" , ".jsx" , ".tsx" ] . some ( ext => file . endsWith ( ext ) ) ) {
61
+ } else if (
62
+ [ ".js" , ".ts" , ".jsx" , ".tsx" ] . some ( ( ext ) => file . endsWith ( ext ) )
63
+ ) {
48
64
removeConsoleLogsFromFile ( fullPath ) ;
49
65
console . log ( `Processed: ${ fullPath } ` ) ;
50
66
}
0 commit comments