From a7b686ce4141f3595c7c1c0681ccc0f5d13b3bb7 Mon Sep 17 00:00:00 2001
From: Ananthu C V <weepingclown@disroot.org>
Date: Thu, 19 Dec 2024 03:31:27 +0530
Subject: [PATCH] Add support for yarnpkg

Debian and derivates have yarn packaged as yarnpkg
---
 lib/install/helpers.rb           | 20 ++++++++++++++++----
 lib/tasks/cssbundling/build.rake |  4 +++-
 2 files changed, 19 insertions(+), 5 deletions(-)

diff --git a/lib/install/helpers.rb b/lib/install/helpers.rb
index dd4e974..c59ead8 100644
--- a/lib/install/helpers.rb
+++ b/lib/install/helpers.rb
@@ -2,11 +2,23 @@
 
 module Helpers
   def bundler_cmd
-    using_bun? ? "bun" : "yarn"
+    if using_bun?
+      "bun"
+    elsif tool_exists?('yarnpkg')
+      "yarnpkg"
+    else
+      "yarn"
+    end
   end
 
   def bundler_run_cmd
-    using_bun? ? "bun run" : "yarn"
+    if using_bun?
+      "bun"
+    elsif tool_exists?('yarnpkg')
+      "yarnpkg"
+    else
+      "yarn"
+    end
   end
 
   def using_bun?
@@ -29,11 +41,11 @@ def add_package_json_script(name, script, run_script=true)
       when 7.1...8.0
         say "Add #{name} script"
         run %(npm set-script #{name} "#{script}")
-        run %(yarn #{name}) if run_script
+        tool_exists?("yarnpkg") ? (run %(yarnpkg #{name}) if run_script) : (run %(yarn #{name}) if run_script)
       when (8.0..)
         say "Add #{name} script"
         run %(npm pkg set scripts.#{name}="#{script}")
-        run %(yarn #{name}) if run_script
+        tool_exists?("yarnpkg") ? (run %(yarnpkg #{name}) if run_script) : (run %(yarn #{name}) if run_script)
       else
         say %(Add "scripts": { "#{name}": "#{script}" } to your package.json), :green
       end
diff --git a/lib/tasks/cssbundling/build.rake b/lib/tasks/cssbundling/build.rake
index 8410147..537783c 100644
--- a/lib/tasks/cssbundling/build.rake
+++ b/lib/tasks/cssbundling/build.rake
@@ -25,6 +25,7 @@ module Cssbundling
       case tool
       when :bun then "bun install"
       when :yarn then "yarn install"
+      when :yarnpkg then "yarnpkg install"
       when :pnpm then "pnpm install"
       when :npm then "npm install"
       else raise "cssbundling-rails: No suitable tool found for installing JavaScript dependencies"
@@ -35,6 +36,7 @@ module Cssbundling
       case tool
       when :bun then "bun run build:css"
       when :yarn then "yarn build:css"
+      when :yarnpkg then "yarnpkg build:css"
       when :pnpm then "pnpm build:css"
       when :npm then "npm run build:css"
       else raise "cssbundling-rails: No suitable tool found for building CSS"
@@ -44,11 +46,11 @@ module Cssbundling
     def tool
       case
       when File.exist?('bun.lockb') then :bun
-      when File.exist?('yarn.lock') then :yarn
       when File.exist?('pnpm-lock.yaml') then :pnpm
       when File.exist?('package-lock.json') then :npm
       when tool_exists?('bun') then :bun
       when tool_exists?('yarn') then :yarn
+      when tool_exists?('yarnpkg') then :yarnpkg
       when tool_exists?('pnpm') then :pnpm
       when tool_exists?('npm') then :npm
       end