Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/falood/file_system
Filesystem monitor for elixir
https://github.com/falood/file_system
Last synced: about 2 months ago
JSON representation
Filesystem monitor for elixir
- Host: GitHub
- URL: https://github.com/falood/file_system
- Owner: falood
- License: apache-2.0
- Created: 2014-10-30T14:18:53.000Z (over 9 years ago)
- Default Branch: main
- Last Pushed: 2024-02-14T04:20:22.000Z (4 months ago)
- Last Synced: 2024-04-14T04:09:36.634Z (2 months ago)
- Language: Elixir
- Homepage:
- Size: 9.6 MB
- Stars: 253
- Watchers: 4
- Forks: 52
- Open Issues: 6
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Lists
- awesome-elixir - exfswatch - A file change watcher wrapper based on __fs__. (Files and Directories)
README
# FileSystem
[![Module Version](https://img.shields.io/hexpm/v/file_system.svg)](https://hex.pm/packages/file_system)
[![Hex Docs](https://img.shields.io/badge/hex-docs-lightgreen.svg)](https://hexdocs.pm/file_system/)
[![Total Download](https://img.shields.io/hexpm/dt/file_system.svg)](https://hex.pm/packages/file_system)
[![License](https://img.shields.io/hexpm/l/file_system.svg)](https://github.com/falood/file_system/blob/master/LICENSE)
[![Last Updated](https://img.shields.io/github/last-commit/falood/file_system.svg)](https://github.com/falood/file_system/commits/master)
[![CI Linux](https://github.com/falood/file_system/actions/workflows/ci-linux.yml/badge.svg)](https://github.com/falood/file_system/actions)
[![CI MacOS](https://github.com/falood/file_system/actions/workflows/ci-macos.yml/badge.svg)](https://github.com/falood/file_system/actions)
[![CI Windows](https://github.com/falood/file_system/actions/workflows/ci-windows.yml/badge.svg)](https://github.com/falood/file_system/actions)An Elixir file change watcher wrapper based on
[FS](https://github.com/synrc/fs), the native file system listener.## System Support
- MacOS - [fsevent](https://github.com/thibaudgg/rb-fsevent)
- GNU/Linux, FreeBSD, DragonFly and OpenBSD - [inotify](https://github.com/rvoicilas/inotify-tools/wiki)
- Windows - [inotify-win](https://github.com/thekid/inotify-win)On MacOS 10.14, to compile `mac_listener`, run:
```console
open /Library/Developer/CommandLineTools/Packages/macOS_SDK_headers_for_macOS_10.14.pkg
```## Usage
Add `file_system` to the `deps` of your mix.exs
``` elixir
defmodule MyApp.Mixfile do
use Mix.Projectdef project do
...
enddefp deps do
[
{:file_system, "~> 1.0", only: :test},
]
end
...
end
```### Subscription API
You can spawn a worker and subscribe to events from it:
```elixir
{:ok, pid} = FileSystem.start_link(dirs: ["/path/to/some/files"])
FileSystem.subscribe(pid)
```or
```elixir
{:ok, pid} = FileSystem.start_link(dirs: ["/path/to/some/files"], name: :my_monitor_name)
FileSystem.subscribe(:my_monitor_name)
```The `pid` you subscribed from will now receive messages like:
```
{:file_event, worker_pid, {file_path, events}}
```
and```
{:file_event, worker_pid, :stop}
```### Example Using GenServer
```elixir
defmodule Watcher do
use GenServerdef start_link(args) do
GenServer.start_link(__MODULE__, args)
enddef init(args) do
{:ok, watcher_pid} = FileSystem.start_link(args)
FileSystem.subscribe(watcher_pid)
{:ok, %{watcher_pid: watcher_pid}}
enddef handle_info({:file_event, watcher_pid, {path, events}}, %{watcher_pid: watcher_pid} = state) do
# Your own logic for path and events
{:noreply, state}
enddef handle_info({:file_event, watcher_pid, :stop}, %{watcher_pid: watcher_pid} = state) do
# Your own logic when monitor stop
{:noreply, state}
end
end
```## Backend Options
For each platform, you can pass extra options to the underlying listener
process.Each backend supports different extra options, check backend module
documentation for more details.Here is an example to get instant notifications on file changes for MacOS:
```elixir
FileSystem.start_link(dirs: ["/path/to/some/files"], latency: 0, watch_root: true)
```