@softwareventures/maintain-project
Version:
Automatically create and maintain TypeScript projects with standard settings for Software Ventures Limited
58 lines • 1.96 kB
JavaScript
import { posix } from "path";
import { concat, first, foldFn, mapFn, tail } from "@softwareventures/array";
import { chain } from "@softwareventures/chain";
import { mapNullableFn, mapNullFn } from "@softwareventures/nullable";
import picomatch from "picomatch";
export function ignoreComment(text) {
return { type: "ignore-comment", text };
}
export function ignoreEntry(text) {
return { type: "ignore-entry", text };
}
export function isIgnored(ignore, path) {
return isIgnoredInternal(ignore, posix.normalize(path).split(posix.sep));
}
function isIgnoredInternal(ignore, segments) {
if (first(segments) === "..") {
return "not-ignored";
}
const subdirectoryStatus = chain(first(segments))
.map(mapNullableFn(dir => ignore.subdirectories.get(dir)))
.map(mapNullableFn(ignore => isIgnoredInternal(ignore, tail(segments))))
.map(mapNullFn(() => "not-ignored")).value;
if (subdirectoryStatus !== "not-ignored") {
return subdirectoryStatus;
}
const path = posix.join(...segments);
return chain(ignore.entries)
.map(concat)
.map(mapFn(line => isIgnoredByLine(line, path)))
.map(foldFn((status, lineStatus) => (lineStatus === "not-ignored" ? status : lineStatus), "not-ignored")).value;
}
function isIgnoredByLine(line, path) {
if (line.type !== "ignore-entry") {
return "not-ignored";
}
const text = line.text.trim();
const negate = line.text.startsWith("!");
const matcher = picomatch(text.replace(/^!/u, "").trim(), {
basename: true,
posix: false,
dot: true,
nobrace: true,
noextglob: true,
nonegate: true,
noquantifiers: true,
strictSlashes: true
});
if (!matcher(path)) {
return "not-ignored";
}
else if (negate) {
return "explicitly-not-ignored";
}
else {
return "ignored";
}
}
//# sourceMappingURL=ignore.js.map