Skip to content

Commit

Permalink
Merge pull request #2458 from rails/better-rake-checks
Browse files Browse the repository at this point in the history
Add better conditional messaging for checking yarn & node
  • Loading branch information
jakeNiemiec authored Feb 12, 2020
2 parents 07a62a9 + 7d40a62 commit 3eecc5c
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 19 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ gemspec
gem "rails"
gem "rake", ">= 11.1"
gem "rack-proxy", require: false
gem "semantic_range", require: false

group :test do
gem "minitest", "~> 5.0"
Expand Down
3 changes: 3 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ PATH
activesupport (>= 4.2)
rack-proxy (>= 0.6.1)
railties (>= 4.2)
semantic_range (>= 2.3.0)

GEM
remote: https://rubygems.org/
Expand Down Expand Up @@ -136,6 +137,7 @@ GEM
rubocop-performance (1.3.0)
rubocop (>= 0.68.0)
ruby-progressbar (1.10.1)
semantic_range (2.3.0)
sprockets (4.0.0)
concurrent-ruby (~> 1.0)
rack (> 1, < 3)
Expand Down Expand Up @@ -165,6 +167,7 @@ DEPENDENCIES
rake (>= 11.1)
rubocop (< 0.69)
rubocop-performance
semantic_range
webpacker!

BUNDLED WITH
Expand Down
21 changes: 14 additions & 7 deletions lib/tasks/webpacker/check_node.rake
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require "semantic_range"
namespace :webpacker do
desc "Verifies if Node.js is installed"
task :check_node do
Expand All @@ -6,19 +7,25 @@ namespace :webpacker do
raise Errno::ENOENT if node_version.blank?

pkg_path = Pathname.new("#{__dir__}/../../../package.json").realpath
node_requirement = JSON.parse(pkg_path.read)["engines"]["node"]
node_range = JSON.parse(pkg_path.read)["engines"]["node"]
is_valid = SemanticRange.satisfies?(node_version, node_range) rescue false
semver_major = node_version[/\d+/] rescue nil
is_unstable = semver_major.to_i.odd? rescue false

requirement = Gem::Requirement.new(node_requirement)
version = Gem::Version.new(node_version.strip.tr("v", ""))
if is_unstable
$stderr.puts "Warning: you are using an unstable release of Node.js (#{node_version}). If you encounter issues with Node.js, consider switching to an Active LTS release. More info: https://docs.npmjs.com/try-the-latest-stable-version-of-node"
end

unless requirement.satisfied_by?(version)
$stderr.puts "Webpacker requires Node.js #{requirement} and you are using #{version}"
unless is_valid
$stderr.puts "Webpacker requires Node.js \"#{node_range}\" and you are using #{node_version}"
$stderr.puts "Please upgrade Node.js https://nodejs.org/en/download/"
$stderr.puts "Exiting!" && exit!
$stderr.puts "Exiting!"
exit!
end
rescue Errno::ENOENT
$stderr.puts "Node.js not installed. Please download and install Node.js https://nodejs.org/en/download/"
$stderr.puts "Exiting!" && exit!
$stderr.puts "Exiting!"
exit!
end
end
end
25 changes: 16 additions & 9 deletions lib/tasks/webpacker/check_yarn.rake
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require "semantic_range"
namespace :webpacker do
desc "Verifies if Yarn is installed"
task :check_yarn do
Expand All @@ -6,19 +7,25 @@ namespace :webpacker do
raise Errno::ENOENT if yarn_version.blank?

pkg_path = Pathname.new("#{__dir__}/../../../package.json").realpath
yarn_requirement = JSON.parse(pkg_path.read)["engines"]["yarn"]
yarn_range = JSON.parse(pkg_path.read)["engines"]["yarn"]
is_valid = SemanticRange.satisfies?(yarn_version, yarn_range) rescue false
is_unsupported = SemanticRange.satisfies?(yarn_version, ">=2.0.0") rescue false

requirement = Gem::Requirement.new(yarn_requirement)
version = Gem::Version.new(yarn_version)

unless requirement.satisfied_by?(version)
$stderr.puts "Webpacker requires Yarn #{requirement} and you are using #{version}"
$stderr.puts "Please upgrade Yarn https://yarnpkg.com/lang/en/docs/install/"
$stderr.puts "Exiting!" && exit!
unless is_valid
$stderr.puts "Webpacker requires Yarn \"#{yarn_range}\" and you are using #{yarn_version}"
if is_unsupported
$stderr.puts "This version of Webpacker does not support Yarn #{yarn_version}. Please downgrade to a supported version of Yarn https://yarnpkg.com/lang/en/docs/install/"
$stderr.puts "For information on using Webpacker with Yarn 2.0, see https://github.com/rails/webpacker/issues/2112"
else
$stderr.puts "Please upgrade Yarn https://yarnpkg.com/lang/en/docs/install/"
end
$stderr.puts "Exiting!"
exit!
end
rescue Errno::ENOENT
$stderr.puts "Yarn not installed. Please download and install Yarn from https://yarnpkg.com/lang/en/docs/install/"
$stderr.puts "Exiting!" && exit!
$stderr.puts "Exiting!"
exit!
end
end
end
11 changes: 11 additions & 0 deletions test/rake_tasks_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,17 @@ def test_rake_task_webpacker_check_binstubs
refute_includes output, "webpack binstubs not found."
end

def test_check_node_version
output = Dir.chdir(test_app_path) { `rake webpacker:check_node 2>&1` }
refute_includes output, "Webpacker requires Node.js"
end

def test_check_yarn_version
output = Dir.chdir(test_app_path) { `rake webpacker:check_yarn 2>&1` }
refute_includes output, "Yarn not installed"
refute_includes output, "Webpacker requires Yarn"
end

def test_rake_webpacker_yarn_install_in_non_production_environments
assert_includes test_app_dev_dependencies, "right-pad"

Expand Down
7 changes: 4 additions & 3 deletions webpacker.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ Gem::Specification.new do |s|

s.required_ruby_version = ">= 2.3.0"

s.add_dependency "activesupport", ">= 4.2"
s.add_dependency "railties", ">= 4.2"
s.add_dependency "rack-proxy", ">= 0.6.1"
s.add_dependency "activesupport", ">= 4.2"
s.add_dependency "railties", ">= 4.2"
s.add_dependency "rack-proxy", ">= 0.6.1"
s.add_dependency "semantic_range", ">= 2.3.0"

s.add_development_dependency "bundler", ">= 1.3.0"
s.add_development_dependency "rubocop", "< 0.69"
Expand Down

0 comments on commit 3eecc5c

Please sign in to comment.