lib/jets/dotenv.rb in jets-5.0.11 vs lib/jets/dotenv.rb in jets-5.0.12
- old
+ new
@@ -1,25 +1,31 @@
-require 'dotenv'
+require "dotenv"
class Jets::Dotenv
- def self.load!(remote=false)
+ def self.load!(remote = false)
new(remote).load!
end
- def initialize(remote=false)
- @remote = ENV['JETS_ENV_REMOTE'] || remote
+ def initialize(remote = false)
+ @remote = ENV["JETS_ENV_REMOTE"] || remote
end
+ # @@vars cache to prevent multiple calls to Ssm
+ # Tricky note: The cache also prevents the second call to Dotenv.load from
+ # returning {} vars. Dotenv 3.0 will not return the vars if it has already been loaded
+ # in the ENV. We want this side-effect due to the new way Dotenv 3.0 works.
+ @@vars = nil
def load!
+ return @@vars if @@vars
return if on_aws? # this prevents ssm calls if used in dotenv files
vars = ::Dotenv.load(*dotenv_files)
- Ssm.new(vars).interpolate!
+ @@vars = Ssm.new(vars).interpolate!
end
def on_aws?
- return true if ENV['ON_AWS']
- !!ENV['_HANDLER'] # https://docs.aws.amazon.com/lambda/latest/dg/lambda-environment-variables.html
+ return true if ENV["ON_AWS"]
+ !!ENV["_HANDLER"] # https://docs.aws.amazon.com/lambda/latest/dg/lambda-environment-variables.html
end
# dotenv files with the following precedence:
#
# - .env.development.jets_extra (highest)
@@ -28,20 +34,19 @@
# - .env.development
# - .env.local - This file is loaded for all environments _except_ `test` or unless JETS_ENV_REMOTE=1
# - .env - The original (lowest)
#
def dotenv_files
- files = [
- root.join(".env"),
- (root.join(".env.local") unless (Jets.env.test? || @remote)),
- root.join(".env.#{Jets.env}"),
- (root.join(".env.#{Jets.env}.local") unless @remote),
- ]
+ files = []
+
+ files << files << root.join(".env.#{Jets.env}.#{Jets.extra}") if Jets.extra
files << root.join(".env.#{Jets.env}.remote") if @remote
- if Jets.extra
- files << root.join(".env.#{Jets.env}.#{Jets.extra}")
- end
- files.compact
+ files << root.join(".env.#{Jets.env}.local") unless @remote
+ files << root.join(".env.#{Jets.env}")
+ files << root.join(".env.local") unless Jets.env.test? || @remote
+ files << root.join(".env")
+
+ files.compact.map(&:to_s)
end
def root
Jets.root || Pathname.new(ENV["JETS_ROOT"] || Dir.pwd)
end