Skip to content

Commit

Permalink
Merge pull request #5 from aosasona/add-js-support
Browse files Browse the repository at this point in the history
Added support for Javascript target
  • Loading branch information
aosasona committed Dec 16, 2023
2 parents 0484814 + e6a4b00 commit b59da8b
Show file tree
Hide file tree
Showing 9 changed files with 164 additions and 80 deletions.
33 changes: 27 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,39 @@
# dot_env

<!--toc:start-->

- [dot_env](#dotenv)
- [Quick start](#quick-start)
- [Installation](#installation)
<!--toc:end-->

[![Package Version](https://img.shields.io/hexpm/v/dot_env)](https://hex.pm/packages/dotenv)
[![Hex Docs](https://img.shields.io/badge/hex-docs-ffaff3)](https://hexdocs.pm/dot_env/)

dot_env is a port of the popular JavaScript [dotenv](https://github.com/motdotla/dotenv/blob/master/lib/main.js) package that helps you load environment variables from .env (or other) files.
dot_env is a port of the popular JavaScript [dotenv](https://github.com/motdotla/dotenv) package that helps you load environment variables from .env (or other) files.

> This package may support other formats in the future but for now, only supports the popular .env format
> This package may support other formats in the future but for now, supports the popular .env format
>
> You can find the Javascript test [here](https://github.com/aosasona/dot_js_test)
## Quick start

```sh
gleam run # Run the project
gleam test # Run the tests
gleam shell # Run an Erlang shell
```gleam
import dot_env
import dot_env/env
import gleam/io
pub fn main() {
dot_env.load_with_opts(dot_env.Opts(path: "path/to/.env", debug: False, capitalize: False))
// or `dot_env.load()` to load it the `.env` file in the root path
case env.get("MY_ENV_VAR") {
Ok(value) -> io.println(value)
Error(_) -> io.println("something went wrong")
}
Nil
}
```

## Installation
Expand Down
10 changes: 3 additions & 7 deletions gleam.toml
Original file line number Diff line number Diff line change
@@ -1,23 +1,19 @@
name = "dot_env"
version = "0.2.0"
version = "0.2.1"

# Fill out these fields if you intend to generate HTML documentation or publish
# your project to the Hex package manager.
#
description = "Load environment variables from a .env file"
description = "Load environment variables from files"
licences = ["Apache-2.0"]
repository = { type = "github", user = "aosasona", repo = "dotenv" }
# links = [{ title = "Website", href = "https://gleam.run" }]

internal_modules = [
"dot_env/internal/*"
]

gleam = ">= 0.32.0"

[dependencies]
gleam_stdlib = "~> 0.32"
simplifile = "~> 0.1"
gleam_erlang = "~> 0.22"

[dev-dependencies]
gleeunit = "~> 0.10"
2 changes: 0 additions & 2 deletions manifest.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@
# You typically do not need to edit this file

packages = [
{ name = "gleam_erlang", version = "0.22.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_erlang", source = "hex", outer_checksum = "367D8B41A7A86809928ED1E7E55BFD0D46D7C4CF473440190F324AFA347109B4" },
{ name = "gleam_stdlib", version = "0.32.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "07D64C26D014CF570F8ACADCE602761EA2E74C842D26F2FD49B0D61973D9966F" },
{ name = "gleeunit", version = "0.11.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "1397E5C4AC4108769EE979939AC39BF7870659C5AFB714630DEEEE16B8272AD5" },
{ name = "simplifile", version = "0.1.14", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "simplifile", source = "hex", outer_checksum = "10EA0207796F20488A3A166C50A189C9385333F3C9FAC187729DE7B9CE4ADDBC" },
]

[requirements]
gleam_erlang = { version = "~> 0.22" }
gleam_stdlib = { version = "~> 0.32" }
gleeunit = { version = "~> 0.10" }
simplifile = { version = "~> 0.1" }
14 changes: 7 additions & 7 deletions src/dot_env.gleam
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import gleam/io
import gleam/string
import gleam/erlang/os
import gleam/result.{try}
import dot_env/internal/parser
import dot_env/env
import simplifile

pub type Opts {
Expand Down Expand Up @@ -94,26 +94,26 @@ fn recursively_set_environment_variables(
kv_pairs: parser.EnvPairs,
) {
case kv_pairs {
[pair, ..rest] -> {
os.set_env(
[] -> Nil
[pair] -> {
env.set(
case config.capitalize {
True -> string.uppercase(pair.0)
False -> pair.0
},
pair.1,
)
recursively_set_environment_variables(config, rest)
}
[pair] -> {
os.set_env(
[pair, ..rest] -> {
env.set(
case config.capitalize {
True -> string.uppercase(pair.0)
False -> pair.0
},
pair.1,
)
recursively_set_environment_variables(config, rest)
}
[] -> Nil
}
}

Expand Down
28 changes: 28 additions & 0 deletions src/dot_env/env.gleam
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/// Set an environment variable (supports both Erlang and JavaScript targets)
///
/// Example:
/// ```gleam
/// import dot_env/env
///
/// env.set("MY_ENV_VAR", "my value")
/// ```
///
@external(erlang, "dot_env_ffi", "set_env")
@external(javascript, "../dot_env_ffi.mjs", "set_env")
pub fn set(key: String, value: String) -> Nil

/// Get an environment variable (supports both Erlang and JavaScript targets)
///
/// Example:
/// ```gleam
/// import dot_env/env
/// import gleam/io
/// import gleam/result
///
/// env.get("MY_ENV_VAR")
/// |> result.unwrap("NOT SET")
/// |> io.println
/// ```
@external(erlang, "dot_env_ffi", "get_env")
@external(javascript, "../dot_env_ffi.mjs", "get_env")
pub fn get(key: String) -> Result(String, String)
1 change: 0 additions & 1 deletion src/dot_env/internal/parser.gleam
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import gleam/bool
import gleam/io
import gleam/string
import gleam/list
import gleam/option.{Some}
Expand Down
15 changes: 15 additions & 0 deletions src/dot_env_ffi.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
-module(dot_env_ffi).

-export([get_env/1, set_env/2]).

get_env(Name) ->
case os:getenv(binary_to_list(Name)) of
false ->
{error, nil};
Value ->
{ok, list_to_binary(Value)}
end.

set_env(Name, Value) ->
os:putenv(binary_to_list(Name), binary_to_list(Value)),
nil.
27 changes: 27 additions & 0 deletions src/dot_env_ffi.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { Ok as GleamOk, Error as GleamError } from "./gleam.mjs";

const Nil = undefined;

export function set_env(key, value) {
if (!process.env) {
console.error("process.env is not available");
return Nil;
}

process.env[key] = value;
return Nil;
}

export function get_env(key) {
if (!process.env) {
console.error("process.env is not available");
return new GleamError("process.env is not available");
}

const value = process.env[key];
if (!value) {
return new GleamError(`key \`${key}\` is not set`);
}

return new GleamOk(value);
}
Loading

0 comments on commit b59da8b

Please sign in to comment.