mirror of
https://github.com/status-im/react-native-config.git
synced 2025-02-23 20:38:10 +00:00
Support Dotenv with quotes and nested quotes (#98)
* Support Dotenv with quotes and nested quotes * Handle case of empty value * Handle empty lines
This commit is contained in:
parent
d2169de8c8
commit
f8d9a7e714
@ -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("**************************")
|
||||
|
Loading…
x
Reference in New Issue
Block a user