print-project
Advanced tools
Comparing version 1.0.19 to 1.0.20
@@ -35,8 +35,3 @@ #!/usr/bin/env node | ||
const program = new commander_1.Command(); | ||
program | ||
.argument("<startPath>", "Starting directory path") | ||
.option("--ignore <patterns>", "Comma-separated list of patterns to ignore") | ||
.option("--include <patterns>", "Comma-separated list of patterns to include") | ||
.option("--ignore-default", "Disable default ignore patterns") | ||
.parse(process.argv); | ||
program.argument("<startPath>", "Starting directory path").option("--ignore <patterns>", "Comma-separated list of patterns to ignore").option("--include <patterns>", "Comma-separated list of patterns to include").option("--ignore-default", "Disable default ignore patterns").parse(process.argv); | ||
const startPath = program.args[0] && path.resolve(program.args[0]); | ||
@@ -50,3 +45,3 @@ const options = program.opts(); | ||
if (!options.ignoreDefault) { | ||
ignorePatterns = [...constants_1.defaultIgnorePatterns, ...ignorePatterns]; | ||
ignorePatterns = [...ignorePatterns, ...constants_1.defaultIgnorePatterns]; | ||
} | ||
@@ -57,26 +52,16 @@ // Add user's ignore patterns | ||
return patterns.some(pattern => { | ||
const regexPattern = pattern | ||
.split("*") | ||
.map(s => s.replace(/[|\\{}()[\]^$+?.]/g, "\\$&")) | ||
.join(".*"); | ||
// Handle paths that might contain slashes | ||
const normalizedPath = filePath.replace(/\\/g, "/"); | ||
const regexPattern = pattern.split("*").map(s => s.replace(/[|\\{}()[\]^$+?.]/g, "\\$&")).join(".*"); | ||
const regex = new RegExp(regexPattern, "i"); | ||
return regex.test(filePath); | ||
return regex.test(normalizedPath); | ||
}); | ||
} | ||
function shouldIncludeFile(filePath, ignorePatterns, includePatterns) { | ||
// First check if the file matches any include patterns | ||
const isIncluded = includePatterns.length > 0 ? matchesPattern(filePath, includePatterns) : true; | ||
// If the file doesn't match include patterns (when they exist), exclude it | ||
if (!isIncluded) { | ||
return false; | ||
// If include patterns exist, check if path contains any of them | ||
if (includePatterns.length > 0) { | ||
return includePatterns.some(pattern => filePath.toLowerCase().includes(pattern.toLowerCase())); | ||
} | ||
// If the file matches include patterns (or there are no include patterns) | ||
// AND doesn't match ignore patterns, include it | ||
const isIgnored = matchesPattern(filePath, ignorePatterns); | ||
// Special case: if the file matches both include and ignore patterns, | ||
// prioritize the include pattern | ||
if (includePatterns.length > 0 && isIncluded) { | ||
return true; | ||
} | ||
return !isIgnored; | ||
// If no include patterns, check against ignore patterns | ||
return !matchesPattern(filePath, ignorePatterns); | ||
} | ||
@@ -89,7 +74,13 @@ function readDirectory(dirPath, ignorePatterns, includePatterns, treeStructure = {}, currentPath = "") { | ||
if (dirent.isDirectory()) { | ||
// For directories, check if they should be included based on patterns | ||
const shouldIncludeDir = shouldIncludeFile(fullPath, ignorePatterns, includePatterns); | ||
if (shouldIncludeDir) { | ||
// Always process directory if it contains include pattern | ||
const shouldProcessDir = includePatterns.length > 0 | ||
? includePatterns.some(pattern => fullPath.toLowerCase().includes(pattern.toLowerCase())) | ||
: !matchesPattern(fullPath, ignorePatterns); | ||
if (shouldProcessDir || includePatterns.length > 0) { // Always traverse if we have include patterns | ||
treeStructure[fullPath] = {}; | ||
readDirectory(path.join(dirPath, dirent.name), ignorePatterns, includePatterns, treeStructure[fullPath], fullPath); | ||
// Remove empty directories from tree structure | ||
if (Object.keys(treeStructure[fullPath]).length === 0) { | ||
delete treeStructure[fullPath]; | ||
} | ||
} | ||
@@ -99,7 +90,12 @@ } | ||
if (shouldIncludeFile(fullPath, ignorePatterns, includePatterns)) { | ||
treeStructure[fullPath] = {}; | ||
const content = fs.readFileSync(path.join(dirPath, dirent.name), "utf8"); | ||
if (content.length > 0) { | ||
projectPrint += `${fullPath}:\n${content}\n\n`; | ||
try { | ||
const content = fs.readFileSync(path.join(dirPath, dirent.name), "utf8"); | ||
if (content.length > 0) { | ||
treeStructure[fullPath] = {}; | ||
projectPrint += `${fullPath}:\n${content}\n\n`; | ||
} | ||
} | ||
catch (error) { | ||
console.error(`Error reading file ${fullPath}:`, error.message); | ||
} | ||
} | ||
@@ -106,0 +102,0 @@ } |
{ | ||
"name": "print-project", | ||
"version": "1.0.19", | ||
"version": "1.0.20", | ||
"description": "A simple CLI tool to print the project tree structure and file contents", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -12,8 +12,3 @@ #!/usr/bin/env node | ||
const program = new Command(); | ||
program | ||
.argument("<startPath>", "Starting directory path") | ||
.option("--ignore <patterns>", "Comma-separated list of patterns to ignore") | ||
.option("--include <patterns>", "Comma-separated list of patterns to include") | ||
.option("--ignore-default", "Disable default ignore patterns") | ||
.parse(process.argv); | ||
program.argument("<startPath>", "Starting directory path").option("--ignore <patterns>", "Comma-separated list of patterns to ignore").option("--include <patterns>", "Comma-separated list of patterns to include").option("--ignore-default", "Disable default ignore patterns").parse(process.argv); | ||
@@ -30,3 +25,3 @@ const startPath: string | undefined = program.args[0] && path.resolve(program.args[0]); | ||
if (!options.ignoreDefault) { | ||
ignorePatterns = [...defaultIgnorePatterns, ...ignorePatterns]; | ||
ignorePatterns = [...ignorePatterns, ...defaultIgnorePatterns]; | ||
} | ||
@@ -39,9 +34,8 @@ | ||
return patterns.some(pattern => { | ||
const regexPattern = pattern | ||
.split("*") | ||
.map(s => s.replace(/[|\\{}()[\]^$+?.]/g, "\\$&")) | ||
.join(".*"); | ||
// Handle paths that might contain slashes | ||
const normalizedPath = filePath.replace(/\\/g, "/"); | ||
const regexPattern = pattern.split("*").map(s => s.replace(/[|\\{}()[\]^$+?.]/g, "\\$&")).join(".*"); | ||
const regex = new RegExp(regexPattern, "i"); | ||
return regex.test(filePath); | ||
return regex.test(normalizedPath); | ||
}); | ||
@@ -51,21 +45,9 @@ } | ||
function shouldIncludeFile(filePath: string, ignorePatterns: string[], includePatterns: string[]): boolean { | ||
// First check if the file matches any include patterns | ||
const isIncluded = includePatterns.length > 0 ? matchesPattern(filePath, includePatterns) : true; | ||
// If the file doesn't match include patterns (when they exist), exclude it | ||
if (!isIncluded) { | ||
return false; | ||
// If include patterns exist, check if path contains any of them | ||
if (includePatterns.length > 0) { | ||
return includePatterns.some(pattern => filePath.toLowerCase().includes(pattern.toLowerCase())); | ||
} | ||
// If the file matches include patterns (or there are no include patterns) | ||
// AND doesn't match ignore patterns, include it | ||
const isIgnored = matchesPattern(filePath, ignorePatterns); | ||
// Special case: if the file matches both include and ignore patterns, | ||
// prioritize the include pattern | ||
if (includePatterns.length > 0 && isIncluded) { | ||
return true; | ||
} | ||
return !isIgnored; | ||
// If no include patterns, check against ignore patterns | ||
return !matchesPattern(filePath, ignorePatterns); | ||
} | ||
@@ -81,6 +63,8 @@ | ||
if (dirent.isDirectory()) { | ||
// For directories, check if they should be included based on patterns | ||
const shouldIncludeDir = shouldIncludeFile(fullPath, ignorePatterns, includePatterns); | ||
// Always process directory if it contains include pattern | ||
const shouldProcessDir = includePatterns.length > 0 | ||
? includePatterns.some(pattern => fullPath.toLowerCase().includes(pattern.toLowerCase())) | ||
: !matchesPattern(fullPath, ignorePatterns); | ||
if (shouldIncludeDir) { | ||
if (shouldProcessDir || includePatterns.length > 0) { // Always traverse if we have include patterns | ||
treeStructure[fullPath] = {}; | ||
@@ -92,11 +76,20 @@ readDirectory( | ||
treeStructure[fullPath], | ||
fullPath | ||
fullPath, | ||
); | ||
// Remove empty directories from tree structure | ||
if (Object.keys(treeStructure[fullPath]).length === 0) { | ||
delete treeStructure[fullPath]; | ||
} | ||
} | ||
} else if (dirent.isFile()) { | ||
if (shouldIncludeFile(fullPath, ignorePatterns, includePatterns)) { | ||
treeStructure[fullPath] = {}; | ||
const content = fs.readFileSync(path.join(dirPath, dirent.name), "utf8"); | ||
if (content.length > 0) { | ||
projectPrint += `${fullPath}:\n${content}\n\n`; | ||
try { | ||
const content = fs.readFileSync(path.join(dirPath, dirent.name), "utf8"); | ||
if (content.length > 0) { | ||
treeStructure[fullPath] = {}; | ||
projectPrint += `${fullPath}:\n${content}\n\n`; | ||
} | ||
} catch (error: any) { | ||
console.error(`Error reading file ${fullPath}:`, error.message); | ||
} | ||
@@ -139,2 +132,2 @@ } | ||
main(); | ||
main(); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
23384
472