fix: move to standard ignore implementation (#50)
This commit is contained in:
parent
7efb9bc8ce
commit
9ce2d6fd0e
4 changed files with 23 additions and 29 deletions
|
|
@ -69,6 +69,7 @@
|
||||||
"@inquirer/prompts": "^6.0.1",
|
"@inquirer/prompts": "^6.0.1",
|
||||||
"commander": "^13.1.0",
|
"commander": "^13.1.0",
|
||||||
"fflate": "^0.8.2",
|
"fflate": "^0.8.2",
|
||||||
|
"ignore": "^7.0.5",
|
||||||
"node-forge": "^1.3.1",
|
"node-forge": "^1.3.1",
|
||||||
"zod": "^3.25.67"
|
"zod": "^3.25.67"
|
||||||
},
|
},
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
import { existsSync, readdirSync, readFileSync, statSync } from "fs";
|
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
|
// Files/patterns to exclude from the package
|
||||||
export const EXCLUDE_PATTERNS = [
|
export const EXCLUDE_PATTERNS = [
|
||||||
|
|
@ -115,35 +116,18 @@ function matchesPattern(
|
||||||
return false;
|
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(
|
export function shouldExclude(
|
||||||
filePath: string,
|
filePath: string,
|
||||||
additionalPatterns: string[] = [],
|
additionalPatterns: string[] = [],
|
||||||
): boolean {
|
): boolean {
|
||||||
const fileName = basename(filePath);
|
return buildIgnoreChecker(additionalPatterns).ignores(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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getAllFiles(
|
export function getAllFiles(
|
||||||
|
|
@ -154,11 +138,13 @@ export function getAllFiles(
|
||||||
): Record<string, Uint8Array> {
|
): Record<string, Uint8Array> {
|
||||||
const files = readdirSync(dirPath);
|
const files = readdirSync(dirPath);
|
||||||
|
|
||||||
|
const ignoreChecker = buildIgnoreChecker(additionalPatterns);
|
||||||
|
|
||||||
for (const file of files) {
|
for (const file of files) {
|
||||||
const filePath = join(dirPath, file);
|
const filePath = join(dirPath, file);
|
||||||
const relativePath = relative(baseDir, filePath);
|
const relativePath = relative(baseDir, filePath);
|
||||||
|
|
||||||
if (shouldExclude(relativePath, additionalPatterns)) {
|
if (ignoreChecker.ignores(relativePath)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -194,11 +180,13 @@ export function getAllFilesWithCount(
|
||||||
): GetAllFilesResult {
|
): GetAllFilesResult {
|
||||||
const files = readdirSync(dirPath);
|
const files = readdirSync(dirPath);
|
||||||
|
|
||||||
|
const ignoreChecker = buildIgnoreChecker(additionalPatterns);
|
||||||
|
|
||||||
for (const file of files) {
|
for (const file of files) {
|
||||||
const filePath = join(dirPath, file);
|
const filePath = join(dirPath, file);
|
||||||
const relativePath = relative(baseDir, filePath);
|
const relativePath = relative(baseDir, filePath);
|
||||||
|
|
||||||
if (shouldExclude(relativePath, additionalPatterns)) {
|
if (ignoreChecker.ignores(relativePath)) {
|
||||||
ignoredCount++;
|
ignoredCount++;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -293,7 +293,7 @@ coverage/`;
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should handle negation with directory patterns", () => {
|
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(
|
expect(shouldExclude("tests/unit/test.js", additionalPatterns)).toBe(
|
||||||
true,
|
true,
|
||||||
|
|
|
||||||
|
|
@ -2357,6 +2357,11 @@ ignore@^5.2.0:
|
||||||
resolved "https://registry.yarnpkg.com/ignore/-/ignore-5.3.2.tgz#3cd40e729f3643fd87cb04e50bf0eb722bc596f5"
|
resolved "https://registry.yarnpkg.com/ignore/-/ignore-5.3.2.tgz#3cd40e729f3643fd87cb04e50bf0eb722bc596f5"
|
||||||
integrity sha512-hsBTNUqQTDwkWtcdYI2i06Y/nUBEsNEDJKjWdigLvegy8kDuJAS8uRlpkkcQpyEXL0Z/pjDy5HBmMjRCJ2gq+g==
|
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:
|
import-fresh@^3.2.1:
|
||||||
version "3.3.1"
|
version "3.3.1"
|
||||||
resolved "https://registry.yarnpkg.com/import-fresh/-/import-fresh-3.3.1.tgz#9cecb56503c0ada1f2741dbbd6546e4b13b57ccf"
|
resolved "https://registry.yarnpkg.com/import-fresh/-/import-fresh-3.3.1.tgz#9cecb56503c0ada1f2741dbbd6546e4b13b57ccf"
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue