-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfiles-to-prompt.ts
executable file
·374 lines (343 loc) · 11.4 KB
/
files-to-prompt.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
#!/usr/bin/env bun
import fs from 'node:fs';
import path from 'node:path';
const VERSION = '0.2.1';
/**
* Represents the configuration for the file processing.
* @interface ProcessingConfig
* @property {boolean} includeHidden - Indicates whether to include hidden files and directories.
* @property {boolean} ignoreGitignore - Indicates whether to ignore .gitignore files.
* @property {string[]} ignorePatterns - An array of patterns to ignore.
* @property {string[]} gitignoreRules - An array of .gitignore rules.
*/
interface ProcessingConfig {
includeHidden: boolean;
ignoreGitignore: boolean;
ignorePatterns: string[];
gitignoreRules: string[];
}
/**
* Outputs the provided arguments to the console.
* exported so it can be overridden in test script
* implicitly tested (no test case)
* @function output
* @param {...any[]} args - The arguments to log.
*/
export function output(...args: any[]): void {
console.log(...args);
}
/**
* Outputs the provided arguments to the console as an error.
* exported so it can be overridden in test script
* implicitly tested (no test case)
* @function error
* @param {...any[]} args - The arguments to log as an error.
*/
export function error(...args: any[]): void {
console.error(...args);
}
/**
* Determines whether a file is a binary file.
* exported to be testable in test script
* @async
* @function isBinaryFile
* @param {string} filePath - The path to the file.
* @param {number} [chunkSize=8192] - The size of the chunks to read from the file.
* @returns {Promise<boolean>} - A promise that resolves to `true` if the file is a binary file, `false` otherwise.
*/
export async function isBinaryFile(filePath: string, chunkSize: number = 8192): Promise<boolean> {
let isBinary = false;
let stream: fs.ReadStream;
try {
stream = fs.createReadStream(filePath, { highWaterMark: chunkSize });
} catch (err: any) {
if (err.code === 'ENOENT') {
// File not found, return false
return false;
} else {
// Rethrow the error
// TODO: write test case (not trivial)
throw err;
}
}
for await (const chunk of stream) {
if (chunk instanceof Uint8Array) {
if (Array.from(chunk).some((byte) => byte > 127)) { // Check for non-ASCII character
isBinary = true;
stream.destroy(); // Stop reading the file
break;
}
}
}
return isBinary;
}
/**
* Processes a single file.
* @async
* @function processFile
* @param {string} filePath - The path to the file to process.
* @returns {Promise<void>}
*/
async function processFile(filePath: string): Promise<void> {
try {
if (await isBinaryFile(filePath)) {
error(`Warning: Skipping binary file ${filePath}`);
} else {
const fileContents = fs.readFileSync(filePath, 'utf8');
output(filePath);
output('---');
output(fileContents);
output('---');
}
} catch (err) {
// This should not happen unless e.g. files get deleted while this tool runs
// I ran into this case as the test framework was cleaning up files before this tool was done
// Remove `Bun.sleep()` from the test script and you will end up here
// TODO: write test case (not trivial)
error(`Error processing file ${filePath}: ${err}`);
}
}
/**
* Determines whether a file should be ignored based on the provided configuration.
* @function shouldIgnore
* @param {string} filePath - The path to the file.
* @param {ProcessingConfig} config - The processing configuration.
* @returns {boolean} - `true` if the file should be ignored, `false` otherwise.
*/
function shouldIgnore(filePath: string, config: ProcessingConfig): boolean {
const { ignorePatterns, gitignoreRules } = config;
for (const pattern of [...gitignoreRules, ...ignorePatterns]) {
if (minimatch(path.basename(filePath), pattern)) {
return true;
}
if (pattern.endsWith('/')) {
const dirPattern = pattern.slice(0, -1);
if (minimatch(path.relative(path.dirname(filePath), filePath), dirPattern)) {
return true;
}
}
}
return false;
}
/**
* Reads the .gitignore file from the specified directory.
* @function readGitignore
* @param {string} dirPath - The path to the directory.
* @returns {string[]} - An array of .gitignore rules.
*/
function readGitignore(dirPath: string): string[] {
const gitignorePath = path.join(dirPath, '.gitignore');
if (fs.existsSync(gitignorePath)) {
return fs.readFileSync(gitignorePath, 'utf8')
.split('\n')
.filter((line: string) => line.trim() !== '' && !line.startsWith('#'))
.map((pattern: string) => pattern.trim());
}
return [];
}
/**
* Checks if a filename matches a pattern using minimatch.
* @function minimatch
* @param {string} filename - The filename to match.
* @param {string} pattern - The pattern to match against.
* @returns {boolean} - `true` if the filename matches the pattern, `false` otherwise.
*/
function minimatch(filename: string, pattern: string): boolean {
const regex = new RegExp(`^${pattern.replace(/\*/g, '.*')}$`);
return regex.test(filename);
}
/**
* Processes a file or directory path.
* @async
* @function processPath
* @param {string} pathToProcess - The path to the file or directory to process.
* @param {ProcessingConfig} config - The processing configuration.
* @returns {Promise<void>}
*/
async function processPath(
pathToProcess: string,
config: ProcessingConfig
): Promise<void> {
if (fs.statSync(pathToProcess).isFile()) {
// Process a single file
if (!shouldIgnore(pathToProcess, config)) {
await processFile(pathToProcess);
}
} else if (fs.statSync(pathToProcess).isDirectory()) {
let newConfig: ProcessingConfig = config; // intentional reference copy
if (config.gitignoreRules.length === 0) {
// only check for .gitingore file for this hierarchy part if not already found one
const gitignoreRules = config.ignoreGitignore ? [] : readGitignore(pathToProcess);
if (gitignoreRules.length > 0) {
// deep cloning so current .gitignore rules only apply to current part of the hierarchy
newConfig = structuredClone(config);
newConfig.gitignoreRules = gitignoreRules;
}
}
const files = fs.readdirSync(pathToProcess, { withFileTypes: true })
.filter((directoryEntry: fs.Dirent) => config.includeHidden || !directoryEntry.name.startsWith('.'))
.filter((directoryEntry: fs.Dirent) => directoryEntry.isFile())
.map((directoryEntry: fs.Dirent) => path.join(pathToProcess, directoryEntry.name));
const directories = fs.readdirSync(pathToProcess, { withFileTypes: true })
.filter((directoryEntry: fs.Dirent) => config.includeHidden || !directoryEntry.name.startsWith('.'))
.filter((directoryEntry: fs.Dirent) => directoryEntry.isDirectory())
.map((directoryEntry: fs.Dirent) => path.join(pathToProcess, directoryEntry.name));
for (const file of files) {
if (!shouldIgnore(file, newConfig)) {
await processFile(file);
}
}
for (const dir of directories) {
if (!shouldIgnore(dir, newConfig)) {
await processPath(dir, newConfig);
}
}
} else {
// Skip everything else, e.g. FIFOs, sockets, symlinks
// applies only to files directly specified on the commandline
// files in directories get filtered above
error(`Skipping ${pathToProcess}: unsupported file type`);
}
}
/**
* Reads the input from stdin.
* This function can be overridden in tests.
* @async
* @function readStdin
* @returns {Promise<string>} - The input from stdin.
*/
let readStdin = async (): Promise<string> => {
return new Promise((resolve, reject) => {
let stdinData = '';
process.stdin.on('data', (chunk) => {
stdinData += chunk.toString();
});
process.stdin.on('end', () => {
resolve(stdinData);
});
process.stdin.on('error', (err) => {
reject(err);
});
});
};
/**
* Parses the file paths from the stdin input.
* @function parseFilePathsFromStdin
* @param {string} stdinData - The input from stdin.
* @returns {string[]} - An array of file paths.
*/
export function parseFilePathsFromStdin(stdinData: string): string[] {
const filePathsFromStdin: string[] = [];
const seenFilePaths = new Set<string>();
const lines = stdinData.trim().split('\n');
for (const line of lines) {
const filePath = line.trim();
if (filePath === '') {
// Ignore empty line
continue;
}
if (filePath.includes(':')) {
// Handle grep/ripgrep output format
const parts = filePath.split(':');
if (isValidFilePath(parts[0]) && !seenFilePaths.has(parts[0])) {
seenFilePaths.add(parts[0]);
filePathsFromStdin.push(parts[0]);
}
} else if (isValidFilePath(filePath) && !seenFilePaths.has(filePath)) {
// Handle file path per line format
seenFilePaths.add(filePath);
filePathsFromStdin.push(filePath);
}
}
return filePathsFromStdin;
}
/**
* Checks if a given string is a valid file path.
* @function isValidFilePath
* @param {string} filePath - The file path to check.
* @returns {boolean} - `true` if the file path is valid, `false` otherwise.
*/
function isValidFilePath(filePath: string): boolean {
// Check if the file path contains only valid characters
for (const char of filePath) {
if (char.charCodeAt(0) < 32 || char.charCodeAt(0) > 126) {
return false;
}
}
// Check if the file path is not too long
if (filePath.length > 1024) {
return false;
}
// If the file path passes the above checks, consider it valid
return true;
}
/**
* The main entry point of the script.
* @async
* @function main
* @param {string[]} args - The command-line arguments.
* @returns {Promise<void>}
*/
export async function main( args: string[] ): Promise<void> {
const config: ProcessingConfig = {
includeHidden: false,
ignoreGitignore: false,
ignorePatterns: [],
gitignoreRules: [],
};
let pathsToProcess: string[] = [];
for (let i = 0; i < args.length; i++) {
const arg = args[i];
switch (arg) {
case '--version':
output(`files-to-prompt.ts version ${VERSION}`);
return;
case '--include-hidden':
config.includeHidden = true;
break;
case '--ignore-gitignore':
config.ignoreGitignore = true;
break;
case '-i':
case '--ignore':
if (i + 1 < args.length) {
config.ignorePatterns.push(args[++i]);
} else {
error('Error: --ignore option requires a pattern');
return;
}
break;
default:
if (arg.startsWith('-')) {
error(`Error: Unsupported option '${arg}'`);
return;
}
pathsToProcess.push(arg);
}
}
// Process input from stdin
if (!process.stdin.isTTY) {
const stdinData = await readStdin();
const filePathsFromStdin = parseFilePathsFromStdin(stdinData);
pathsToProcess.push(...filePathsFromStdin);
}
for (const path of pathsToProcess) {
if (!fs.existsSync(path)) {
error(`Path does not exist: ${path}`);
return;
}
await processPath(path, config);
}
return;
}
// Check if the script is being run directly and detect the runtime environment
// main() may not be called here when this script gets imported in the test script
// call the main function with the appropriate arguments
// TODO: write test case (not trivial)
if (import.meta.main) {
if (typeof (globalThis as any).Deno !== 'undefined') {
await main((globalThis as any).Deno.args);
} else {
await main(process.argv.slice(2));
}
}