1- const { existsSync, readdirSync, readFileSync, statSync } = require ( "fs" ) ;
2- const { join } = require ( "path" ) ;
3- const { yellow } = require ( "ansi-colors" ) ;
1+ import { existsSync , readdirSync , readFileSync , statSync } from "fs" ;
2+ import { join } from "path" ;
3+ import { yellow } from "ansi-colors" ;
44
5- function checkForEnzymeUsage ( srcDir = "src" ) {
5+ export function checkForEnzymeUsage ( srcDir : string = "src" ) : void {
66 const projectRoot = process . cwd ( ) ;
77 const srcPath = join ( projectRoot , srcDir ) ;
88
99 if ( ! existsSync ( srcPath ) ) {
1010 return ;
1111 }
1212
13- const enzymeFiles = [ ] ;
13+ const enzymeFiles : string [ ] = [ ] ;
1414
15- function scanDirectory ( dir ) {
15+ function scanDirectory ( dir : string ) : void {
1616 try {
1717 const entries = readdirSync ( dir ) ;
18+ const isTestDir = / _ _ t e s t s _ _ | [ / \\ ] t e s t [ / \\ ] / . test ( dir ) ;
19+
1820 for ( const entry of entries ) {
1921 const fullPath = join ( dir , entry ) ;
2022 const stat = statSync ( fullPath ) ;
2123
2224 if ( stat . isDirectory ( ) ) {
2325 scanDirectory ( fullPath ) ;
24- } else if (
25- stat . isFile ( ) &&
26- ( entry . endsWith ( ".js" ) ||
27- entry . endsWith ( ".jsx" ) ||
28- entry . endsWith ( ".ts" ) ||
29- entry . endsWith ( ".tsx" ) )
26+ continue ;
27+ }
28+
29+ const isTestFile = / \. ( s p e c | t e s t ) \. ( j s x ? | t s x ? ) $ / . test ( entry ) ;
30+ if ( ! stat . isFile ( ) || ( ! isTestFile && ! isTestDir ) ) {
31+ continue ;
32+ }
33+
34+ const content = readFileSync ( fullPath , "utf8" ) ;
35+ if (
36+ / ( f r o m | r e q u i r e \s * \( ) \s * [ ' " ] e n z y m e [ ' " ] | e n z y m e .* (?: s h a l l o w | m o u n t | r e n d e r ) | (?: s h a l l o w | m o u n t | r e n d e r ) .* e n z y m e / . test (
37+ content
38+ )
3039 ) {
31- const content = readFileSync ( fullPath , "utf8" ) ;
32- // Check for enzyme imports or requires
33- if (
34- / f r o m \s + [ ' " ] e n z y m e [ ' " ] / . test ( content ) ||
35- / r e q u i r e \s * \( \s * [ ' " ] e n z y m e [ ' " ] \s * \) / . test ( content ) ||
36- / i m p o r t \s + .* \s + f r o m \s + [ ' " ] e n z y m e [ ' " ] / . test ( content ) ||
37- / s h a l l o w | m o u n t | r e n d e r / . test ( content ) && / e n z y m e / . test ( content )
38- ) {
39- enzymeFiles . push ( fullPath . replace ( projectRoot , "." ) ) ;
40- }
40+ enzymeFiles . push ( fullPath . replace ( projectRoot , "." ) ) ;
4141 }
4242 }
4343 } catch ( error ) {
44-
44+ console . error ( `Error scanning directory ${ dir } :` , error ) ;
4545 }
4646 }
4747
@@ -64,5 +64,3 @@ function checkForEnzymeUsage(srcDir = "src") {
6464 console . log ( ) ;
6565 }
6666}
67-
68- module . exports = { checkForEnzymeUsage } ;
0 commit comments