Skip to content

Commit

Permalink
Wrap node commands with Capistrano TODO: maybe we can simplify the co…
Browse files Browse the repository at this point in the history
…mmand or use it just for assets:precompile

This code is based on what the rvm1-capistrano and capistrano-nvm gems
do, but simplified a bit to take advantage of the `fnm exec` command.

Since we're currently only using Node to precompile the assets, the
important command here is `rake` in order to run the `assets:precompile`
task. I've also included the default commands gems like capistrano-nvm
use because we will add the `npm` command in the near future.
  • Loading branch information
javierm committed Aug 7, 2023
1 parent bf4f20d commit dec4b9c
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 36 deletions.
37 changes: 36 additions & 1 deletion config/deploy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,16 @@ def main_deploy_server

set :local_user, ENV["USER"]

set :fnm_path, "$HOME/.fnm"
set :fnm_install_command, "curl -fsSL https://fnm.vercel.app/install | " \
"bash -s -- --install-dir \"#{fetch(:fnm_path)}\""
set :fnm_setup_command, -> do
"export PATH=\"#{fetch(:fnm_path)}:$PATH\" && " \
"cd #{release_path} && eval \"$(fnm env)\""
end
set :fnm_install_node_command, -> { "#{fetch(:fnm_setup_command)} && fnm use --install-if-missing" }
set :fnm_map_bins, %w[node npm rake yarn]

set :puma_conf, "#{release_path}/config/puma/#{fetch(:rails_env)}.rb"

set :delayed_job_workers, 2
Expand All @@ -50,6 +60,7 @@ def main_deploy_server

after :updating, "install_ruby"
after "bundler:install", "install_node"
after "bundler:install", "map_node_bins"

after "deploy:migrate", "add_new_settings"

Expand Down Expand Up @@ -90,10 +101,34 @@ def main_deploy_server
end

task :install_node do
on roles(:app) do
with rails_env: fetch(:rails_env) do
begin
execute fetch(:fnm_install_node_command)
rescue SSHKit::Command::Failed
begin
execute fetch(:fnm_setup_command)
rescue SSHKit::Command::Failed
execute fetch(:fnm_install_command)
else
execute "#{fetch(:fnm_install_command)} --skip-shell"
end

execute fetch(:fnm_install_node_command)
end
end
end
end

task :map_node_bins do
on roles(:app) do
within release_path do
with rails_env: fetch(:rails_env) do
execute :rake, "node_manager:install:node"
prefix = "#{fetch(:fnm_setup_command)} && fnm exec"

fetch(:fnm_map_bins).each do |command|
SSHKit.config.command_map.prefix[command.to_sym].unshift(prefix)
end
end
end
end
Expand Down
35 changes: 0 additions & 35 deletions lib/tasks/node_manager.rake

This file was deleted.

0 comments on commit dec4b9c

Please sign in to comment.