https://github.com/bannzai/ocha
Listen to the file system change notifications and raises events when a directory, or file in a directory, changes. π΅
https://github.com/bannzai/ocha
Last synced: about 1 year ago
JSON representation
Listen to the file system change notifications and raises events when a directory, or file in a directory, changes. π΅
- Host: GitHub
- URL: https://github.com/bannzai/ocha
- Owner: bannzai
- License: mit
- Created: 2019-03-01T07:59:08.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2019-06-25T23:23:14.000Z (almost 7 years ago)
- Last Synced: 2025-04-06T05:00:02.474Z (about 1 year ago)
- Language: Swift
- Homepage:
- Size: 2.67 MB
- Stars: 130
- Watchers: 3
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](logo/ocha_logo.png)
[](https://developer.apple.com/swift)
[](https://img.shields.io/badge/platform-macOS-green.svg)
[](https://github.com/bannzai/Ocha/blob/master/LICENSE)
# Ocha
`Ocha` can be listen to the file system change notifications and raises events when a directory, or file in a directory, changes.
Ocha means Teaπ΅ in Japan.
## Usage
To use Ocha, first create a `Watcher` instance with file paths you want to watch.
```swift
let watcher = Watcher(paths: [pathString])
```
And you can call for `Watcher.start(_:)` method when it start to watch file events.
When file removed, you get callback with removed file path infomations.
```swift
watcher.start { (events) in ... }
```
## Example
The following example execute `git add << REMOVED_FILE_PATH >>` and `git commit -m << REMOVED_FILE_PATH >>` to watch the file path, when it removed.
```swift
import Foundation
import Ocha
import SwiftShell
import PathKit
let path = Path(
#file.components(separatedBy: "/")
.dropLast() // main.swift
.dropLast() // GitCommitExample
.dropLast() // Sources
.joined(separator: "/")
)
let pathString = path.absolute().string
main.currentdirectory = pathString
let watcher = Watcher(paths: [pathString])
watcher.start { (events) in
let removedEventPaths = events
.filter { $0.flag.contains(.removedFile) }
.map { $0.path }
removedEventPaths.forEach { path in
main.run(bash: "git add \(path)")
main.run(bash: "git commit -m \"Delete file \(path)\"")
}
}
RunLoop.current.run()
```
If you execute this swift code, you can confirm removed file git log. `$ git log -1`
## Try it easily
You can easily try Ocha by editing the `Playground` target.
1. Clone this repository with git or Download sources.
2. Open `./Sources/Playground/main.swift` .
3. Add monitored paths and processes.
4. Run `swift run Playground` on Terminal.
## LICENSE
[Ocha](https://github.com/bannzai/Ocha/) is released under the MIT license. See [LICENSE](https://github.com/bannzai/Ocha/blob/master/LICENSE.txt) for details.
Header logo is released [CC BY-NC 4.0](https://creativecommons.org/licenses/by-nc/4.0/deed) license. Original design by [noainoue](https://github.com/noainoue).