Skip to content

Commit

Permalink
Improve package json template
Browse files Browse the repository at this point in the history
  • Loading branch information
robwise committed Jan 15, 2016
1 parent c1593aa commit 4e8b7d8
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 39 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<%- require 'react_on_rails/version' -%>
<%- require_relative "../react_on_rails/version_syntax_converter" -%>
{
"name": "react-webpack-rails-tutorial",
"version": "1.1.0",
Expand Down Expand Up @@ -64,7 +64,7 @@
"react-bootstrap": "^0.28.1",
<%- end -%>
"react-dom": "^0.14.3",
"react-on-rails": "<%= x = ReactOnRails::VERSION.match(/(\d+\.\d+\.\d+)/); "#{x[1]}" %>",
"react-on-rails": "<%= VersionSyntaxConverter.new.rubygem_to_npm %>",
<%- if options.redux? -%>
"react-redux": "^4.0.5",
"redux": "^3.0.5",
Expand Down
25 changes: 11 additions & 14 deletions lib/react_on_rails/version_checker.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
require_relative "version"
require_relative "version_syntax_converter"

module ReactOnRails
# Responsible for checking versions of rubygem versus npm node package
# against each otherat runtime.
class VersionChecker
attr_reader :node_package_version, :logger

Expand All @@ -13,10 +16,12 @@ def initialize(node_package_version, logger)
@node_package_version = node_package_version
end

# For compatibility, the gem and the node package versions should always match, unless the user
# really knows what they're doing. So we will give a warning if they do not.
# For compatibility, the gem and the node package versions should always match,
# unless the user really knows what they're doing. So we will give a
# warning if they do not.
def warn_if_gem_and_node_package_versions_differ
return if node_package_version.major == gem_major_version || node_package_version.relative_path?
return if node_package_version.relative_path?
return if node_package_version.major == gem_major_version
log_differing_versions_warning
end

Expand Down Expand Up @@ -58,21 +63,13 @@ def raw
JSON.parse(package_json_contents)["dependencies"]["react-on-rails"]
end

def normalized
match = raw
.tr("-", ".")
.strip
.match(/(\d.*)/)
match.present? ? match[0] : nil
end

def relative_path?
normalized.nil? # must be a relative path if nil and we haven't failed
raw.match(/\.\./).present?
end

def major
return if normalized.nil?
normalized.match(/(\d+)\./)[1]
return if relative_path?
raw.match(/(\d+)\./)[1]
end

private
Expand Down
21 changes: 21 additions & 0 deletions lib/react_on_rails/version_syntax_converter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
require_relative "version"

class VersionSyntaxConverter
def rubygem_to_npm(rubygem_version)
rubygem_version ||= ReactOnRails::VERSION
regex_match = rubygem_version.match(/(\d+\.\d+\.\d+)[.\-]?(.+)?/)
if regex_match[2]
return "#{regex_match[1]}-#{regex_match[2]}"
else
return "#{regex_match[1]}"
end
end

def npm_to_rubygem(npm_version)
match = npm_version
.tr("-", ".")
.strip
.match(/(\d.*)/)
match.present? ? match[0] : nil
end
end
5 changes: 3 additions & 2 deletions spec/react_on_rails/generators/install_generator_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require File.expand_path("../../support/generator_spec_helper", __FILE__)
require_relative "../support/generator_spec_helper"
require_relative "../support/version_test_helpers"

describe InstallGenerator, type: :generator do
destination File.expand_path("../../dummy-for-generators/", __FILE__)
Expand Down Expand Up @@ -210,7 +211,7 @@
end

context "with missing files to trigger errors" do
it "GeneratorMessages has the missing file error" do
specify "GeneratorMessages has the missing file error" do
run_generator_test_with_args([], gitignore: false)
expected = <<-MSG.strip_heredoc
.gitignore was not found.
Expand Down
32 changes: 32 additions & 0 deletions spec/react_on_rails/generators/version_syntax_converter_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
require_relative "../simplecov_helper"
require_relative "../spec_helper"
require_relative "../support/version_test_helpers"
require_relative "../../../lib/react_on_rails/version_syntax_converter"

RSpec.describe VersionSyntaxConverter do
subject(:converter) { VersionSyntaxConverter.new }

describe "#rubygem_to_npm" do
context "when gem version is 1.0.0" do
specify { expect(converter.rubygem_to_npm("1.0.0")).to eq "1.0.0" }
end

context "when gem version is 10.20.30.rc.4" do
specify { expect(converter.rubygem_to_npm("10.20.30.rc.4")).to eq "10.20.30-rc.4" }
end
end

describe "#npm_to_rubygem" do
context "with an npm version of '0.0.2'" do
specify { expect(converter.npm_to_rubygem("0.0.2")).to eq("0.0.2") }
end

context "with an npm version of '^14.0.0-beta.2'" do
specify { expect(converter.npm_to_rubygem("^14.0.0-beta.2")).to eq("14.0.0.beta.2") }
end

context "with an npm version of '../../..'" do
specify { expect(converter.npm_to_rubygem("../../..")).to be_nil }
end
end
end
3 changes: 3 additions & 0 deletions spec/react_on_rails/support/version_test_helpers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
def stub_gem_version(version)
stub_const("ReactOnRails::VERSION", version)
end
26 changes: 5 additions & 21 deletions spec/react_on_rails/version_checker_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require_relative "simplecov_helper"
require_relative "spec_helper"
require_relative "support/version_test_helpers"

class FakeLogger
attr_accessor :message
Expand All @@ -15,7 +16,7 @@ module ReactOnRails
let(:logger) { FakeLogger.new }

context "when gem and node package major versions are equal" do
let(:node_package_version) { double_package_version(raw: "^2.2.5", normalized: "2.2.5", major: "2") }
let(:node_package_version) { double_package_version(raw: "^2.2.5", major: "2") }
before { stub_gem_version("2.0.0.beta.2") }

it "does not log a warning" do
Expand All @@ -26,7 +27,7 @@ module ReactOnRails

context "when gem and node package major versions differ" do
let(:node_package_version) do
double_package_version(raw: "13.0.0.beta-2", normalized: "13.0.0.beta.2", major: "13")
double_package_version(raw: "13.0.0.beta-2", major: "13")
end
before { stub_gem_version("12.0.0.beta.1") }

Expand All @@ -38,7 +39,7 @@ module ReactOnRails

context "when package json uses a relative path" do
let(:node_package_version) do
double_package_version(raw: "../../..", normalized: "", major: "", relative_path: true)
double_package_version(raw: "../../..", major: "", relative_path: true)
end
before { stub_gem_version("2.0.0.beta.1") }

Expand All @@ -49,14 +50,9 @@ module ReactOnRails
end
end

def stub_gem_version(version)
stub_const("ReactOnRails::VERSION", version)
end

def double_package_version(raw:, normalized:, major:, relative_path: false)
def double_package_version(raw:, major:, relative_path: false)
instance_double(VersionChecker::NodePackageVersion,
raw: raw,
normalized: normalized,
major: major,
relative_path?: relative_path)
end
Expand All @@ -76,10 +72,6 @@ def check_version(node_package_version, logger)
specify { expect(node_package_version.raw).to eq("0.0.2") }
end

describe "#normalized" do
specify { expect(node_package_version.normalized).to eq("0.0.2") }
end

describe "#relative_path?" do
specify { expect(node_package_version.relative_path?).to be false }
end
Expand All @@ -96,10 +88,6 @@ def check_version(node_package_version, logger)
specify { expect(node_package_version.raw).to eq("^14.0.0.beta-2") }
end

describe "#normalized" do
specify { expect(node_package_version.normalized).to eq("14.0.0.beta.2") }
end

describe "#relative_path?" do
specify { expect(node_package_version.relative_path?).to be false }
end
Expand All @@ -116,10 +104,6 @@ def check_version(node_package_version, logger)
specify { expect(node_package_version.raw).to eq("../../..") }
end

describe "#normalized" do
specify { expect(node_package_version.normalized).to be_nil }
end

describe "#relative_path?" do
specify { expect(node_package_version.relative_path?).to be true }
end
Expand Down

0 comments on commit 4e8b7d8

Please sign in to comment.