Skip to content

Commit

Permalink
Add upgrade task for 7.0
Browse files Browse the repository at this point in the history
Supports removing webpacker and adding jsbundling-rails
  • Loading branch information
tvdeyen committed Jan 31, 2023
1 parent 9420293 commit cf2273d
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 5 deletions.
11 changes: 8 additions & 3 deletions lib/alchemy/upgrader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,14 @@ def copy_new_config_file
end

def update_npm_package
desc "Install new npm package."
`yarn add @alchemy_cms/admin@~#{Alchemy.version}`
log "Installed new npm package."
desc "Update npm package."
if File.exist? Rails.root.join("config/importmap.rb")
`bin/importmap pin @alchemy_cms/admin@~#{Alchemy.version}`
elsif File.exist? Rails.root.join("package.json")
`yarn add @alchemy_cms/admin@~#{Alchemy.version}`
else
log("Could not update alchemy admin package! Make sure you have a JS bundler installed", :warning)
end
end
end
end
Expand Down
47 changes: 47 additions & 0 deletions lib/alchemy/upgrader/seven_point_zero.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# frozen_string_literal: true

require "thor"

module Alchemy
class Upgrader::SevenPointZero < Upgrader
include Thor::Base
include Thor::Actions

class << self
def update_admin_entrypoint
if File.exist? "app/javascript/packs/alchemy/admin.js"
FileUtils.mv "app/javascript/packs/alchemy/admin.js", "app/javascript/alchemy_admin.js"
else
log "Skipping. No alchemy/admin entrypoint found. Maybe already migrated from Webpacker?", :info
end
if Dir.exist? "app/javascript/packs/alchemy"
if Dir.empty? "app/javascript/packs/alchemy"
FileUtils.rm_r "app/javascript/packs/alchemy"
end
end
if File.exist? "config/importmap.rb"
# We want the bundled package if using importmaps
task.gsub_file "app/javascript/alchemy_admin.js", 'import "@alchemy_cms/admin"', 'import "@alchemy_cms/dist/admin"'
end
if task.ask("Do you want to remove webpacker now? (y/N)", default: "N") == "y"
task.run "yarn remove @rails/webpacker webpack webpack-cli webpack-dev-server"
FileUtils.rm_r "app/javascript/packs"
FileUtils.rm_r "config/webpack"
FileUtils.rm "config/webpacker.yml"
FileUtils.rm "bin/webpack"
FileUtils.rm "bin/webpack-dev-server"
end
if task.ask("Do you want to add jsbundling-rails now? (Y/n)", default: "Y") == "Y"
task.run "bundle add jsbundling-rails"
task.run "bin/rails javascript:install:esbuild"
end
end

private

def task
@_task || new
end
end
end
end
10 changes: 8 additions & 2 deletions lib/tasks/alchemy/upgrade.rake
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,14 @@ namespace :alchemy do
end

namespace "7.0" do
task "run" do
# no-op
task "run" => [
"alchemy:upgrade:7.0:update_admin_entrypoint",
]

desc "Update alchemy admin entrypoint"
task update_admin_entrypoint: [:environment] do
puts "adding npm_package..."
Alchemy::Upgrader::SevenPointZero.update_admin_entrypoint
end
end
end
Expand Down

0 comments on commit cf2273d

Please sign in to comment.