mirror of
https://github.com/vrtmrz/obsidian-livesync.git
synced 2026-05-14 19:41:16 +00:00
cli: add configurable ignore rules and deployment artifacts
IgnoreRules (src/apps/cli/serviceModules/IgnoreRules.ts): - Reads .livesync/ignore for user-defined glob patterns - Applies gitignore matchBase semantics: patterns without / get **/ prefix, patterns ending with / get ** appended for directory contents - Supports `import: .gitignore` directive to merge gitignore patterns - Rejects negation patterns with a warning (not fully supportable) - Integrated into both daemon and mirror commands via isTargetFile handler Wiring: - IgnoreRules loaded before LiveSyncBaseCore construction so beginWatch() receives rules when it fires during onLoad/onFirstInitialise - Passed through initialiseServiceModulesCLI -> StorageEventManagerCLI -> CLIStorageEventManagerAdapter -> CLIWatchAdapter Deployment: - src/apps/cli/deploy/livesync-cli.service - systemd unit template - src/apps/cli/deploy/install.sh - user/system install script Testing: - src/apps/cli/test/test-daemon-linux.sh - e2e tests for ignore rules - src/apps/cli/serviceModules/IgnoreRules.unit.spec.ts - 15 unit tests - src/apps/cli/commands/daemonCommand.unit.spec.ts - 7 unit tests
This commit is contained in:
@@ -9,6 +9,7 @@ import { ServiceFileAccessCLI } from "./ServiceFileAccessImpl";
|
||||
import { ServiceDatabaseFileAccessCLI } from "./DatabaseFileAccess";
|
||||
import { StorageEventManagerCLI } from "../managers/StorageEventManagerCLI";
|
||||
import type { ServiceModules } from "@lib/interfaces/ServiceModule";
|
||||
import type { IgnoreRules } from "./IgnoreRules";
|
||||
|
||||
/**
|
||||
* Initialize service modules for CLI version
|
||||
@@ -23,6 +24,7 @@ export function initialiseServiceModulesCLI(
|
||||
basePath: string,
|
||||
core: LiveSyncBaseCore<ServiceContext, any>,
|
||||
services: InjectableServiceHub<ServiceContext>,
|
||||
ignoreRules?: IgnoreRules,
|
||||
watchEnabled: boolean = false,
|
||||
): ServiceModules {
|
||||
const storageAccessManager = new StorageAccessManager();
|
||||
@@ -43,7 +45,7 @@ export function initialiseServiceModulesCLI(
|
||||
vaultService: services.vault,
|
||||
storageAccessManager: storageAccessManager,
|
||||
APIService: services.API,
|
||||
}, false);
|
||||
}, ignoreRules, watchEnabled);
|
||||
|
||||
// Close the file watcher during graceful shutdown so the process can exit cleanly.
|
||||
services.appLifecycle.onUnload.addHandler(async () => {
|
||||
|
||||
129
src/apps/cli/serviceModules/IgnoreRules.ts
Normal file
129
src/apps/cli/serviceModules/IgnoreRules.ts
Normal file
@@ -0,0 +1,129 @@
|
||||
import * as fs from "fs/promises";
|
||||
import * as path from "path";
|
||||
|
||||
import { minimatch } from "minimatch";
|
||||
|
||||
/**
|
||||
* Loads and evaluates ignore rules from `.livesync/ignore` inside the vault.
|
||||
*
|
||||
* File format:
|
||||
* - Lines starting with `#` are comments.
|
||||
* - Blank lines are ignored.
|
||||
* - `import: .gitignore` (exactly) — merges patterns from the vault's `.gitignore`.
|
||||
* - All other lines are minimatch glob patterns relative to the vault root.
|
||||
*
|
||||
* Negation patterns (lines starting with `!`) are not supported. Loading a
|
||||
* ruleset containing them throws an error — use separate include/exclude files
|
||||
* instead.
|
||||
*
|
||||
* Missing files (`.livesync/ignore` or `.gitignore`) are silently skipped.
|
||||
*/
|
||||
export class IgnoreRules {
|
||||
private patterns: string[] = [];
|
||||
|
||||
constructor(private vaultPath: string) {}
|
||||
|
||||
/**
|
||||
* Reads `.livesync/ignore` (and optionally `.gitignore`) and populates the
|
||||
* pattern list. Safe to call multiple times — each call replaces the
|
||||
* previous state. Does not throw if files are absent.
|
||||
*
|
||||
* @throws if any pattern line begins with `!` (negation is unsupported).
|
||||
*/
|
||||
async load(): Promise<void> {
|
||||
this.patterns = [];
|
||||
const ignorePath = path.join(this.vaultPath, ".livesync", "ignore");
|
||||
let rawLines: string[];
|
||||
try {
|
||||
const content = await fs.readFile(ignorePath, "utf-8");
|
||||
rawLines = content.split(/\r?\n/);
|
||||
} catch {
|
||||
// File absent or unreadable — treat as empty ruleset.
|
||||
return;
|
||||
}
|
||||
|
||||
for (const line of rawLines) {
|
||||
const trimmed = line.trim();
|
||||
if (!trimmed || trimmed.startsWith("#")) {
|
||||
continue;
|
||||
}
|
||||
// NOTE: Only the exact string "import: .gitignore" is recognised.
|
||||
// Any future generalisation of this directive must validate that
|
||||
// the resolved path stays within the vault directory.
|
||||
if (trimmed === "import: .gitignore") {
|
||||
await this._importGitignore();
|
||||
continue;
|
||||
}
|
||||
if (trimmed.startsWith("import:")) {
|
||||
console.error(`[IgnoreRules] Warning: unrecognised directive '${trimmed}' — only 'import: .gitignore' is supported`);
|
||||
continue;
|
||||
}
|
||||
this._addPattern(trimmed);
|
||||
}
|
||||
if (this.patterns.length > 0) {
|
||||
console.error(`[IgnoreRules] Loaded ${this.patterns.length} ignore patterns`);
|
||||
}
|
||||
}
|
||||
|
||||
// Normalises a single gitignore-style pattern:
|
||||
// - Patterns ending with `/` (directory patterns like `build/`) are
|
||||
// converted to `build/**` so they match all files inside that directory.
|
||||
// - Patterns without a `/` are prefixed with `**/` to give them matchBase
|
||||
// semantics (e.g. `*.tmp` → `**/*.tmp`), matching the basename in any
|
||||
// subdirectory as gitignore does.
|
||||
// - Patterns that already contain a `/` (but don't end with one) are
|
||||
// path-specific and used as-is.
|
||||
private _normalisePattern(pattern: string): string {
|
||||
if (pattern.endsWith("/")) {
|
||||
return "**/" + pattern + "**";
|
||||
} else if (!pattern.includes("/")) {
|
||||
return "**/" + pattern;
|
||||
}
|
||||
return pattern;
|
||||
}
|
||||
|
||||
private async _importGitignore(): Promise<void> {
|
||||
const gitignorePath = path.join(this.vaultPath, ".gitignore");
|
||||
let content: string;
|
||||
try {
|
||||
content = await fs.readFile(gitignorePath, "utf-8");
|
||||
} catch {
|
||||
return;
|
||||
}
|
||||
this._parseLines(content);
|
||||
}
|
||||
|
||||
private _parseLines(content: string): void {
|
||||
for (const line of content.split(/\r?\n/)) {
|
||||
const trimmed = line.trim();
|
||||
if (!trimmed || trimmed.startsWith("#")) continue;
|
||||
this._addPattern(trimmed);
|
||||
}
|
||||
}
|
||||
|
||||
private _addPattern(raw: string): void {
|
||||
if (raw.startsWith("!")) {
|
||||
throw new Error(
|
||||
`[IgnoreRules] Negation pattern '${raw}' is not supported. ` +
|
||||
`Remove it from .livesync/ignore or use a separate include/exclude file.`
|
||||
);
|
||||
}
|
||||
this.patterns.push(this._normalisePattern(raw));
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns `true` if the given vault-relative path matches any loaded
|
||||
* ignore pattern.
|
||||
*
|
||||
* @param relativePath - Path relative to the vault root, using forward
|
||||
* slashes or the OS separator.
|
||||
*/
|
||||
shouldIgnore(relativePath: string): boolean {
|
||||
if (this.patterns.length === 0) {
|
||||
return false;
|
||||
}
|
||||
// Normalise to forward slashes for minimatch.
|
||||
const normalised = relativePath.replace(/\\/g, "/");
|
||||
return this.patterns.some((p) => minimatch(normalised, p, { dot: true }));
|
||||
}
|
||||
}
|
||||
172
src/apps/cli/serviceModules/IgnoreRules.unit.spec.ts
Normal file
172
src/apps/cli/serviceModules/IgnoreRules.unit.spec.ts
Normal file
@@ -0,0 +1,172 @@
|
||||
import * as fs from "node:fs/promises";
|
||||
import * as os from "node:os";
|
||||
import * as path from "node:path";
|
||||
import { afterEach, beforeEach, describe, expect, it } from "vitest";
|
||||
import { IgnoreRules } from "./IgnoreRules";
|
||||
|
||||
describe("IgnoreRules", () => {
|
||||
const tempDirs: string[] = [];
|
||||
|
||||
async function createVault(): Promise<string> {
|
||||
const tempDir = await fs.mkdtemp(path.join(os.tmpdir(), "livesync-ignorerules-"));
|
||||
tempDirs.push(tempDir);
|
||||
return tempDir;
|
||||
}
|
||||
|
||||
async function writeIgnoreFile(vaultPath: string, content: string): Promise<void> {
|
||||
const ignoreDir = path.join(vaultPath, ".livesync");
|
||||
await fs.mkdir(ignoreDir, { recursive: true });
|
||||
await fs.writeFile(path.join(ignoreDir, "ignore"), content, "utf-8");
|
||||
}
|
||||
|
||||
afterEach(async () => {
|
||||
await Promise.all(tempDirs.splice(0).map((dir) => fs.rm(dir, { recursive: true, force: true })));
|
||||
});
|
||||
|
||||
describe("pattern normalisation", () => {
|
||||
it("adds **/ prefix to basename patterns (no slash)", async () => {
|
||||
const vaultPath = await createVault();
|
||||
await writeIgnoreFile(vaultPath, "*.tmp\n");
|
||||
const rules = new IgnoreRules(vaultPath);
|
||||
await rules.load();
|
||||
expect(rules.shouldIgnore("notes/scratch.tmp")).toBe(true);
|
||||
expect(rules.shouldIgnore("scratch.tmp")).toBe(true);
|
||||
expect(rules.shouldIgnore("deep/nested/file.tmp")).toBe(true);
|
||||
});
|
||||
|
||||
it("appends ** to directory patterns ending with / and prepends **/", async () => {
|
||||
const vaultPath = await createVault();
|
||||
await writeIgnoreFile(vaultPath, "build/\n");
|
||||
const rules = new IgnoreRules(vaultPath);
|
||||
await rules.load();
|
||||
expect(rules.shouldIgnore("build/output.js")).toBe(true);
|
||||
expect(rules.shouldIgnore("build/nested/file.js")).toBe(true);
|
||||
expect(rules.shouldIgnore("subproject/build/output.js")).toBe(true);
|
||||
});
|
||||
|
||||
it("leaves patterns containing / as-is", async () => {
|
||||
const vaultPath = await createVault();
|
||||
await writeIgnoreFile(vaultPath, "docs/private.md\n");
|
||||
const rules = new IgnoreRules(vaultPath);
|
||||
await rules.load();
|
||||
expect(rules.shouldIgnore("docs/private.md")).toBe(true);
|
||||
expect(rules.shouldIgnore("other/docs/private.md")).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe("shouldIgnore", () => {
|
||||
it("matches **/*.tmp against notes/scratch.tmp", async () => {
|
||||
const vaultPath = await createVault();
|
||||
await writeIgnoreFile(vaultPath, "*.tmp\n");
|
||||
const rules = new IgnoreRules(vaultPath);
|
||||
await rules.load();
|
||||
expect(rules.shouldIgnore("notes/scratch.tmp")).toBe(true);
|
||||
});
|
||||
|
||||
it("does not match notes/readme.md against **/*.tmp", async () => {
|
||||
const vaultPath = await createVault();
|
||||
await writeIgnoreFile(vaultPath, "*.tmp\n");
|
||||
const rules = new IgnoreRules(vaultPath);
|
||||
await rules.load();
|
||||
expect(rules.shouldIgnore("notes/readme.md")).toBe(false);
|
||||
});
|
||||
|
||||
it("returns false when no patterns are loaded", async () => {
|
||||
const vaultPath = await createVault();
|
||||
const rules = new IgnoreRules(vaultPath);
|
||||
// No load() call — patterns are empty
|
||||
expect(rules.shouldIgnore("anything.md")).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe("negation patterns", () => {
|
||||
it("throws when a negation pattern is encountered", async () => {
|
||||
const vaultPath = await createVault();
|
||||
await writeIgnoreFile(vaultPath, "*.tmp\n!important.tmp\n");
|
||||
const rules = new IgnoreRules(vaultPath);
|
||||
await expect(rules.load()).rejects.toThrow(/Negation pattern/);
|
||||
});
|
||||
|
||||
it("throws when a .gitignore imported via directive contains negation", async () => {
|
||||
const vaultPath = await createVault();
|
||||
await writeIgnoreFile(vaultPath, "import: .gitignore\n");
|
||||
await fs.writeFile(path.join(vaultPath, ".gitignore"), "*.log\n!keep.log\n", "utf-8");
|
||||
const rules = new IgnoreRules(vaultPath);
|
||||
await expect(rules.load()).rejects.toThrow(/Negation pattern/);
|
||||
});
|
||||
});
|
||||
|
||||
describe("unrecognised import: directives", () => {
|
||||
it("warns and skips unrecognised import: forms (does not add as literal pattern)", async () => {
|
||||
const vaultPath = await createVault();
|
||||
// Typo: "import:.gitignore" instead of "import: .gitignore"
|
||||
await writeIgnoreFile(vaultPath, "*.tmp\nimport:.gitignore\n");
|
||||
const rules = new IgnoreRules(vaultPath);
|
||||
await rules.load();
|
||||
// *.tmp still loaded; import:.gitignore is skipped (not treated as a literal pattern)
|
||||
expect(rules.shouldIgnore("scratch.tmp")).toBe(true);
|
||||
expect(rules.shouldIgnore("import:.gitignore")).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe("load() with missing file", () => {
|
||||
it("returns without error when .livesync/ignore is absent", async () => {
|
||||
const vaultPath = await createVault();
|
||||
// No ignore file created
|
||||
const rules = new IgnoreRules(vaultPath);
|
||||
await expect(rules.load()).resolves.toBeUndefined();
|
||||
expect(rules.shouldIgnore("anything.md")).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe("load() with comments and blank lines", () => {
|
||||
it("skips # comment lines and blank lines", async () => {
|
||||
const vaultPath = await createVault();
|
||||
await writeIgnoreFile(
|
||||
vaultPath,
|
||||
"# This is a comment\n\n \n*.tmp\n# another comment\nbuild/\n"
|
||||
);
|
||||
const rules = new IgnoreRules(vaultPath);
|
||||
await rules.load();
|
||||
expect(rules.shouldIgnore("scratch.tmp")).toBe(true);
|
||||
expect(rules.shouldIgnore("build/output.js")).toBe(true);
|
||||
expect(rules.shouldIgnore("readme.md")).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe("import: .gitignore directive", () => {
|
||||
it("reads and normalises patterns from .gitignore", async () => {
|
||||
const vaultPath = await createVault();
|
||||
await writeIgnoreFile(vaultPath, "import: .gitignore\n");
|
||||
await fs.writeFile(path.join(vaultPath, ".gitignore"), "*.log\nnode_modules/\n", "utf-8");
|
||||
const rules = new IgnoreRules(vaultPath);
|
||||
await rules.load();
|
||||
expect(rules.shouldIgnore("app.log")).toBe(true);
|
||||
expect(rules.shouldIgnore("node_modules/package.json")).toBe(true);
|
||||
expect(rules.shouldIgnore("src/node_modules/package.json")).toBe(true);
|
||||
expect(rules.shouldIgnore("src/index.ts")).toBe(false);
|
||||
});
|
||||
|
||||
it("merges .gitignore patterns with other patterns", async () => {
|
||||
const vaultPath = await createVault();
|
||||
await writeIgnoreFile(vaultPath, "*.tmp\nimport: .gitignore\n");
|
||||
await fs.writeFile(path.join(vaultPath, ".gitignore"), "*.log\n", "utf-8");
|
||||
const rules = new IgnoreRules(vaultPath);
|
||||
await rules.load();
|
||||
expect(rules.shouldIgnore("scratch.tmp")).toBe(true);
|
||||
expect(rules.shouldIgnore("error.log")).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe("import: .gitignore with missing .gitignore", () => {
|
||||
it("does not throw when .gitignore is absent", async () => {
|
||||
const vaultPath = await createVault();
|
||||
await writeIgnoreFile(vaultPath, "*.tmp\nimport: .gitignore\n");
|
||||
// No .gitignore created
|
||||
const rules = new IgnoreRules(vaultPath);
|
||||
await expect(rules.load()).resolves.toBeUndefined();
|
||||
// The *.tmp pattern from the ignore file still works
|
||||
expect(rules.shouldIgnore("scratch.tmp")).toBe(true);
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user