Skip to content

Commit

Permalink
Make sure .env files are loaded properly if provided.
Browse files Browse the repository at this point in the history
  • Loading branch information
gdotdesign committed Oct 21, 2024
1 parent e8d6c08 commit b81322d
Show file tree
Hide file tree
Showing 13 changed files with 30 additions and 19 deletions.
3 changes: 3 additions & 0 deletions spec/workspace_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ describe Mint::Workspace do
path: Path[workspace.root_path, "mint.json"].to_s,
check: Mint::Check::Environment,
include_tests: false,
dot_env: ".env",
format: false)

results.size.should eq(1)
Expand All @@ -30,6 +31,7 @@ describe Mint::Workspace do
check: Mint::Check::Environment,
path: workspace.root_path,
include_tests: false,
dot_env: ".env",
format: false)

results.size.should eq(1)
Expand All @@ -51,6 +53,7 @@ describe Mint::Workspace do
path: Path[workspace.root_path, "mint.json"].to_s,
check: Mint::Check::Environment,
include_tests: false,
dot_env: ".env",
format: false)

FileUtils.touch(Path[workspace.root_path, "Main.mint"])
Expand Down
5 changes: 0 additions & 5 deletions src/command.cr
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,6 @@ module Mint
result = nil

begin
# Load environment variables.
Env.init(env) do |file|
terminal.puts "#{COG} Loaded environment variables from: #{file}"
end

check_dependencies! if check_dependencies

# Measure elapsed time of a command.
Expand Down
3 changes: 2 additions & 1 deletion src/commands/build.cr
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,10 @@ module Mint

def run
execute "Building for production",
check_dependencies: true, env: flags.env do
check_dependencies: true do
Workspace.new(
path: Path[Dir.current, "mint.json"].to_s,
dot_env: flags.env || ".env",
check: Check::Environment,
include_tests: false,
format: false,
Expand Down
3 changes: 2 additions & 1 deletion src/commands/start.cr
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,11 @@ module Mint

def run
execute "Running the development server",
check_dependencies: true, env: flags.env do
check_dependencies: true do
Reactor.new(
reload: !flags.no_reload,
format: flags.format,
dot_env: flags.env,
host: flags.host,
port: flags.port
) do |type_checker|
Expand Down
2 changes: 1 addition & 1 deletion src/commands/test.cr
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ module Mint

def run
runner =
execute "Running tests", env: flags.env do
execute "Running tests" do
TestRunner.new(flags, arguments)
end

Expand Down
1 change: 1 addition & 0 deletions src/commands/tool/docs_server.cr
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ module Mint
Reactor.new(
host: flags.host,
port: flags.port,
dot_env: ".env",
format: false,
reload: true,
) do |type_checker|
Expand Down
7 changes: 3 additions & 4 deletions src/env.cr
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,14 @@ module Mint

class_getter env : String?

def load(&)
def load
env.try do |value|
MINT_ENV.clear
MINT_ENV.merge!(Dotenv.load(value))
yield
end
end

def init(raw, &)
def init(raw)
env =
if !raw.presence
".env" if File.exists?(".env")
Expand All @@ -33,7 +32,7 @@ module Mint
end unless File.exists?(env)

@@env = env
load { yield env }
load
end
end
end
Expand Down
1 change: 1 addition & 0 deletions src/ls/sandbox.cr
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ module Mint
listener: ->build(TypeChecker | Error),
check: Check::Unreachable,
include_tests: true,
dot_env: ".env",
format: false)
end

Expand Down
1 change: 1 addition & 0 deletions src/ls/server.cr
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ module Mint
Workspace.new(
check: Check::Unreachable,
include_tests: true,
dot_env: ".env",
listener: nil,
format: false,
path: base)
Expand Down
2 changes: 2 additions & 0 deletions src/reactor.cr
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ module Mint
def initialize(
*,
@reload : Bool,
dot_env,
format,
host,
port,
&listener : Proc(TypeChecker, Hash(String, Proc(String)))
)
Workspace.new(
path: Path[Dir.current, "mint.json"].to_s,
dot_env: dot_env || ".env",
check: Check::Environment,
include_tests: false,
format: format,
Expand Down
1 change: 1 addition & 0 deletions src/test_runner.cr
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ module Mint

Workspace.new(
path: Path[Dir.current, "mint.json"].to_s,
dot_env: flags.env || ".env",
check: Check::Environment,
include_tests: true,
format: false,
Expand Down
4 changes: 2 additions & 2 deletions src/utils/source_files.cr
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ module Mint
end.map { |dir| glob_pattern(File.dirname(json.path), dir) }
end

def everything(json : MintJson, *, include_tests = false) : Array(String)
def everything(json : MintJson, *, include_tests = false, dot_env = ".env") : Array(String)
packages(json, include_self: true)
.flat_map { |item| globs(item, include_tests: include_tests) + [item.path] }
.push(Path[File.dirname(json.path), ".env"].to_s)
.push(Path[File.dirname(json.path), dot_env].to_s)
end

def packages(json : MintJson, *, include_self = false) : Array(MintJson)
Expand Down
16 changes: 11 additions & 5 deletions src/workspace.cr
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,14 @@ module Mint
*,
@listener : Proc(TypeChecker | Error, Nil) | Nil,
@include_tests : Bool,
dot_env : String,
@format : Bool,
@check : Check,
@path : String
)
@dot_env =
File.basename(dot_env)

(@watcher = Watcher.new(&->update(Array(String), Symbol)))
.tap { reset }
.watch
Expand Down Expand Up @@ -102,7 +106,8 @@ module Mint
@watcher.patterns =
SourceFiles.everything(
MintJson.parse(@path, search: true),
include_tests: @include_tests)
include_tests: @include_tests,
dot_env: @dot_env)
rescue error : Error
set(error)
end
Expand Down Expand Up @@ -154,11 +159,12 @@ module Mint
# 1. packages could have changed
# 2. source directories could have changed
# 3. variables in the .env file cloud have changed
case basename = File.basename(file)
when "mint.json", ".env"
Env.init(basename) { } if basename == ".env"
actions << :reset
case File.basename(file)
when @dot_env
Env.init(file)
end

actions << :reset
end
end
end
Expand Down

0 comments on commit b81322d

Please sign in to comment.