Skip to content

Commit

Permalink
Support Dotenv with quotes and nested quotes (lugg#98)
Browse files Browse the repository at this point in the history
* Support Dotenv with quotes and nested quotes

* Handle case of empty value

* Handle empty lines
  • Loading branch information
Carl Thuringer authored and Pedro Belo committed Apr 26, 2017
1 parent d2169de commit f8d9a7e
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions ios/ReactNativeConfig/BuildDotenvConfig.ruby
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,17 @@ end
puts "Reading env from #{file}"

dotenv = begin
# https://regex101.com/r/SLdbes/1
dotenv_pattern = /^(?<key>[[:upper:]_]+)=((?<quote>["'])(?<val>.*?[^\\])\k<quote>|)$/
# find that above node_modules/react-native-config/ios/
raw = File.read(File.join(Dir.pwd, "../../../#{file}"))
raw.split("\n").inject({}) do |h, line|
key, val = line.split("=", 2)
if line.strip.empty? or line.start_with?('#')
h
else
key, val = line.split("=", 2)
h.merge!(key => val)
end
m = line.match(dotenv_pattern)
next h if m.nil?
key = m[:key]
# Ensure string (in case of empty value) and escape any quotes present in the value.
val = m[:val].to_s.gsub('"', '\"')
h.merge(key => val)
end
rescue Errno::ENOENT
puts("**************************")
Expand Down

0 comments on commit f8d9a7e

Please sign in to comment.