Sha256: 504588c187325d42de7974eec33578b678462daa0082fc8682250fc9c290ee04

Contents?: true

Size: 1.34 KB

Versions: 13

Compression:

Stored size: 1.34 KB

Contents

require 'dotenv'

class Jets::Dotenv
  def self.load!(remote=false)
    new(remote).load!
  end

  def initialize(remote=false)
    @remote = ENV['JETS_ENV_REMOTE'] || remote
  end

  def load!
    return if on_aws? # this prevents ssm calls if used in dotenv files
    vars = ::Dotenv.load(*dotenv_files)
    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
  end

  # dotenv files with the following precedence:
  #
  # - .env.development.jets_extra (highest)
  # - .env.development.remote (2nd highest, only if JETS_ENV_REMOTE=1)
  # - .env.development.local (unless JETS_ENV_REMOTE=1)
  # - .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 << root.join(".env.#{Jets.env}.remote") if @remote
    if Jets.extra
      files << root.join(".env.#{Jets.env}.#{Jets.extra}")
    end
    files.compact
  end

  def root
    Jets.root || Pathname.new(ENV["JETS_ROOT"] || Dir.pwd)
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
jets-5.0.11 lib/jets/dotenv.rb
jets-5.0.10 lib/jets/dotenv.rb
jets-5.0.9 lib/jets/dotenv.rb
jets-5.0.8 lib/jets/dotenv.rb
jets-5.0.7 lib/jets/dotenv.rb
jets-5.0.6 lib/jets/dotenv.rb
jets-5.0.5 lib/jets/dotenv.rb
jets-5.0.4 lib/jets/dotenv.rb
jets-5.0.3 lib/jets/dotenv.rb
jets-5.0.2 lib/jets/dotenv.rb
jets-5.0.1 lib/jets/dotenv.rb
jets-5.0.0 lib/jets/dotenv.rb
jets-5.0.0.beta1 lib/jets/dotenv.rb