Skip to content

Commit 32f99fb

Browse files
committedApr 16, 2024
Refactor file processing logic and enable strict TypeScript checks
- Add explicit handling for single files, directories, and unsupported file types - Enable strict TypeScript checks to catch unused code
1 parent 2cc985d commit 32f99fb

File tree

2 files changed

+20
-17
lines changed

2 files changed

+20
-17
lines changed
 

‎files-to-prompt.ts

+17-14
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ async function isBinaryFile(filePath: string, chunkSize: number = 8192): Promise
2828
return isBinary;
2929
}
3030

31-
async function processFile(filePath: string, config: ProcessingConfig): Promise<void> {
31+
async function processFile(filePath: string): Promise<void> {
3232
try {
3333
if (await isBinaryFile(filePath)) {
3434
console.error(`Warning: Skipping binary file ${filePath}`);
@@ -81,7 +81,12 @@ async function processPath(
8181
pathToProcess: string,
8282
config: ProcessingConfig
8383
): Promise<void> {
84-
if (fs.statSync(pathToProcess).isDirectory()) {
84+
if (fs.statSync(pathToProcess).isFile()) {
85+
// Process a single file
86+
if (!shouldIgnore(pathToProcess, config)) {
87+
await processFile(pathToProcess);
88+
}
89+
} else if (fs.statSync(pathToProcess).isDirectory()) {
8590
let newConfig: ProcessingConfig = config; // intentional reference copy
8691
if (config.gitignoreRules.length === 0) {
8792
// only check for another .gitingore for this hierarchy part if not already found one
@@ -94,18 +99,18 @@ async function processPath(
9499
}
95100

96101
const files = fs.readdirSync(pathToProcess, { withFileTypes: true })
97-
.filter((dirent: any) => config.includeHidden || !dirent.name.startsWith('.'))
98-
.filter((dirent: any) => dirent.isFile())
99-
.map((dirent: any) => path.join(pathToProcess, dirent.name));
102+
.filter((directoryEntry: fs.Dirent) => config.includeHidden || !directoryEntry.name.startsWith('.'))
103+
.filter((directoryEntry: fs.Dirent) => directoryEntry.isFile())
104+
.map((directoryEntry: fs.Dirent) => path.join(pathToProcess, directoryEntry.name));
100105

101106
const directories = fs.readdirSync(pathToProcess, { withFileTypes: true })
102-
.filter((dirent: any) => config.includeHidden || !dirent.name.startsWith('.'))
103-
.filter((dirent: any) => dirent.isDirectory())
104-
.map((dirent: any) => path.join(pathToProcess, dirent.name));
107+
.filter((directoryEntry: fs.Dirent) => config.includeHidden || !directoryEntry.name.startsWith('.'))
108+
.filter((directoryEntry: fs.Dirent) => directoryEntry.isDirectory())
109+
.map((directoryEntry: fs.Dirent) => path.join(pathToProcess, directoryEntry.name));
105110

106111
for (const file of files) {
107112
if (!shouldIgnore(file, newConfig)) {
108-
await processFile(file, newConfig);
113+
await processFile(file);
109114
}
110115
}
111116

@@ -115,10 +120,8 @@ async function processPath(
115120
}
116121
}
117122
} else {
118-
// Process a single file
119-
if (!shouldIgnore(pathToProcess, config)) {
120-
await processFile(pathToProcess, config);
121-
}
123+
// Skip everything else, e.g. FIFOs, sockets, symlinks
124+
console.error(`Skipping ${pathToProcess}: unsupported file type`);
122125
}
123126
}
124127

@@ -130,7 +133,7 @@ program
130133
.argument('<paths...>', 'One or more paths to files or directories to process')
131134
.option('--include-hidden', 'Include files and folders starting with .', false)
132135
.option('--ignore-gitignore', 'Ignore .gitignore files and include all files', false)
133-
.option('-i, --ignore <pattern>', 'Specify one or more patterns to ignore', (value: any, previous: any) => [...previous, value], [])
136+
.option('-i, --ignore <pattern>', 'Specify one or more patterns to ignore', (value: string, previous: any[]) => [...previous, value], [])
134137
.action((paths, options) => {
135138
const config: ProcessingConfig = {
136139
includeHidden: options.includeHidden,

‎tsconfig.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020
"noFallthroughCasesInSwitch": true,
2121

2222
// Some stricter flags (disabled by default)
23-
"noUnusedLocals": false,
24-
"noUnusedParameters": false,
25-
"noPropertyAccessFromIndexSignature": false
23+
"noUnusedLocals": true,
24+
"noUnusedParameters": true,
25+
"noPropertyAccessFromIndexSignature": true
2626
}
2727
}

0 commit comments

Comments
 (0)
Please sign in to comment.