https://github.com/preciz/tmp
Temporary monitored directories in Elixir.
https://github.com/preciz/tmp
directory elixir temporary tmp
Last synced: 3 months ago
JSON representation
Temporary monitored directories in Elixir.
- Host: GitHub
- URL: https://github.com/preciz/tmp
- Owner: preciz
- License: mit
- Created: 2020-08-16T16:21:33.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2025-01-01T14:03:05.000Z (about 1 year ago)
- Last Synced: 2025-09-27T02:19:45.160Z (4 months ago)
- Topics: directory, elixir, temporary, tmp
- Language: Elixir
- Homepage: https://hex.pm/packages/tmp
- Size: 70.3 KB
- Stars: 17
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# Tmp
[](https://github.com/preciz/tmp/actions/workflows/test.yml)
Temporary directories that are monitored and automatically removed in Elixir.
## Installation
The package can be installed by adding `tmp` to your list of dependencies in `mix.exs`:
```elixir
def deps do
[
{:tmp, "~> 0.3.0"}
]
end
```
## Usage
Define your Tmp module:
```elixir
defmodule MyApp.Tmp do
use Tmp
end
# Or with a custom base directory
defmodule MyApp.CustomTmp do
use Tmp, base_dir: "/path/where/tmp/dirs/will/be"
end
```
Add it to your supervision tree:
```elixir
children = [
{MyApp.Tmp, name: MyApp.Tmp}
]
```
Use it in your code:
```elixir
MyApp.Tmp.dir(fn tmp_dir_path ->
file_path = Path.join(tmp_dir_path, "my_file")
# do work with file_path...
# then return a value
Enum.sum([1,2,3])
end)
# => 6
```
### Options
When calling `MyApp.Tmp.dir/2`, you can pass the following options:
- `:prefix` (optional) - Prefix for the temporary directory name, defaults to `nil`
- `:base_dir` (optional) - Base directory for the temporary directory, defaults to `System.tmp_dir()` or the value set in `use Tmp`
- `:timeout` (optional) - Timeout in milliseconds, defaults to `:infinity`
### More Examples
```elixir
MyApp.Tmp.dir(fn tmp_dir_path ->
File.touch(Path.join(tmp_dir_path, "file_one"))
# ... other important work
2 + 2
end, prefix: "my_app", base_dir: "/tmp/custom_base")
# => 4
```
## Docs
Documentation can be found at [https://hexdocs.pm/tmp](https://hexdocs.pm/tmp).
## License
Tmp is [MIT licensed](LICENSE).