Skip to content

Commit

Permalink
Preserve bundle-size; backward-compatible with unpadded placeholder. (#4
Browse files Browse the repository at this point in the history
)

* Preserve bundle-size; backward-compatible with unpadded placeholder.

* Preserve size of bundle in characters instead of bytes.
  • Loading branch information
mars authored Oct 24, 2016
1 parent 7c02a59 commit 1db3314
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 6 deletions.
9 changes: 7 additions & 2 deletions lib/injectable_env.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

class InjectableEnv
DefaultVarMatcher = /^REACT_APP_/
Placeholder='{{REACT_APP_VARS_AS_JSON}}'
Placeholder = /\{\{REACT_APP_VARS_AS_JSON_*?\}\}/

def self.create(var_matcher=DefaultVarMatcher)
vars = ENV.find_all {|name,value| var_matcher===name }
Expand All @@ -25,10 +25,15 @@ def self.render(*args)

def self.replace(file, *args)
injectee = IO.read(file)
return unless injectee.index(Placeholder)
return unless placeholder = injectee.match(Placeholder)
placeholder_size = placeholder.to_s.size

env = create(*args)
env_size = env.size
new_padding = placeholder_size - env_size
env = env + (' ' * [new_padding, 0].max)
head,_,tail = injectee.partition(Placeholder)

injected = head + env + tail
File.open(file, 'w') do |f|
f.write(injected)
Expand Down
58 changes: 54 additions & 4 deletions spec/injectable_env_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@

RSpec.describe InjectableEnv do

Placeholder = '{{REACT_APP_VARS_AS_JSON______________________________________________________________________________________________________}}'
UnpaddedPlaceholder = '{{REACT_APP_VARS_AS_JSON}}'

describe '.create' do
it "returns empty object" do
expect(InjectableEnv.create).to eq('{}')
Expand Down Expand Up @@ -63,7 +66,7 @@

describe '.replace' do
before do
ENV['REACT_APP_HELLO'] = "Hello\n\"World\" we \\ prices today"
ENV['REACT_APP_HELLO'] = "Hello\n\"World\" we \\ prices today 🌞"
end
after do
ENV.delete 'REACT_APP_HELLO'
Expand All @@ -72,14 +75,61 @@
it "writes into file" do
begin
file = Tempfile.new('injectable_env_test')
file.write('var injected="{{REACT_APP_VARS_AS_JSON}}"')
file.write(%{var injected="#{Placeholder}"})
file.rewind

InjectableEnv.replace(file.path)

expected_value='var injected="{\\"REACT_APP_HELLO\\":\\"Hello\\\\n\\\\\"World\\\\\" we \\\\\\\\ prices today\\"}"'
expected_value='var injected="{\\"REACT_APP_HELLO\\":\\"Hello\\\\n\\\\\"World\\\\\" we \\\\\\\\ prices today 🌞\\"}'
actual_value=file.read
expect(actual_value).to eq(expected_value)
expect(actual_value.index(expected_value)).to eq(0)
# Closing double-quote is padded out but still last char.
actual_size = actual_value.size
expect(actual_value.index(/\"\Z/)).to eq(actual_size-1)
ensure
if file
file.close
file.unlink
end
end
end

it "matches unpadded placeholder" do
begin
file = Tempfile.new('injectable_env_test')
file.write(%{var injected="#{UnpaddedPlaceholder}"})
file.rewind

InjectableEnv.replace(file.path)

expected_value='var injected="{\\"REACT_APP_HELLO\\":\\"Hello\\\\n\\\\\"World\\\\\" we \\\\\\\\ prices today 🌞\\"}'
actual_value=file.read
expect(actual_value.index(expected_value)).to eq(0)
# Closing double-quote is padded out but still last char.
actual_size = actual_value.size
expect(actual_value.index(/\"\Z/)).to eq(actual_size-1)
ensure
if file
file.close
file.unlink
end
end
end

it "preserves character length of bundle" do
begin
placeholder_size = Placeholder.size
file = Tempfile.new('injectable_env_test')
file.write(Placeholder)
file.rewind

InjectableEnv.replace(file.path)

expected_value = '{\\"REACT_APP_HELLO\\":\\"Hello\\\\n\\\\\"World\\\\\" we \\\\\\\\ prices today 🌞\\"}'
actual_value = file.read
replaced_size = actual_value.size
expect(replaced_size).to eq(placeholder_size)
expect(actual_value.index(expected_value)).to eq(0)
ensure
if file
file.close
Expand Down

0 comments on commit 1db3314

Please sign in to comment.