diff --git a/frontend/local-client-cli/src/args.ts b/frontend/local-client-cli/src/args.ts index 442c4817..34d839b1 100644 --- a/frontend/local-client-cli/src/args.ts +++ b/frontend/local-client-cli/src/args.ts @@ -2,7 +2,8 @@ import { Command, Option } from "commander"; import packageJson from "../package.json"; import { LogLevel } from "sync-client"; -type LineEndingMode = "auto" | "lf" | "crlf"; +export const LINE_ENDING_MODES = ["auto", "lf", "crlf"] as const; +export type LineEndingMode = (typeof LINE_ENDING_MODES)[number]; interface CliArgs { remoteUri: string; @@ -21,6 +22,35 @@ interface CliArgs { const VALID_PROTOCOLS = ["http://", "https://", "ws://", "wss://"]; +const REQUIRED_OPTIONS = { + localPath: { + flags: "-l, --local-path ", + env: "VAULTLINK_LOCAL_PATH" + }, + remoteUri: { + flags: "-r, --remote-uri ", + env: "VAULTLINK_REMOTE_URI" + }, + token: { flags: "-t, --token ", env: "VAULTLINK_TOKEN" }, + vaultName: { + flags: "-v, --vault-name ", + env: "VAULTLINK_VAULT_NAME" + } +} as const; + +function requireOption( + value: T | undefined, + name: keyof typeof REQUIRED_OPTIONS +): T { + if (value === undefined) { + const { flags, env } = REQUIRED_OPTIONS[name]; + throw new Error( + `required option '${flags}' not specified (or set ${env})` + ); + } + return value; +} + export function parseArgs(argv: string[]): CliArgs { const program = new Command(); @@ -32,23 +62,25 @@ export function parseArgs(argv: string[]): CliArgs { .version(packageJson.version) .addOption( new Option( - "-l, --local-path ", + REQUIRED_OPTIONS.localPath.flags, "Local directory path to sync" - ).env("VAULTLINK_LOCAL_PATH") + ).env(REQUIRED_OPTIONS.localPath.env) ) .addOption( - new Option("-r, --remote-uri ", "Remote server URI").env( - "VAULTLINK_REMOTE_URI" - ) + new Option( + REQUIRED_OPTIONS.remoteUri.flags, + "Remote server URI" + ).env(REQUIRED_OPTIONS.remoteUri.env) ) .addOption( - new Option("-t, --token ", "Authentication token").env( - "VAULTLINK_TOKEN" - ) + new Option( + REQUIRED_OPTIONS.token.flags, + "Authentication token" + ).env(REQUIRED_OPTIONS.token.env) ) .addOption( - new Option("-v, --vault-name ", "Vault name").env( - "VAULTLINK_VAULT_NAME" + new Option(REQUIRED_OPTIONS.vaultName.flags, "Vault name").env( + REQUIRED_OPTIONS.vaultName.env ) ) .addOption( @@ -105,7 +137,7 @@ export function parseArgs(argv: string[]): CliArgs { "[OPTIONAL] Line ending style: auto (platform default), lf, crlf" ) .default("auto") - .choices(["auto", "lf", "crlf"]) + .choices([...LINE_ENDING_MODES]) .env("VAULTLINK_LINE_ENDINGS") ) .addHelpText( @@ -144,22 +176,6 @@ Environment variables: const lineEndingsStr = (opts.lineEndings as string | undefined) ?? "auto"; /* eslint-enable @typescript-eslint/no-unsafe-type-assertion */ - const requireOption = (value: T | undefined, name: string): T => { - if (value === undefined) { - const option = program.options.find( - (o) => o.attributeName() === name - ); - const envHint = - option?.envVar !== undefined - ? ` (or set ${option.envVar})` - : ""; - throw new Error( - `required option '${option?.flags ?? name}' not specified${envHint}` - ); - } - return value; - }; - const requiredLocalPath = requireOption(localPath, "localPath"); const requiredRemoteUri = requireOption(remoteUri, "remoteUri"); const requiredToken = requireOption(token, "token"); @@ -187,13 +203,11 @@ Environment variables: } const logLevel = logLevelUpper; - const validLineEndings: readonly string[] = ["auto", "lf", "crlf"]; - const isLineEndingMode = (value: string): value is LineEndingMode => { - return validLineEndings.includes(value); - }; + const isLineEndingMode = (value: string): value is LineEndingMode => + (LINE_ENDING_MODES as readonly string[]).includes(value); if (!isLineEndingMode(lineEndingsStr)) { throw new Error( - `Invalid line endings mode '${lineEndingsStr}'. Valid values are: ${validLineEndings.join(", ")}` + `Invalid line endings mode '${lineEndingsStr}'. Valid values are: ${LINE_ENDING_MODES.join(", ")}` ); } const lineEndings = lineEndingsStr; diff --git a/frontend/local-client-cli/src/cli.ts b/frontend/local-client-cli/src/cli.ts index e06fda47..31c81d5c 100644 --- a/frontend/local-client-cli/src/cli.ts +++ b/frontend/local-client-cli/src/cli.ts @@ -7,12 +7,12 @@ import { DEFAULT_SETTINGS, Logger, LogLevel, - type LogLine, + LogLine, type SyncSettings, type StoredDatabase } from "sync-client"; -import { parseArgs } from "./args"; -import { NodeFileSystemOperations } from "./node-filesystem"; +import { parseArgs, type LineEndingMode } from "./args"; +import { NodeFileSystemOperations, VAULTLINK_DIR } from "./node-filesystem"; import { FileWatcher } from "./file-watcher"; import { formatLogLine } from "./logger-formatter"; import packageJson from "../package.json"; @@ -50,7 +50,7 @@ function createLogHandler(minLevel: LogLevel): (logLine: LogLine) => void { const HEALTH_CHECK_INTERVAL_MS = 30 * 1000; const PROGRESS_LOG_INTERVAL_MS = 2000; -function resolveLineEndings(mode: "auto" | "lf" | "crlf"): string { +function resolveLineEndings(mode: LineEndingMode): string { switch (mode) { case "lf": return "\n"; @@ -65,9 +65,13 @@ async function main(): Promise { const args = parseArgs(process.argv); const absolutePath = path.resolve(args.localPath); - const logger = new Logger(); const logHandler = createLogHandler(args.logLevel); - logger.onLogEmitted.add(logHandler); + // Boot-time messages are emitted directly through logHandler before the + // SyncClient (and its Logger) exist; afterwards every log line flows + // through client.logger. + const emitBoot = (level: LogLevel, message: string): void => { + logHandler(new LogLine(level, message)); + }; if (!fsSync.existsSync(absolutePath)) { fsSync.mkdirSync(absolutePath, { recursive: true }); @@ -76,27 +80,31 @@ async function main(): Promise { try { const stats = await fs.stat(absolutePath); if (!stats.isDirectory()) { - logger.error(`${absolutePath} is not a directory`); + emitBoot(LogLevel.ERROR, `${absolutePath} is not a directory`); process.exit(1); } } catch (error) { - logger.error( + emitBoot( + LogLevel.ERROR, `Cannot access directory ${absolutePath}: ${error instanceof Error ? error.message : String(error)}` ); process.exit(1); } if (!args.quiet) { - logger.info(`VaultLink Local CLI v${packageJson.version}`); - logger.info(`Local path: ${absolutePath}`); - logger.info(`Remote URI: ${args.remoteUri}`); - logger.info(`Vault name: ${args.vaultName}`); + emitBoot(LogLevel.INFO, `VaultLink Local CLI v${packageJson.version}`); + emitBoot(LogLevel.INFO, `Local path: ${absolutePath}`); + emitBoot(LogLevel.INFO, `Remote URI: ${args.remoteUri}`); + emitBoot(LogLevel.INFO, `Vault name: ${args.vaultName}`); if (args.lineEndings !== "auto") { - logger.info(`Line endings: ${args.lineEndings.toUpperCase()}`); + emitBoot( + LogLevel.INFO, + `Line endings: ${args.lineEndings.toUpperCase()}` + ); } } - const dataDir = path.join(absolutePath, ".vaultlink"); + const dataDir = path.join(absolutePath, VAULTLINK_DIR); const dataFile = path.join(dataDir, "sync-data.json"); await fs.mkdir(dataDir, { recursive: true }); @@ -105,8 +113,7 @@ async function main(): Promise { const ignorePatterns = [ ...(args.ignorePatterns ?? []), - ".vaultlink/**", - ".git/**" + `${VAULTLINK_DIR}/**` ]; const settings: SyncSettings = { @@ -134,7 +141,10 @@ async function main(): Promise { // eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion database = JSON.parse(content) as Partial; } catch { - logger.warn(`Cannot read data file at ${dataFile}`); + emitBoot( + LogLevel.WARNING, + `Cannot read data file at ${dataFile}` + ); } return { @@ -269,7 +279,6 @@ async function main(): Promise { } main().catch((error: unknown) => { - // Last-resort handler before the logger exists // eslint-disable-next-line no-console console.error( `Unexpected error: ${error instanceof Error ? error.message : String(error)}` diff --git a/frontend/local-client-cli/src/node-filesystem.ts b/frontend/local-client-cli/src/node-filesystem.ts index 7b736c22..08db361e 100644 --- a/frontend/local-client-cli/src/node-filesystem.ts +++ b/frontend/local-client-cli/src/node-filesystem.ts @@ -1,6 +1,7 @@ import * as fs from "fs/promises"; import type { Dirent } from "fs"; import * as path from "path"; +import { randomUUID } from "crypto"; import type { FileSystemOperations, RelativePath, @@ -8,6 +9,11 @@ import type { } from "sync-client"; import { toUnixPath } from "./path-utils"; +// VaultLink's per-vault metadata directory. Holds the persisted sync database +// and the tmp files atomicWrite renames into place; the matching `${VAULTLINK_DIR}/**` +// ignore pattern keeps everything in here invisible to the file watcher. +export const VAULTLINK_DIR = ".vaultlink"; + export class NodeFileSystemOperations implements FileSystemOperations { public constructor(private readonly basePath: string) {} @@ -132,12 +138,22 @@ export class NodeFileSystemOperations implements FileSystemOperations { content: Uint8Array | string, encoding?: BufferEncoding ): Promise { - const tmpPath = fullPath + ".tmp"; - await fs.writeFile(tmpPath, content, encoding); - const fd = await fs.open(tmpPath, "r"); - await fd.datasync(); - await fd.close(); - await fs.rename(tmpPath, fullPath); + const tmpDir = path.join(this.basePath, VAULTLINK_DIR); + await fs.mkdir(tmpDir, { recursive: true }); + const tmpPath = path.join(tmpDir, `atomic-write-${randomUUID()}.tmp`); + try { + await fs.writeFile(tmpPath, content, encoding); + const fd = await fs.open(tmpPath, "r"); + try { + await fd.datasync(); + } finally { + await fd.close(); + } + await fs.rename(tmpPath, fullPath); + } catch (error) { + await fs.unlink(tmpPath).catch(() => undefined); + throw error; + } } private async walkDirectory( diff --git a/frontend/local-client-cli/src/path-utils.ts b/frontend/local-client-cli/src/path-utils.ts index dd89fa67..1ead144c 100644 --- a/frontend/local-client-cli/src/path-utils.ts +++ b/frontend/local-client-cli/src/path-utils.ts @@ -5,8 +5,14 @@ export function toUnixPath(nativePath: string): string { return nativePath.split(path.sep).join(path.posix.sep); } -// Match a file path against a glob pattern -// Extends path.matchesGlob so that "dir/**" also matches the directory itself +// Match a file path against a glob pattern. +// +// Behaves like Node's path.matchesGlob with one extension: `dir/**` matches +// the directory `dir` itself, not only its descendants. The watcher feeds us +// a directory's relative path (e.g. ".git") at the same time it's about to +// recurse into it, and the natural way for users to write the ignore pattern +// is `.git/**` — under stdlib semantics that pattern would let the directory +// through and only block its children, defeating the prune. export function matchesGlob(filePath: string, pattern: string): boolean { if (pattern.endsWith("/**") && filePath === pattern.slice(0, -3)) { return true; diff --git a/frontend/obsidian-plugin/webpack.config.js b/frontend/obsidian-plugin/webpack.config.js index 794f30de..12844fd7 100644 --- a/frontend/obsidian-plugin/webpack.config.js +++ b/frontend/obsidian-plugin/webpack.config.js @@ -47,7 +47,6 @@ module.exports = (env, argv) => ({ const destinations = [ "/volumes/syncthing/Desktop/test/test/.obsidian/plugins/vault-link", "/volumes/syncthing/Desktop/test/test2/.obsidian/plugins/vault-link" - // "/home/andras/obsidian-test/.obsidian/plugins/vault-link" ]; destinations.forEach((destination) => { fs.copy(source, destination)