1
- import * as fs from "fs" ;
1
+ import { promises as fs } from "fs" ;
2
2
import * as path from "path" ;
3
3
import type { Configuration } from "./Configuration.js" ;
4
4
5
+ export interface FileChange {
6
+ filename : string ;
7
+ status : string ;
8
+ additions : number ;
9
+ deletions : number ;
10
+ changes : number ;
11
+ contents_url ?: string ;
12
+ }
13
+
5
14
/**
6
15
* DirectoryTraversal class for traversing through directories and files.
7
16
* @class DirectoryTraversal
@@ -30,15 +39,19 @@ export class DirectoryTraversal {
30
39
"static" ,
31
40
] ;
32
41
42
+ private fileChanges : Map < string , FileChange > ;
43
+
33
44
/**
34
45
* Constructor for directory traversal
35
46
* @param {Configuration } config - Configuration object containing paths and exclusions
36
- * @param {string [] } [prFiles =[]] - PR files to process
47
+ * @param {FileChange [] } [changes =[]] - Array of file changes from git diff
37
48
*/
38
49
constructor (
39
50
private config : Configuration ,
40
- public prFiles : string [ ] = [ ]
41
- ) { }
51
+ changes : FileChange [ ] = [ ]
52
+ ) {
53
+ this . fileChanges = new Map ( changes . map ( change => [ change . filename , change ] ) ) ;
54
+ }
42
55
43
56
/**
44
57
* Gets the absolute path for a file
@@ -55,62 +68,76 @@ export class DirectoryTraversal {
55
68
}
56
69
57
70
/**
58
- * Traverses the directory based on PRFiles or all files in the root directory.
59
- * If PRFiles are detected, processes only files from the PR.
71
+ * Gets the file change information for a given file
72
+ * @param {string } filePath - The path to the file
73
+ * @returns {FileChange | undefined } The file change information if available
74
+ */
75
+ public getFileChange ( filePath : string ) : FileChange | undefined {
76
+ const relativePath = this . getRelativePath ( filePath ) ;
77
+ return this . fileChanges . get ( relativePath ) ;
78
+ }
79
+
80
+ /**
81
+ * Traverses the directory based on file changes from git diff.
82
+ * If changes are detected, processes only changed files.
60
83
* Otherwise, scans all files in the root directory for TypeScript files.
61
84
*
62
- *
63
85
* @returns An array of string containing the files to process.
64
86
*/
65
87
public traverse ( ) : string [ ] {
66
- if ( this . prFiles . length > 0 ) {
67
- console . log ( "Detected PR Files:" , this . prFiles ) ;
88
+ if ( this . fileChanges . size > 0 ) {
89
+ console . log ( "Detected Changes:" , Array . from ( this . fileChanges . values ( ) ) ) ;
90
+
91
+ // Changes are already relative to repo root, filter and convert to absolute paths
92
+ const files = Array . from ( this . fileChanges . entries ( ) )
93
+ . filter ( ( [ file , change ] : [ string , FileChange ] ) => {
94
+ // Skip deleted files
95
+ if ( change . status === 'deleted' ) {
96
+ return false ;
97
+ }
68
98
69
- // PR files are already relative to repo root, filter and convert to absolute paths
70
- const files = this . prFiles
71
- . filter ( ( file ) => {
72
- // Convert PR file (repo-relative) to absolute path
99
+ // Convert file (repo-relative) to absolute path
73
100
const absolutePath = this . config . toAbsolutePath ( file ) ;
74
101
75
- // Check if the file is within our target directory
76
- const isInTargetDir = absolutePath . startsWith (
77
- this . config . absolutePath
78
- ) ;
79
-
80
- return (
81
- isInTargetDir &&
82
- fs . existsSync ( absolutePath ) &&
83
- ! this . isExcluded ( absolutePath ) &&
84
- path . extname ( file ) === ".ts"
85
- ) ;
102
+ // // Check if the file is within our target directory
103
+ // const isInTargetDir = absolutePath.startsWith(
104
+ // this.config.absolutePath
105
+ // );
106
+
107
+ // return (
108
+ // isInTargetDir &&
109
+ // fs.existsSync(absolutePath) &&
110
+ // !this.isExcluded(absolutePath) &&
111
+ // path.extname(file) === ".ts"
112
+ // );
86
113
} )
87
- . map ( ( file ) => this . config . toAbsolutePath ( file ) ) ;
114
+ . map ( ( [ file ] ) => this . config . toAbsolutePath ( file ) ) ;
88
115
89
116
console . log ( "Files to process:" , files ) ;
90
117
return files ;
91
118
} else {
92
119
console . log (
93
- "No PR Files Detected, Scanning all files in root directory"
120
+ "No Changes Detected, Scanning all files in root directory"
94
121
) ;
95
122
const typeScriptFiles : string [ ] = [ ] ;
96
123
97
124
const traverseDirectory = ( currentDirectory : string ) => {
98
- const files = fs . readdirSync ( currentDirectory ) ;
99
-
100
- files . forEach ( ( file ) => {
101
- const filePath = path . join ( currentDirectory , file ) ;
102
- const stats = fs . statSync ( filePath ) ;
103
-
104
- if ( stats . isDirectory ( ) ) {
105
- if ( ! this . isExcluded ( filePath ) ) {
106
- traverseDirectory ( filePath ) ;
107
- }
108
- } else if ( stats . isFile ( ) && ! this . isExcluded ( filePath ) ) {
109
- if ( path . extname ( file ) === ".ts" ) {
110
- typeScriptFiles . push ( filePath ) ;
111
- }
112
- }
113
- } ) ;
125
+ // const files = fs.readdirSync(currentDirectory);
126
+
127
+ // files.forEach((file) => {
128
+ // const filePath = path.join(currentDirectory, file);
129
+ // const stats = fs.statSync(filePath);
130
+
131
+ // if (stats.isDirectory()) {
132
+ // if (!this.isExcluded(filePath)) {
133
+ // traverseDirectory(filePath);
134
+ // }
135
+ // } else if (stats.isFile() && !this.isExcluded(filePath)) {
136
+ // if (path.extname(file) === ".ts") {
137
+ // typeScriptFiles.push(filePath);
138
+ // }
139
+ // }
140
+ // });
114
141
} ;
115
142
116
143
traverseDirectory ( this . config . absolutePath ) ;
0 commit comments