@@ -12,22 +12,34 @@ commander
12
12
"Array of transformations to be applied in order" ,
13
13
"[]"
14
14
)
15
+ . option ( "-R, --recursive" , "Perform recursive rename" )
15
16
. parse ( process . argv ) ;
16
17
17
- const { match, flags, replace, transformations } = commander ;
18
+ const { match, flags, replace, transformations, recursive } = commander ;
18
19
19
20
const transformationsArray = JSON . parse ( transformations ) ;
20
21
21
22
const currentDirectory = process . cwd ( ) ;
22
23
23
- fs . readdirSync ( currentDirectory ) . forEach ( node =>
24
+ const rename = ( currentDirectory : string ) => ( node : string ) =>
24
25
fs . rename (
25
26
`${ currentDirectory } /${ node } ` ,
26
27
`${ currentDirectory } /${ node . replace (
27
28
new RegExp ( match , flags ) ,
28
- transformationsArray . length
29
- ? transform ( replace ) ( transformationsArray )
30
- : replace
29
+ transform ( replace ) ( transformationsArray )
31
30
) } `
32
- )
31
+ ) ;
32
+
33
+ const renameRecursively = ( currentDirectory : string ) => ( node : string ) => {
34
+ const fullPathToNode = `${ currentDirectory } /${ node } ` ;
35
+
36
+ if ( fs . lstatSync ( fullPathToNode ) . isDirectory ( ) ) {
37
+ fs . readdirSync ( fullPathToNode ) . forEach ( renameRecursively ( fullPathToNode ) ) ;
38
+ } else {
39
+ rename ( currentDirectory ) ( node ) ;
40
+ }
41
+ } ;
42
+
43
+ fs . readdirSync ( currentDirectory ) . forEach (
44
+ recursive ? renameRecursively ( currentDirectory ) : rename ( currentDirectory )
33
45
) ;
0 commit comments