[Feature] olcli sync --watch: sync again on every local change
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
synccommand 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
scanLocalFilesand the ignore rules, so the watcher sees exactly the files sync would upload. Dot-entries,.olcli.jsonand.olignorematches 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:SSorsync failed at HH:MM:SS (exit N)and keeps watching.--watchwith--dry-runis refused. - No new dependency. New code is in
src/watch.tswith 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:
- Is polling acceptable, or would you prefer
fs.watch? - 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.
- 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.
Thanks for checking first. The design sounds good, please go ahead and open the PR.
On your questions:
- Polling is fine. It works reliably in iCloud and OneDrive folders, and comparing content avoids extra runs after a pull.
- A child process per run is fine for now. Moving sync into a function can be a separate refactor later.
- 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.