3
3
import { readFileSync , existsSync } from 'fs' ;
4
4
import path from 'path' ;
5
5
import { execSync } from 'child_process' ;
6
+ import type { ExecSyncOptionsWithStringEncoding } from 'child_process' ;
6
7
7
- const args = process . argv . slice ( 2 ) ;
8
- const scanEntireRepo = args . includes ( '--scan-entire-repo' ) ;
8
+ const args : string [ ] = process . argv . slice ( 2 ) ;
9
+ const scanEntireRepo : boolean = args . includes ( '--scan-entire-repo' ) ;
9
10
10
- const containsSkipComment = ( file ) => {
11
+ const containsSkipComment = ( file : string ) : boolean => {
11
12
try {
12
13
const content = readFileSync ( file , 'utf-8' ) ;
13
14
return content . includes ( '// SKIP_LOCALSTORAGE_CHECK' ) ;
14
15
} catch ( error ) {
15
- console . error ( `Error reading file ${ file } :` , error . message ) ;
16
+ console . error (
17
+ `Error reading file ${ file } :` ,
18
+ error instanceof Error ? error . message : error ,
19
+ ) ;
16
20
return false ;
17
21
}
18
22
} ;
19
23
20
- const getModifiedFiles = ( ) => {
24
+ const getModifiedFiles = ( ) : string [ ] => {
21
25
try {
26
+ const options : ExecSyncOptionsWithStringEncoding = { encoding : 'utf-8' } ;
27
+
22
28
if ( scanEntireRepo ) {
23
- const result = execSync ( 'git ls-files | grep ".tsx\\?$"' , {
24
- encoding : 'utf-8' ,
25
- } ) ;
29
+ const result = execSync ( 'git ls-files | grep ".tsx\\?$"' , options ) ;
26
30
return result . trim ( ) . split ( '\n' ) ;
27
31
}
28
32
29
- const result = execSync ( 'git diff --cached --name-only' , {
30
- encoding : 'utf-8' ,
31
- } ) ;
33
+ const result = execSync ( 'git diff --cached --name-only' , options ) ;
32
34
return result . trim ( ) . split ( '\n' ) ;
33
35
} catch ( error ) {
34
- console . error ( 'Error fetching modified files:' , error . message ) ;
36
+ console . error (
37
+ 'Error fetching modified files:' ,
38
+ error instanceof Error ? error . message : error ,
39
+ ) ;
35
40
process . exit ( 1 ) ;
36
41
}
42
+ return [ ] ;
37
43
} ;
38
44
39
- const files = getModifiedFiles ( ) ;
40
-
41
- const filesWithLocalStorage = [ ] ;
45
+ const files : string [ ] = getModifiedFiles ( ) ;
46
+ const filesWithLocalStorage : string [ ] = [ ] ;
42
47
43
- const checkLocalStorageUsage = ( file ) => {
48
+ const checkLocalStorageUsage = ( file : string ) : void => {
44
49
if ( ! file ) {
45
50
return ;
46
51
}
@@ -49,7 +54,7 @@ const checkLocalStorageUsage = (file) => {
49
54
50
55
// Skip files with specific names or containing a skip comment
51
56
if (
52
- fileName === 'check-localstorage-usage.js ' ||
57
+ fileName === 'check-localstorage-usage.ts ' || // Updated extension
53
58
fileName === 'useLocalstorage.test.ts' ||
54
59
fileName === 'useLocalstorage.ts' ||
55
60
containsSkipComment ( file )
@@ -73,7 +78,10 @@ const checkLocalStorageUsage = (file) => {
73
78
console . log ( `File ${ file } does not exist.` ) ;
74
79
}
75
80
} catch ( error ) {
76
- console . error ( `Error reading file ${ file } :` , error . message ) ;
81
+ console . error (
82
+ `Error reading file ${ file } :` ,
83
+ error instanceof Error ? error . message : error ,
84
+ ) ;
77
85
}
78
86
} ;
79
87
@@ -86,10 +94,10 @@ if (filesWithLocalStorage.length > 0) {
86
94
87
95
console . info (
88
96
'\x1b[34m%s\x1b[0m' ,
89
- '\nInfo: Consider using custom hook functions.'
97
+ '\nInfo: Consider using custom hook functions.' ,
90
98
) ;
91
99
console . info (
92
- 'Please use the getItem, setItem, and removeItem functions provided by the custom hook useLocalStorage.\n'
100
+ 'Please use the getItem, setItem, and removeItem functions provided by the custom hook useLocalStorage.\n' ,
93
101
) ;
94
102
95
103
process . exit ( 1 ) ;
0 commit comments