2024-04-01 09:07:45 -07:00
|
|
|
# typed: strict
|
2023-03-14 13:01:58 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2024-04-01 09:07:45 -07:00
|
|
|
require "abstract_command"
|
2023-03-14 13:01:58 -04:00
|
|
|
require "formula"
|
|
|
|
|
|
|
|
module Homebrew
|
2024-04-01 09:07:45 -07:00
|
|
|
module Cmd
|
|
|
|
class NodenvSync < AbstractCommand
|
|
|
|
cmd_args do
|
|
|
|
description <<~EOS
|
2025-01-27 15:12:50 +00:00
|
|
|
Create symlinks for Homebrew's installed NodeJS versions in `~/.nodenv/versions`.
|
2023-03-14 13:01:58 -04:00
|
|
|
|
2024-04-01 09:07:45 -07:00
|
|
|
Note that older version symlinks will also be created so e.g. NodeJS 19.1.0 will
|
|
|
|
also be symlinked to 19.0.0.
|
|
|
|
EOS
|
2023-03-14 13:01:58 -04:00
|
|
|
|
2024-04-01 09:07:45 -07:00
|
|
|
named_args :none
|
|
|
|
end
|
2023-03-14 13:01:58 -04:00
|
|
|
|
2024-04-01 09:07:45 -07:00
|
|
|
sig { override.void }
|
|
|
|
def run
|
|
|
|
nodenv_root = Pathname(ENV.fetch("HOMEBREW_NODENV_ROOT", Pathname(Dir.home)/".nodenv"))
|
2023-03-14 13:01:58 -04:00
|
|
|
|
2024-04-01 09:07:45 -07:00
|
|
|
# Don't run multiple times at once.
|
|
|
|
nodenv_sync_running = nodenv_root/".nodenv_sync_running"
|
|
|
|
return if nodenv_sync_running.exist?
|
2023-03-14 13:01:58 -04:00
|
|
|
|
2024-04-01 09:07:45 -07:00
|
|
|
begin
|
|
|
|
nodenv_versions = nodenv_root/"versions"
|
|
|
|
nodenv_versions.mkpath
|
|
|
|
FileUtils.touch nodenv_sync_running
|
2023-03-14 13:01:58 -04:00
|
|
|
|
2024-04-01 09:07:45 -07:00
|
|
|
HOMEBREW_CELLAR.glob("node{,@*}")
|
|
|
|
.flat_map(&:children)
|
|
|
|
.each { |path| link_nodenv_versions(path, nodenv_versions) }
|
2023-03-14 13:01:58 -04:00
|
|
|
|
2024-04-01 09:07:45 -07:00
|
|
|
nodenv_versions.children
|
|
|
|
.select(&:symlink?)
|
|
|
|
.reject(&:exist?)
|
|
|
|
.each { |path| FileUtils.rm_f path }
|
|
|
|
ensure
|
|
|
|
nodenv_sync_running.unlink if nodenv_sync_running.exist?
|
|
|
|
end
|
|
|
|
end
|
2023-03-14 13:01:58 -04:00
|
|
|
|
2024-04-01 09:07:45 -07:00
|
|
|
private
|
2023-03-14 13:01:58 -04:00
|
|
|
|
2024-04-01 09:07:45 -07:00
|
|
|
sig { params(path: Pathname, nodenv_versions: Pathname).void }
|
|
|
|
def link_nodenv_versions(path, nodenv_versions)
|
|
|
|
nodenv_versions.mkpath
|
2023-03-14 13:01:58 -04:00
|
|
|
|
2024-04-01 09:07:45 -07:00
|
|
|
version = Keg.new(path).version
|
|
|
|
major_version = version.major.to_i
|
|
|
|
minor_version = version.minor.to_i || 0
|
|
|
|
patch_version = version.patch.to_i || 0
|
2023-03-14 13:01:58 -04:00
|
|
|
|
2025-03-24 15:00:16 +00:00
|
|
|
minor_version_range, patch_version_range = if Homebrew::EnvConfig.env_sync_strict?
|
|
|
|
# Only create symlinks for the exact installed patch version.
|
|
|
|
# e.g. 23.9.0 => 23.9.0
|
|
|
|
[[minor_version], [patch_version]]
|
|
|
|
else
|
|
|
|
# Create folder symlinks for all patch versions to the latest patch version
|
|
|
|
# e.g. 23.9.0 => 23.10.1
|
|
|
|
[0..minor_version, 0..patch_version]
|
|
|
|
end
|
|
|
|
|
|
|
|
minor_version_range.each do |minor|
|
|
|
|
patch_version_range.each do |patch|
|
2024-04-01 09:07:45 -07:00
|
|
|
link_path = nodenv_versions/"#{major_version}.#{minor}.#{patch}"
|
2024-04-26 18:32:54 +08:00
|
|
|
# Don't clobber existing user installations.
|
|
|
|
next if link_path.exist? && !link_path.symlink?
|
2023-03-14 13:01:58 -04:00
|
|
|
|
2024-04-01 09:07:45 -07:00
|
|
|
FileUtils.rm_f link_path
|
2025-03-24 15:00:16 +00:00
|
|
|
FileUtils.ln_s path, link_path
|
2024-04-01 09:07:45 -07:00
|
|
|
end
|
|
|
|
end
|
2023-03-14 13:01:58 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|