-
Notifications
You must be signed in to change notification settings - Fork 0
/
files-to-prompt.ts
executable file
·694 lines (640 loc) · 22.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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
#!/usr/bin/env bun
import fs from 'node:fs';
import os from 'node:os';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import { execSync } from 'node:child_process';
const VERSION = 'v0.5.2';
/**
* Represents runtime compatibility configuration with various engines.
* @interface ComaptConfig
* @property {boolean} isDeno - Set when the Deno runtime was detected
* @property {boolean} isNode - Set when the Node runtime was detected
*/
interface CompatConfig {
isDeno: boolean;
isNode: boolean;
}
const compatConfig: CompatConfig = {
isDeno: false,
isNode: false,
}
/**
* Represents the configuration for output.
* @interface OutputConfig
* @property {string} stdoutFile - Filename to redirect stdout.
* @property {string} stderrFile - Filename to redirect stderr.
*/
interface OutputConfig {
stdoutFile: string;
stderrFile: string;
}
const outputConfig: OutputConfig = {
stdoutFile: '',
stderrFile: '',
}
/**
* 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.
* @property {string} nbconvertName - Filename or full path of nbconvert tool to convert .ipynb files to ASCII or Markdown.
* @property {string} nbconvertFormat - The format to convert .ipynb files to ('asciidoc' or 'markdown').
*/
interface ProcessingConfig {
includeHidden: boolean;
ignoreGitignore: boolean;
ignorePatterns: string[];
gitignoreRules: string[];
nbconvertName: string;
nbconvertFormat: 'asciidoc' | 'markdown';
}
/**
* Outputs the provided arguments to the console.
* exported so it can be overridden in test script
* implicitly tested (no test case)
* @function consoleOutput
* @param {...any[]} args - The arguments to log.
*/
export function consoleOutput(...args: any[]): void {
console.log(...args);
}
/**
* Outputs the provided arguments to the console or file.
* @function output
* @param {...any[]} args - The arguments to log.
*/
function output(...args: any[]): void {
if (outputConfig.stdoutFile) {
try {
fs.appendFileSync(outputConfig.stdoutFile, args.join(' ') + '\n');
} catch (err) {
error(`Error writing to output file ${outputConfig.stdoutFile}: ${err}`);
}
} else {
consoleOutput(...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 consoleError
* @param {...any[]} args - The arguments to log.
*/
export function consoleError(...args: any[]): void {
console.error(...args);
}
/**
* Outputs the provided arguments to the console or file as an error.
* @function error
* @param {...any[]} args - The arguments to log as an error.
*/
function error(...args: any[]): void {
if (outputConfig.stderrFile) {
try {
fs.appendFileSync(outputConfig.stderrFile, args.join(' ') + '\n');
} catch (err) {
error(`Error writing to error file ${outputConfig.stderrFile}: ${err}`);
}
} else {
consoleError(...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.
* @param {ProcessingConfig} config - The processing configuration.
* @returns {Promise<void>}
*/
async function processFile(filePath: string, config: ProcessingConfig): Promise<void> {
try {
if (config.nbconvertName && filePath.endsWith('.ipynb')) {
// Handle Jupyter Notebook files first
if (config.nbconvertName === 'internal') {
// internal conversion requested
await convertNotebookInternal(filePath, config);
} else {
// external conversion requested
await convertNotebookExternal(filePath, config);
}
} else if (await isBinaryFile(filePath)) {
// Skip binary files
error(`Warning: Skipping binary file ${filePath}`);
} else {
// Put everything else verbatim on the output stream
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 with an earlier version of this script, where main()
// was not returning a promise correctly. This lead to the test framework to
// cleaning up files before this tool was done processing.
// TODO: write test case (not trivial)
error(`Error processing file ${filePath}: ${err}`);
}
}
/**
* Converts a Jupyter Notebook file to the specified format using internal conversion.
* @async
* @function convertNotebookInternal
* @param {string} filePath - The path to the Jupyter Notebook file.
* @param {ProcessingConfig} config - The processing configuration.
* @returns {Promise<void>}
*/
async function convertNotebookInternal(filePath: string, config: ProcessingConfig): Promise<void> {
try {
const ipynbContents = await fs.promises.readFile(filePath, 'utf8');
const ipynbData = JSON.parse(ipynbContents);
let convertedContent = '';
if (config.nbconvertFormat === 'asciidoc') {
convertedContent = convertToAsciidoc(ipynbData);
} else {
convertedContent = convertToMarkdown(ipynbData);
}
output(`${filePath}`);
output('---');
output(convertedContent);
output('---');
} catch (err) {
error(`Error converting .ipynb file ${filePath}: ${err}`);
}
}
/**
* Converts Jupyter Notebook JSON to AsciiDoc format.
* @function convertToAsciidoc
* @param {any} ipynbData - The parsed JSON data of the Jupyter Notebook file.
* @returns {string} - The AsciiDoc content.
*/
function convertToAsciidoc(ipynbData: any): string {
let asciidocContent = '';
for (const cell of ipynbData.cells) {
switch (cell.cell_type) {
case 'code':
asciidocContent += `+*In[${cell.execution_count}]:*+\n[source, ipython3]\n----\n${cell.source.join('')}\n----\n\n`;
for (const output of cell.outputs) {
if (output.data['text/plain']) {
asciidocContent += `+*Out[${cell.execution_count}]:*+\n----\n${output.data['text/plain']}\n----\n\n`;
}
// TODO: handle images
// if (output.data['image/png']) {
// asciidocContent += `+*Out[${cell.execution_count}]:*+\n[PNG Image]\n\n`;
// }
}
break;
case 'markdown':
asciidocContent += `${cell.source.join('')}\n\n`;
break;
}
}
return asciidocContent;
}
/**
* Converts Jupyter Notebook JSON to Markdown format.
* @function convertToMarkdown
* @param {any} ipynbData - The parsed JSON data of the Jupyter Notebook file.
* @returns {string} - The Markdown content.
*/
function convertToMarkdown(ipynbData: any): string {
let markdownContent = '';
for (const cell of ipynbData.cells) {
switch (cell.cell_type) {
case 'code':
markdownContent += `\`\`\`python\n${cell.source.join('')}\n\`\`\`\n\n`;
for (const output of cell.outputs) {
if (output.data['text/plain']) {
markdownContent += `\`\`\`\n${output.data['text/plain']}\n\`\`\`\n\n`;
}
// TODO: handle images
// if (output.data['image/png']) {
// }
}
break;
case 'markdown':
markdownContent += `${cell.source.join('')}\n\n`;
break;
}
}
return markdownContent;
}
/**
* Converts a Jupyter Notebook file to the specified format using external conversion.
* @async
* @function convertIPythonNotebookExternal
* @param {string} filePath - The path to the Jupyter Notebook file.
* @param {ProcessingConfig} config - The processing configuration.
* @returns {Promise<void>}
*/
async function convertNotebookExternal(filePath: string, config: ProcessingConfig): Promise<void> {
const tempDir = await fs.promises.mkdtemp(path.join(os.tmpdir(), 'files-to-prompt-'));
const tempFilePath = path.join(tempDir, path.basename(filePath));
try {
// Copy the .ipynb file to the temporary directory
await fs.promises.copyFile(filePath, tempFilePath);
// Run nbconvert with the appropriate command-line options
const convertCommand = `${config.nbconvertName} --to ${config.nbconvertFormat} "${tempFilePath}"`;
try {
execSync(convertCommand, { stdio: 'inherit' });
} catch (err) {
error(`Error running ${config.nbconvertName}: ${err}`);
return;
}
// Determine the correct file extension based on the conversion format
const convertedFileExtension = config.nbconvertFormat === 'markdown' ? '.md' : `.${config.nbconvertFormat}`;
// Read the converted file from the temporary directory
const convertedFilePath = path.join(tempDir, `${path.basename(filePath, '.ipynb')}${convertedFileExtension}`);
const convertedFileContents = await fs.promises.readFile(convertedFilePath, 'utf8');
// Output the converted file with the original file name and path
output(`${filePath}`);
output('---');
output(convertedFileContents);
output('---');
} catch (err) {
error(`Error converting .ipynb file ${filePath}: ${err}`);
} finally {
// Clean up the temporary directory
await fs.promises.rm(tempDir, { recursive: true, force: true });
}
}
/**
* 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, config);
}
} 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, newConfig);
}
}
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.
* @async
* @function readStdin
* @returns {Promise<string>} - The input from stdin.
*/
async function readStdin(): 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;
}
/**
* Parses the command-line arguments and updates the processing configuration and output configuration.
* @function parseCommandLineArgs
* @param {string[]} args - The command-line arguments array to read from.
* @param {string[]} pathsToProcess - The array with paths to process to append to.
* @param {ProcessingConfig} config - The processing configuration to modify.
* @param {OutputConfig} outputConfig - The output configuration to modify.
* @returns {boolean} - A boolean indicating if an error occurred, true means error.
*/
function parseCommandLineArgs(args: string[], pathsToProcess: string[], config: ProcessingConfig, outputConfig: OutputConfig): boolean {
for (let i = 0; i < args.length; i++) {
const arg = args[i];
switch (arg) {
case '--version':
output(`files-to-prompt.ts ${VERSION}`);
return false;
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 true;
}
break;
case '--output':
case '-o':
if (i + 1 < args.length) {
outputConfig.stdoutFile = args[++i];
try {
fs.writeFileSync(outputConfig.stdoutFile, '');
// delete existing content in file
fs.truncateSync(outputConfig.stdoutFile);
} catch (err) {
error(`Error writing to output file ${outputConfig.stdoutFile}: ${err}`);
outputConfig.stdoutFile = '';
return true;
}
} else {
error('Error: --output option requires a file path');
return true;
}
break;
case '--nbconvert':
if (i + 1 < args.length) {
config.nbconvertName = args[++i];
} else {
error('Error: --nbconvert option requires the filename or full path of the tool or \'internal\'');
return true;
}
if (!(config.nbconvertName === 'internal')) {
try {
execSync(`${config.nbconvertName} --version`, { stdio: 'ignore' });
} catch (err) {
error(`Warning: ${config.nbconvertName} command not found`);
config.nbconvertName = '';
}
}
break;
case '--format':
if (i + 1 < args.length) {
const format = args[++i];
if (format === 'asciidoc' || format === 'markdown') {
config.nbconvertFormat = format;
} else {
error(`Error: Unsupported format '${format}', use 'asciidoc' or 'markdown'`);
return true;
}
} else {
error('Error: --format option requires a format');
return true;
}
break;
default:
if (arg.startsWith('-')) {
error(`Error: Unsupported option '${arg}'`);
return true;
}
// Assume it's a file or directory path to process
pathsToProcess.push(arg);
}
}
return false;
}
/**
* Processes the provided paths recursively.
* @async
* @function processPathsRecursively
* @param {string[]} pathsToProcess - The paths to process.
* @param {ProcessingConfig} config - The processing configuration.
* @returns {Promise<void>} - A Promise that resolves when all the paths have been processed.
*/
async function processPathsRecursively(
pathsToProcess: string[],
config: ProcessingConfig
): Promise<void> {
for (const path of pathsToProcess) {
if (!fs.existsSync(path)) {
error(`Path does not exist: ${path}`);
return;
}
await processPath(path, config);
}
}
/**
* The main entry point of the script.
* @async
* @function main
* @param {string[]} args - The command-line arguments.
* @returns {Promise<void>} - A Promise that resolves when the script has finished processing all the files.
*/
export async function main( args: string[] ): Promise<void> {
return new Promise((resolve, reject) => {
const config: ProcessingConfig = {
includeHidden: false,
ignoreGitignore: false,
ignorePatterns: [],
gitignoreRules: [],
nbconvertName: '',
nbconvertFormat: 'asciidoc',
};
const pathsToProcess: string[] = [];
const hasError = parseCommandLineArgs(args, pathsToProcess, config, outputConfig);
if (hasError) {
// Quit silently before processing further
resolve();
return;
}
// Process input from stdin
if ((compatConfig.isNode && !process.stdin.isTTY) ||
(compatConfig.isDeno && !Deno.stdin.isTerminal())) {
readStdin()
.then((stdinData) => {
const filePathsFromStdin = parseFilePathsFromStdin(stdinData);
pathsToProcess.push(...filePathsFromStdin);
return processPathsRecursively(pathsToProcess, config);
})
.then(() => resolve())
.catch((err) => reject(err));
} else {
processPathsRecursively(pathsToProcess, config)
.then(() => resolve())
.catch((err) => reject(err));
}
});
}
// Check if the script is being run directly and detect the runtime environment,
// then call main() with appropriate arguments.
// Note: main() may not be called from here when this script gets imported in test script
// TODO: write test case (not trivial)
if (import.meta.main || (process.argv[1] === fileURLToPath(import.meta.url))) {
if (typeof Deno !== 'undefined') {
compatConfig.isDeno = true;
await main(Deno.args);
} else {
// for now Bun and Node can be considered equal
compatConfig.isNode = true;
await main(process.argv.slice(2));
}
}