From 9ce2d6fd0ee79c6e402d9f6963e02f60886582c6 Mon Sep 17 00:00:00 2001 From: Samuel Attard Date: Sun, 6 Jul 2025 16:35:24 -0700 Subject: [PATCH] fix: move to standard ignore implementation (#50) --- package.json | 1 + src/node/files.ts | 44 +++++++++++++++--------------------------- test/dxtignore.test.ts | 2 +- yarn.lock | 5 +++++ 4 files changed, 23 insertions(+), 29 deletions(-) diff --git a/package.json b/package.json index d87e6c2..8417f07 100644 --- a/package.json +++ b/package.json @@ -69,6 +69,7 @@ "@inquirer/prompts": "^6.0.1", "commander": "^13.1.0", "fflate": "^0.8.2", + "ignore": "^7.0.5", "node-forge": "^1.3.1", "zod": "^3.25.67" }, diff --git a/src/node/files.ts b/src/node/files.ts index ce98c89..eeb0e20 100644 --- a/src/node/files.ts +++ b/src/node/files.ts @@ -1,5 +1,6 @@ import { existsSync, readdirSync, readFileSync, statSync } from "fs"; -import { basename, join, relative, sep } from "path"; +import ignore from "ignore"; +import { join, relative, sep } from "path"; // Files/patterns to exclude from the package export const EXCLUDE_PATTERNS = [ @@ -115,35 +116,18 @@ function matchesPattern( return false; } +function buildIgnoreChecker(additionalPatterns: string[]) { + return ignore().add(EXCLUDE_PATTERNS).add(additionalPatterns); +} + +/** + * Used for testing, calls the same methods as the other ignore checks + */ export function shouldExclude( filePath: string, additionalPatterns: string[] = [], ): boolean { - const fileName = basename(filePath); - const allPatterns = [...EXCLUDE_PATTERNS, ...additionalPatterns]; - const negationPatterns = allPatterns.filter((p) => p.startsWith("!")); - const exclusionPatterns = allPatterns.filter((p) => !p.startsWith("!")); - - let shouldExcludeFile = false; - for (const pattern of exclusionPatterns) { - if (matchesPattern(pattern, filePath, fileName)) { - shouldExcludeFile = true; - break; - } - } - - // If excluded, check if any negation pattern un-excludes it - if (shouldExcludeFile) { - for (const pattern of negationPatterns) { - const negatedPattern = pattern.slice(1); - if (matchesPattern(negatedPattern, filePath, fileName)) { - // File should not be excluded (negation pattern matched) - return false; - } - } - } - - return shouldExcludeFile; + return buildIgnoreChecker(additionalPatterns).ignores(filePath); } export function getAllFiles( @@ -154,11 +138,13 @@ export function getAllFiles( ): Record { const files = readdirSync(dirPath); + const ignoreChecker = buildIgnoreChecker(additionalPatterns); + for (const file of files) { const filePath = join(dirPath, file); const relativePath = relative(baseDir, filePath); - if (shouldExclude(relativePath, additionalPatterns)) { + if (ignoreChecker.ignores(relativePath)) { continue; } @@ -194,11 +180,13 @@ export function getAllFilesWithCount( ): GetAllFilesResult { const files = readdirSync(dirPath); + const ignoreChecker = buildIgnoreChecker(additionalPatterns); + for (const file of files) { const filePath = join(dirPath, file); const relativePath = relative(baseDir, filePath); - if (shouldExclude(relativePath, additionalPatterns)) { + if (ignoreChecker.ignores(relativePath)) { ignoredCount++; continue; } diff --git a/test/dxtignore.test.ts b/test/dxtignore.test.ts index 762aaa3..6e33a50 100644 --- a/test/dxtignore.test.ts +++ b/test/dxtignore.test.ts @@ -293,7 +293,7 @@ coverage/`; }); it("should handle negation with directory patterns", () => { - const additionalPatterns = ["tests/", "!tests/critical/"]; + const additionalPatterns = ["tests/*", "!tests/critical/"]; expect(shouldExclude("tests/unit/test.js", additionalPatterns)).toBe( true, diff --git a/yarn.lock b/yarn.lock index 67d7ee7..c13b924 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2357,6 +2357,11 @@ ignore@^5.2.0: resolved "https://registry.yarnpkg.com/ignore/-/ignore-5.3.2.tgz#3cd40e729f3643fd87cb04e50bf0eb722bc596f5" integrity sha512-hsBTNUqQTDwkWtcdYI2i06Y/nUBEsNEDJKjWdigLvegy8kDuJAS8uRlpkkcQpyEXL0Z/pjDy5HBmMjRCJ2gq+g== +ignore@^7.0.5: + version "7.0.5" + resolved "https://artifactory.infra.ant.dev:443/artifactory/api/npm/npm-all/ignore/-/ignore-7.0.5.tgz#4cb5f6cd7d4c7ab0365738c7aea888baa6d7efd9" + integrity sha512-Hs59xBNfUIunMFgWAbGX5cq6893IbWg4KnrjbYwX3tx0ztorVgTDA6B2sxf8ejHJ4wz8BqGUMYlnzNBer5NvGg== + import-fresh@^3.2.1: version "3.3.1" resolved "https://registry.yarnpkg.com/import-fresh/-/import-fresh-3.3.1.tgz#9cecb56503c0ada1f2741dbbd6546e4b13b57ccf"