fix: move to standard ignore implementation (#50)

This commit is contained in:
Samuel Attard 2025-07-06 16:35:24 -07:00 • committed by GitHub
commit 9ce2d6fd0e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 23 additions and 29 deletions

View file

@ -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"
},

View file

@ -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<string, Uint8Array> {
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;
}

View file

@ -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,

View file

@ -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"