Skip to content

Commit 1808fdf

Browse files
committed
Update llm-prepare.js
1 parent 0b447bd commit 1808fdf

1 file changed

Lines changed: 13 additions & 6 deletions

File tree

llm-prepare.js

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,14 @@ const argv = yargs(hideBin(process.argv))
3636
.version("v", "Display the version number", packageJson.version) // Adding version command
3737
.alias("v", "version").argv;
3838

39-
// Compile file pattern only once
39+
// Safely escape special characters and convert wildcard * to .*
40+
const escapeRegExp = (string) => string.replace(/[.+?^${}()|[\]\\]/g, "\\$&");
41+
const convertWildcard = (pattern) => pattern.replace(/\*/g, ".*");
42+
4043
const filePattern = new RegExp(
41-
argv.filePattern === "*" ? ".*" : argv.filePattern
44+
argv.filePattern === "*"
45+
? ".*"
46+
: convertWildcard(escapeRegExp(argv.filePattern))
4247
);
4348
const compress = argv.compress;
4449
let singleFileOutput = ""; // Declare singleFileOutput at the top level
@@ -59,9 +64,8 @@ async function readIgnoreFiles(dir) {
5964
ig.add(".gitignore"); // Ignore .gitignore file
6065
ig.add("vendor"); // Ignore vendor directory
6166
ig.add("node_modules"); // Ignore vendor directory
62-
ig.add("*.lock"); // Ignore composer.lock files
63-
ig.add("npm-shrinkwrap.json"); // Ignore npm-shrinkwrap.json file
64-
ig.add("package-lock.json"); // Ignore package-lock.json" file
67+
ig.add("*.lock"); // Ignore *.lock files
68+
ig.add("*.json"); // Ignore *.json files
6569

6670
try {
6771
const files = await fs.readdir(dir);
@@ -108,14 +112,17 @@ async function processDirectory(dir, baseDir = dir, ig) {
108112
"/\n";
109113
await processDirectory(entryPath, baseDir, ig);
110114
} else if (stats.isFile() && entry.match(filePattern)) {
115+
let content = await fs.readFile(entryPath, "utf8");
116+
if (content.trim().length === 0) continue; // Skip empty files
117+
111118
layout +=
112119
"│ ".repeat(
113120
dir.split(path.sep).length - baseDir.split(path.sep).length
114121
) +
115122
"└── " +
116123
entry +
117124
"\n";
118-
let content = await fs.readFile(entryPath, "utf8");
125+
119126
content = content.replace(/\/\/.*|\/\*[\s\S]*?\*\//g, ""); // Remove comments
120127
content = content.replace(/[ \t]+/g, " "); // Normalize spaces and tabs to single space
121128

0 commit comments

Comments
 (0)