tiddlywiki-plugin-dev
Version:
[](https://github.com/tiddly-gittly)
49 lines (48 loc) • 1.55 kB
JavaScript
import fs from "fs";
import path from "path";
import ignore from "ignore";
const wikiLoopProneBasenameRegExp = /^\$__(?:StoryList|layout|palette)(?:_\d+)?\.tid$/;
const buildWatchIgnored = ($tw, srcDir, wikiDir) => {
const twExcludeSource = $tw.boot.excludeRegExp?.source.split("|").filter((part) => part !== "^.*\\.meta$").join("|");
const srcExcludeRegExp = twExcludeSource ? new RegExp(twExcludeSource) : void 0;
const wikiRoot = path.resolve(wikiDir);
const ig = ignore();
for (const gip of [
path.resolve(".", ".gitignore"),
path.resolve(wikiDir, ".gitignore")
]) {
if (fs.existsSync(gip)) {
ig.add(fs.readFileSync(gip, "utf-8"));
}
}
const isIgnoredByGitignore = (filePath) => {
try {
const relative = path.relative(wikiRoot, filePath).replace(/\\/g, "/");
if (relative.startsWith("..")) {
return false;
}
return ig.ignores(relative);
} catch {
return false;
}
};
const srcRoot = path.resolve(srcDir);
return (filePath) => {
const basename = path.basename(filePath);
const resolvedPath = path.resolve(filePath);
const isInSrc = resolvedPath.startsWith(srcRoot + path.sep) || resolvedPath === srcRoot;
if (isInSrc) {
return srcExcludeRegExp ? srcExcludeRegExp.test(basename) : false;
}
if ($tw.boot.excludeRegExp?.test(basename)) {
return true;
}
if (wikiLoopProneBasenameRegExp.test(basename)) {
return true;
}
return isIgnoredByGitignore(filePath);
};
};
export {
buildWatchIgnored
};