From f8d9a7e714f76e20d40c11c0b7fc858032d06a53 Mon Sep 17 00:00:00 2001 From: Carl Thuringer Date: Wed, 26 Apr 2017 17:58:25 -0400 Subject: [PATCH] Support Dotenv with quotes and nested quotes (#98) * Support Dotenv with quotes and nested quotes * Handle case of empty value * Handle empty lines --- ios/ReactNativeConfig/BuildDotenvConfig.ruby | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/ios/ReactNativeConfig/BuildDotenvConfig.ruby b/ios/ReactNativeConfig/BuildDotenvConfig.ruby index da334f4a..5de61414 100755 --- a/ios/ReactNativeConfig/BuildDotenvConfig.ruby +++ b/ios/ReactNativeConfig/BuildDotenvConfig.ruby @@ -14,16 +14,17 @@ end puts "Reading env from #{file}" dotenv = begin + # https://regex101.com/r/SLdbes/1 + dotenv_pattern = /^(?[[:upper:]_]+)=((?["'])(?.*?[^\\])\k|)$/ # 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("**************************")