|
1 | 1 | import { execSync, spawn } from 'child_process'; |
2 | 2 |
|
| 3 | +function hasStderr(err: unknown): err is { stderr: string } { |
| 4 | + return typeof err === 'object' && err !== null && 'stderr' in err && typeof (err as { stderr: unknown }).stderr === 'string'; |
| 5 | +} |
| 6 | + |
3 | 7 | // Define a type for the file status and path |
4 | 8 | export type ChangedFile = { |
5 | 9 | status: 'A' | 'M' | 'D' | 'R' | 'C' | 'T' | 'U' | 'X' | 'B'; // Git status codes |
@@ -61,7 +65,7 @@ export function getChangedJsonFilesWithStatus(): ChangedFile[] { |
61 | 65 | }); |
62 | 66 | } catch (error) { |
63 | 67 | // Check if the error is due to missing history (e.g., first commit) |
64 | | - if (error.stderr?.includes('unknown revision or path not in the working tree')) { |
| 68 | + if (hasStderr(error) && error.stderr.includes('unknown revision or path not in the working tree')) { |
65 | 69 | console.warn( |
66 | 70 | 'Could not find previous commit (HEAD~1). Checking working tree status against index...' |
67 | 71 | ); |
@@ -122,21 +126,21 @@ export async function getOldFileContent(gitPath: string): Promise<string> { |
122 | 126 | let oldFileContent = ''; |
123 | 127 | let errorOutput = ''; |
124 | 128 |
|
125 | | - gitShow.stdout.on('data', (data) => { |
| 129 | + gitShow.stdout.on('data', (data: Buffer) => { |
126 | 130 | oldFileContent += data.toString(); |
127 | 131 | }); |
128 | 132 |
|
129 | | - gitShow.stderr.on('data', (data) => { |
| 133 | + gitShow.stderr.on('data', (data: Buffer) => { |
130 | 134 | errorOutput += data.toString(); |
131 | 135 | }); |
132 | 136 |
|
133 | | - gitShow.on('error', (err) => { |
| 137 | + gitShow.on('error', (err: Error) => { |
134 | 138 | // Handle errors spawning the process itself |
135 | 139 | console.error(`Error spawning git show for HEAD~1:${gitPath}:`, err); |
136 | 140 | resolve(''); // Resolve with empty string on spawn error |
137 | 141 | }); |
138 | 142 |
|
139 | | - gitShow.on('close', (code) => { |
| 143 | + gitShow.on('close', (code: number | null) => { |
140 | 144 | if (code === 0) { |
141 | 145 | resolve(oldFileContent); |
142 | 146 | } else { |
|
0 commit comments