Skip to content

Commit

Permalink
Switch to NPM by default and away from a Yarn dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredcwhite committed Mar 17, 2024
1 parent 2a413fc commit 1373077
Show file tree
Hide file tree
Showing 42 changed files with 3,420 additions and 2,214 deletions.
2 changes: 1 addition & 1 deletion bridgetown-core/lib/bridgetown-core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ def load_tasks
unless Bridgetown::Current.preloaded_configuration
Bridgetown::Current.preloaded_configuration = Bridgetown::Configuration::Preflight.new
end
Bridgetown::PluginManager.setup_bundler(skip_yarn: true)
Bridgetown::PluginManager.setup_bundler(skip_npm: true)

if Bridgetown::Current.preloaded_configuration.is_a?(Bridgetown::Configuration::Preflight)
Bridgetown::Current.preloaded_configuration = Bridgetown.configuration
Expand Down
13 changes: 9 additions & 4 deletions bridgetown-core/lib/bridgetown-core/commands/concerns/actions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,19 @@ def ruby_configure(name, data = "")
before: %r!^end$!, verbose: false, force: false
end

def add_yarn_for_gem(gemname)
say_status :add_yarn, gemname
def add_npm_for_gem(gemname)
say_status :add_npm, gemname

Bundler.reset!
Bridgetown::PluginManager.load_determined_bundler_environment
Bridgetown::PluginManager.install_yarn_dependencies(name: gemname)
Bridgetown::PluginManager.install_npm_dependencies(name: gemname)
rescue SystemExit
say_status :add_yarn, "Package not added due to yarn error", :red
say_status :add_npm, "Package not added due to NPM error", :red
end
alias_method :add_yarn_for_gem, :add_npm_for_gem

def add_npm_package(package_details)
run "#{Bridgetown::PluginManager.package_manager} #{Bridgetown::PluginManager.package_manager_install_command} #{package_details}" # rubocop:disable Layout
end

def apply_from_url(url)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ const sassPlugin = (options) => ({
// Process .scss and .sass files with Sass
build.onLoad({ filter: /\.(sass|scss)$/ }, async (args) => {
if (!sass) {
console.error("error: Sass is not installed. Try running `yarn add sass` and then building again.")
console.error("error: Sass is not installed. Try running `npm i sass -D` and then building again.")
return
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

gsub_file "package.json", %r! "postcss-focus-within": "^4.0.0",?!, ""

run "yarn add -D #{required_packages.join(" ")}"
add_npm_package "-D #{required_packages.join(" ")}"

packages_to_remove = package_json["devDependencies"].slice(*redundant_packages).keys
unless packages_to_remove.empty?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
say "🎉 esbuild configuration updated successfully!"
say "You may need to add `$styles/` to the front of your main CSS imports."
say "See https://www.bridgetownrb.com/docs/frontend-assets#esbuild-setup for details."
say "⚠️ Don't forget to update the esbuild version in your `package.json` file to \"^0.19.2\""
say "and run `yarn install`!"
say "⚠️ Don't forget to update the esbuild version in your `package.json` file to \"^0.20.2\""
say "and run `npm install`!"
22 changes: 11 additions & 11 deletions bridgetown-core/lib/bridgetown-core/commands/new.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ def self.banner
class_option :"skip-bundle",
type: :boolean,
desc: "Skip 'bundle install'"
class_option :"skip-yarn",
class_option :"skip-npm",
type: :boolean,
desc: "Skip 'yarn install'"
desc: "Skip 'npm install'"
class_option :"use-sass",
type: :boolean,
desc: "Set up a Sass configuration for your stylesheet"
Expand Down Expand Up @@ -186,8 +186,8 @@ def after_install(path, cli_path, options = {})
@skipped_bundle = true # is set to false if bundle install worked
bundle_install path unless options["skip-bundle"]

@skipped_yarn = true
yarn_install path unless options["skip-yarn"]
@skipped_npm = true
npm_install path unless options["skip-npm"]

invoke(Apply, [], options) if options[:apply]
invoke(Configure, options[:configure].split(","), {}) if options[:configure]
Expand All @@ -197,9 +197,9 @@ def after_install(path, cli_path, options = {})
logger.info ""
logger.info "Success!".green, "🎉 Your new Bridgetown site was " \
"generated in #{cli_path.cyan}."
if options["skip-yarn"]
if options["skip-npm"]
logger.info "You can now #{"cd".cyan} #{cli_path.cyan} to get started."
logger.info "You'll probably also want to #{"yarn install".cyan} " \
logger.info "You'll probably also want to #{"npm install".cyan} " \
"to load in your frontend assets."
else
logger.info "You can now #{"cd".cyan} #{cli_path.cyan} and run #{bt_start.cyan} " \
Expand All @@ -213,7 +213,7 @@ def after_install(path, cli_path, options = {})
logger.info "You will need to run #{"bundle binstubs bridgetown-core".cyan} manually."
end

logger.info "Yarn install skipped.".yellow if @skipped_yarn
logger.info "NPM install skipped.".yellow if @skipped_npm
end
# rubocop:enable Metrics/CyclomaticComplexity
# rubocop:enable Metrics/PerceivedComplexity
Expand Down Expand Up @@ -246,15 +246,15 @@ def git_init(path)
say_status :alert, "Could not load git. git init skipped.", :red
end

def yarn_install(path)
def npm_install(path)
unless Bridgetown.environment.test?
inside(path) do
run "yarn install", abort_on_failure: true
run "npm install", abort_on_failure: true
end
end
@skipped_yarn = false
@skipped_npm = false
rescue SystemExit
say_status :alert, "Could not load yarn. yarn install skipped.", :red
say_status :alert, "Could not load npm. NPM install skipped.", :red
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
plugins = %w(postcss-mixins postcss-color-mod-function cssnano)

say "Adding the following PostCSS plugins: #{plugins.join(' | ')}", :green
run "yarn add -D #{plugins.join(' ')}"
add_npm_package "-D #{plugins.join(' ')}"

copy_file in_templates_dir("postcss.config.js"), "postcss.config.js", force: true

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# Install packages

say "Installing Cypress...", :green
run "yarn add -D cypress"
add_npm_package "-D cypress"

# Copy cypress files and tasks into place
cypress_tasks = File.read(in_templates_dir("cypress_tasks"))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
namespace :cy do
desc "Open Cypress test runner"
task :open do
system "yarn run cypress open"
system "npm run cypress open"
end

desc "Run Cypress tests headless"
task :run do
system "yarn run cypress run"
system "npm run cypress run"
end

desc "Start server and open Cypress test runner"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ jobs:
- name: Setup Node
uses: actions/setup-node@v2
with:
node-version: "16"
cache: "yarn"
- run: yarn install
node-version: "20"
cache: "npm"
- run: npm install

- name: Build
env:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

say_status "is-land", "Installing <is-land>..."

run "yarn add @11ty/is-land"
add_npm_package "@11ty/is-land"

javascript_import do
<<~JS
Expand Down
2 changes: 1 addition & 1 deletion bridgetown-core/lib/bridgetown-core/configurations/lit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

add_gem "bridgetown-lit-renderer", version: "2.1.0.beta2"

run "yarn add lit esbuild-plugin-lit-css [email protected]"
add_npm_package "lit esbuild-plugin-lit-css [email protected]"

copy_file in_templates_dir("lit-ssr.config.js"), "config/lit-ssr.config.js"
copy_file in_templates_dir("lit-components-entry.js"), "config/lit-components-entry.js"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

run "yarn add open-props"
add_npm_package "open-props"

variables_import = <<~CSS
@import "variables.css";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

say_status :purgecss, "Installing PurgeCSS"

run "yarn add -D purgecss"
add_npm_package "-D purgecss"

create_builder "purgecss.rb" do
<<~RUBY
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
say_status :ruby2js, "Installing Ruby2JS..."

add_gem "ruby2js"
run "yarn add -D @ruby2js/esbuild-plugin"
add_npm_package "-D @ruby2js/esbuild-plugin"

insert_into_file "esbuild.config.js",
after: 'const build = require("./config/esbuild.defaults.js")' do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

say_status :shoelace, "Installing Shoelace..."

run "yarn add @shoelace-style/shoelace"
add_npm_package "@shoelace-style/shoelace"

stylesheet_import = <<~CSS
/* Import the base Shoelace stylesheet: */
Expand Down Expand Up @@ -42,9 +42,9 @@
"shoelace:copy-assets": "mkdir -p src/shoelace-assets && cp -r node_modules/@shoelace-style/shoelace/dist/assets src/shoelace-assets",
JS
end
gsub_file "package.json", %r{"esbuild": "node}, '"esbuild": "yarn shoelace:copy-assets && node'
gsub_file "package.json", %r{"esbuild": "node}, '"esbuild": "npm run shoelace:copy-assets && node'
gsub_file "package.json", %r{"esbuild-dev": "node},
'"esbuild-dev": "yarn shoelace:copy-assets && node'
'"esbuild-dev": "npm run shoelace:copy-assets && node'

if File.exist?(".gitignore")
append_to_file ".gitignore" do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

say "Installing Stimulus...", :green

run("yarn add @hotwired/stimulus")
add_npm_package "@hotwired/stimulus"

say 'Adding Stimulus to "frontend/javascript/index.js"...', :magenta

Expand Down
3 changes: 1 addition & 2 deletions bridgetown-core/lib/bridgetown-core/configurations/turbo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

say_status :turbo, "Installing Turbo..."

run("yarn add @hotwired/turbo")
run("yarn add turbo-shadow")
add_npm_package "@hotwired/turbo turbo-shadow"

say_status :turbo, 'Adding Turbo to "frontend/javascript/index.js"...', :magenta

Expand Down
57 changes: 30 additions & 27 deletions bridgetown-core/lib/bridgetown-core/plugin_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
module Bridgetown
class PluginManager
LEGACY_PLUGINS_GROUP = :bridgetown_plugins
YARN_DEPENDENCY_REGEXP = %r!(.+)@([^@]*)$!
NPM_DEPENDENCY_REGEXP = %r!(.+)@([^@]*)$!

attr_reader :site, :loaders_manager

Expand Down Expand Up @@ -60,13 +60,13 @@ def source_manifests
end
end

def self.setup_bundler(skip_yarn: false)
def self.setup_bundler(skip_npm: false)
if !ENV["BRIDGETOWN_NO_BUNDLER_REQUIRE"] &&
(Bundler::SharedHelpers.in_bundle? || Bridgetown.env.test?)
require "bundler"

require_relative "utils/initializers"
load_determined_bundler_environment(skip_yarn:)
load_determined_bundler_environment(skip_npm:)

ENV["BRIDGETOWN_NO_BUNDLER_REQUIRE"] = "true"
true
Expand All @@ -78,7 +78,7 @@ class << self
alias_method :require_from_bundler, :setup_bundler
end

def self.load_determined_bundler_environment(skip_yarn: false)
def self.load_determined_bundler_environment(skip_npm: false)
boot_file = File.join("config", "boot.rb")

if File.file?(boot_file)
Expand All @@ -91,13 +91,13 @@ def self.load_determined_bundler_environment(skip_yarn: false)
Bundler.setup(:default, Bridgetown.env)
else
# Only setup and require :bridgetown_plugins
legacy_yarn_and_register(legacy_require, skip_yarn:)
legacy_npm_and_register(legacy_require, skip_npm:)
end
end

def self.require_gem(name)
Bridgetown::Utils::RequireGems.require_with_graceful_fail(name)
plugins = Bridgetown::PluginManager.install_yarn_dependencies(name:)
plugins = Bridgetown::PluginManager.install_npm_dependencies(name:)

plugin_to_register = if plugins.length == 1
plugins.first
Expand All @@ -118,8 +118,8 @@ def self.legacy_require
end
end

def self.legacy_yarn_and_register(required_gems, skip_yarn: false)
install_yarn_dependencies(required_gems) unless skip_yarn
def self.legacy_npm_and_register(required_gems, skip_npm: false)
install_npm_dependencies(required_gems) unless skip_npm

required_gems.each do |installed_gem|
add_registered_plugin installed_gem
Expand All @@ -130,10 +130,10 @@ def self.legacy_yarn_and_register(required_gems, skip_yarn: false)
end

def self.package_manager
@package_manager ||= if File.exist?("yarn.lock")
"yarn"
elsif File.exist?("package-lock.json")
@package_manager ||= if File.exist?("package-lock.json")
"npm"
elsif File.exist?("yarn.lock")
"yarn"
elsif File.exist?("pnpm-lock.yaml")
"pnpm"
else
Expand All @@ -147,11 +147,11 @@ def self.package_manager_install_command

# rubocop:disable Metrics/PerceivedComplexity, Metrics/CyclomaticComplexity

# Iterates through loaded gems and finds yard-add gemspec metadata.
# If that exact package hasn't been installed, execute yarn add
# Iterates through loaded gems and finds npm-add gemspec metadata.
# If that exact package hasn't been installed, execute npm i
#
# @return [Bundler::SpecSet]
def self.install_yarn_dependencies(required_gems = bundler_specs, name: nil)
def self.install_npm_dependencies(required_gems = bundler_specs, name: nil)
return required_gems unless File.exist?("package.json")

package_json = JSON.parse(File.read("package.json"))
Expand All @@ -166,34 +166,37 @@ def self.install_yarn_dependencies(required_gems = bundler_specs, name: nil)

# all right, time to install the package
gems_to_search.each do |loaded_gem|
yarn_dependency = find_yarn_dependency(loaded_gem)
next unless add_yarn_dependency?(yarn_dependency, package_json)
npm_dependency = find_npm_dependency(loaded_gem)
next unless add_npm_dependency?(npm_dependency, package_json)

next if package_manager.empty?

cmd = "#{package_manager} #{package_manager_install_command} #{yarn_dependency.join("@")}"
cmd = "#{package_manager} #{package_manager_install_command} #{npm_dependency.join("@")}"
system cmd
end

gems_to_search
end

# rubocop:enable Metrics/PerceivedComplexity, Metrics/CyclomaticComplexity

def self.find_yarn_dependency(loaded_gem)
yarn_dependency = loaded_gem.to_spec&.metadata&.dig("yarn-add")&.match(YARN_DEPENDENCY_REGEXP)
return nil if yarn_dependency&.length != 3 || yarn_dependency[2] == ""
def self.find_npm_dependency(loaded_gem)
npm_metadata = loaded_gem.to_spec&.metadata&.dig("npm-add") ||
loaded_gem.to_spec&.metadata&.dig("yarn-add")
npm_dependency = npm_metadata&.match(NPM_DEPENDENCY_REGEXP)
return nil if npm_dependency&.length != 3 || npm_dependency[2] == ""

yarn_dependency[1..2]
npm_dependency[1..2]
end

def self.add_yarn_dependency?(yarn_dependency, package_json)
return false if yarn_dependency.nil?
# rubocop:enable Metrics/PerceivedComplexity, Metrics/CyclomaticComplexity

def self.add_npm_dependency?(npm_dependency, package_json)
return false if npm_dependency.nil?

# check matching version number is see if it's already installed
if package_json["dependencies"]
current_version = package_json["dependencies"][yarn_dependency.first]
package_requires_updating?(current_version, yarn_dependency.last)
current_version = package_json["dependencies"][npm_dependency.first]
current_version = current_version.delete_prefix("^") if current_version
package_requires_updating?(current_version, npm_dependency.last)
else
true
end
Expand Down
Loading

0 comments on commit 1373077

Please sign in to comment.