olcli icon indicating copy to clipboard operation
olcli copied to clipboard

[Feature] olcli sync --watch: sync again on every local change

Open MehdiMamas opened this issue 2 weeks ago • 1 comments

What would you like?

A --watch flag on olcli sync: after the first sync it stays running and syncs again whenever a local file changes, until Ctrl+C.

olcli sync . --watch

Use case / why this matters:

I write papers in a local editor and keep the Overleaf project open for the PDF and for a co-author. Today that means running olcli sync by hand after each save, or wrapping it in a small watcher script, which is what I did. It seems like something olcli could do itself.

I have a working version on a branch and would like to check the design with you before opening a PR, as CONTRIBUTING.md asks: https://github.com/MehdiMamas/olcli/compare/main...MehdiMamas:olcli:sync-watch

How it works:

  • Each run is the existing sync command started as a child process with the same options (--no-delete, --project, ...), so a watch run behaves exactly like a manual sync and the sync action itself is not touched.
  • Changes are found by polling a snapshot every 2 s, built with scanLocalFiles and the ignore rules, so the watcher sees exactly the files sync would upload. Dot-entries, .olcli.json and .olignore matches never trigger.
  • The snapshot compares file content (a hash, recomputed only when mtime or size changes). A pull that rewrites identical files therefore does not start another run.
  • A save during a running sync gives one more run afterwards; runs never overlap. At most 3 runs in a row, then it waits for the next edit.
  • After each run it prints synced at HH:MM:SS or sync failed at HH:MM:SS (exit N) and keeps watching. --watch with --dry-run is refused.
  • No new dependency. New code is in src/watch.ts with unit tests (injected snapshot, sync and timers, no network), plus README, SKILL.md and CHANGELOG entries.

Tested on Windows against a real project: idle gives one sync and then nothing; one edit gives one push.

Open questions for you:

  1. Is polling acceptable, or would you prefer fs.watch?
  2. Child process per run, or would you rather have the sync action extracted into a function and called in-process? I avoided that refactor to keep the change small.
  3. Should the interval be an option (for example --watch-interval), or is a fixed 2 s fine?

Any alternatives you've considered?

  • fs.watch(dir, { recursive: true }): no polling, but it needs its own ignore filter and debounce, and it is less dependable on synced folders such as iCloud or OneDrive, which is where my papers live.
  • chokidar: solid, but a new runtime dependency for something the existing scan already covers.
  • An external watcher script around olcli sync: works, but every user has to write it again.

MehdiMamas avatar Nov 14 '26 05:11 MehdiMamas

Thanks for checking first. The design sounds good, please go ahead and open the PR.

On your questions:

  1. Polling is fine. It works reliably in iCloud and OneDrive folders, and comparing content avoids extra runs after a pull.
  2. A child process per run is fine for now. Moving sync into a function can be a separate refactor later.
  3. Yes, please add --watch-interval <seconds>, default 2, minimum 1.

Refusing --watch with --dry-run and stopping after 3 runs in a row both make sense. I'll test it on macOS once the PR is up.

aloth avatar Nov 15 '26 18:11 aloth