Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prerequisites: Use system version of Node #1201

Merged
merged 1 commit into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ GEM
base64 (0.1.1)
bigdecimal (3.1.4)
builder (3.2.4)
climate_control (1.2.0)
concurrent-ruby (1.2.2)
connection_pool (2.4.1)
crass (1.0.6)
Expand Down Expand Up @@ -234,6 +235,7 @@ PLATFORMS
x86_64-linux

DEPENDENCIES
climate_control
mocha
puma
sqlite3
Expand Down
1 change: 1 addition & 0 deletions lib/generators/suspenders/install/web_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module Install
class WebGenerator < Rails::Generators::Base
include Suspenders::Generators::APIAppUnsupported
include Suspenders::Generators::DatabaseUnsupported
include Suspenders::Generators::NodeNotInstalled

source_root File.expand_path("../../../templates/install/web", __FILE__)
desc <<~MARKDOWN
Expand Down
5 changes: 4 additions & 1 deletion lib/generators/suspenders/prerequisites_generator.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
module Suspenders
module Generators
class PrerequisitesGenerator < Rails::Generators::Base
include Suspenders::Generators::Helpers
include Suspenders::Generators::NodeNotInstalled

source_root File.expand_path("../../templates/prerequisites", __FILE__)

desc <<~MARKDOWN
Creates `.node-version` file set to the current LTS version.
MARKDOWN

def node_version
def set_node_version
template "node-version", ".node-version"
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/generators/templates/prerequisites/node-version.tt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<%= Suspenders::NODE_LTS_VERSION %>
<%= node_version %>
4 changes: 3 additions & 1 deletion lib/suspenders/cleanup/generate_readme.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
module Suspenders
module Cleanup
class GenerateReadme
include Suspenders::Generators::Helpers

def self.perform(readme, app_name)
new(readme, app_name).perform
end
Expand Down Expand Up @@ -153,7 +155,7 @@ def prerequisites

@file.write <<~MARKDOWN
Ruby: `#{Suspenders::MINIMUM_RUBY_VERSION}`
Node: `#{Suspenders::NODE_LTS_VERSION}`
Node: `#{node_version}`
MARKDOWN

new_line
Expand Down
27 changes: 27 additions & 0 deletions lib/suspenders/generators.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ def default_test_helper_present?
def rspec_test_helper_present?
File.exist? Rails.root.join("spec/rails_helper.rb")
end

def node_version
ENV["NODE_VERSION"] || `node --version`[/\d+\.\d+\.\d+/]
end
Comment on lines +22 to +24
Copy link
Contributor Author

@stevepolitodesign stevepolitodesign May 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As an aside, the code we lifted from Rails actually has a bug. This is why I modified it here to not use a fallback.

end

module APIAppUnsupported
Expand Down Expand Up @@ -46,6 +50,8 @@ def api_only_app?
end

module DatabaseUnsupported
include Helpers

class Error < StandardError
def message
"This generator requires PostgreSQL"
Expand All @@ -72,5 +78,26 @@ def database_unsupported?
end
end
end

module NodeNotInstalled
class Error < StandardError
end

extend ActiveSupport::Concern

included do
def raise_if_node_not_installed
if node_not_installed?
raise Suspenders::Generators::NodeNotInstalled::Error
end
end
end

private

def node_not_installed?
!node_version.present?
end
end
end
end
1 change: 0 additions & 1 deletion lib/suspenders/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,4 @@ module Suspenders
VERSION = "3.0.0".freeze
RAILS_VERSION = "~> 7.0".freeze
MINIMUM_RUBY_VERSION = ">= 3.1".freeze
NODE_LTS_VERSION = "20.11.1".freeze
end
1 change: 1 addition & 0 deletions suspenders.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ Gem::Specification.new do |spec|

spec.add_dependency "rails", Suspenders::RAILS_VERSION

spec.add_development_dependency "climate_control"
spec.add_development_dependency "mocha"
spec.add_development_dependency "standard"
end
10 changes: 10 additions & 0 deletions test/generators/suspenders/install/web_generator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@ class WebGeneratorTest < Rails::Generators::TestCase
end
end

test "raises if Node is not installed" do
Object.any_instance.stubs(:`).returns("")

with_database "postgresql" do
assert_raises Suspenders::Generators::NodeNotInstalled::Error do
run_generator
end
end
end

private

def prepare_destination
Expand Down
27 changes: 25 additions & 2 deletions test/generators/suspenders/prerequisites_generator_test.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require "test_helper"
require "climate_control"
require "generators/suspenders/prerequisites_generator"

module Suspenders
Expand All @@ -10,14 +11,36 @@ class PrerequisitesGeneratorTest < Rails::Generators::TestCase
destination Rails.root
teardown :restore_destination

test "generates .node-version file" do
test "generates .node-version file (from ENV)" do
ClimateControl.modify NODE_VERSION: "1.2.3" do
run_generator

assert_file app_root(".node-version") do |file|
assert_match(/1\.2\.3/, file)
end
end
end

test "generates .node-version file (from system)" do
Object.any_instance.stubs(:`).returns("v1.2.3\n")

run_generator

assert_file app_root(".node-version") do |file|
assert_match(/20\.11\.1/, file)
assert_match(/1\.2\.3/, file)
end
end

test "raises if Node is not installed" do
Object.any_instance.stubs(:`).returns("")

assert_raises Suspenders::Generators::NodeNotInstalled::Error do
run_generator
end

assert_no_file app_root(".node-version")
end

private

def restore_destination
Expand Down
4 changes: 3 additions & 1 deletion test/suspenders/cleanup/generate_readme_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ module Suspenders
module Cleanup
class GenerateReadmeTest < ActiveSupport::TestCase
test "generates README using generator descriptions" do
Object.any_instance.stubs(:`).returns("v1.2.3\n")

Tempfile.create "README.md" do |readme|
path = readme.path

Expand All @@ -18,7 +20,7 @@ class GenerateReadmeTest < ActiveSupport::TestCase

assert_match "## Prerequisites", readme
assert_match Suspenders::MINIMUM_RUBY_VERSION, readme
assert_match Suspenders::NODE_LTS_VERSION, readme
assert_match "Node: `1.2.3`", readme

assert_match "## Configuration", readme
assert_match "### Test", readme
Expand Down
4 changes: 0 additions & 4 deletions test/suspenders_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,4 @@ class SuspendersTest < ActiveSupport::TestCase
test "it has a Minimum Ruby version number" do
assert Suspenders::MINIMUM_RUBY_VERSION
end

test "it has a Node LTS version number" do
assert Suspenders::NODE_LTS_VERSION
end
end