-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from aosasona/add-js-support
Added support for Javascript target
- Loading branch information
Showing
9 changed files
with
164 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
Oops, something went wrong.