- 
                Notifications
    You must be signed in to change notification settings 
- Fork 99
Improved detection logic for package manager #171
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
          
     Open
      
      
            navidemad
  wants to merge
  2
  commits into
  rails:main
  
    
      
        
          
  
    
      Choose a base branch
      
     
    
      
        
      
      
        
          
          
        
        
          
            
              
              
              
  
           
        
        
          
            
              
              
           
        
       
     
  
        
          
            
          
            
          
        
       
    
      
from
navidemad:feat-package-manager-detection
  
      
      
   
  
    
  
  
  
 
  
      
    base: main
Could not load branches
            
              
  
    Branch not found: {{ refName }}
  
            
                
      Loading
              
            Could not load tags
            
            
              Nothing to show
            
              
  
            
                
      Loading
              
            Are you sure you want to change the base?
            Some commits from the old base branch may be removed from the timeline,
            and old review comments may become outdated.
          
          
  
     Open
                    Changes from 1 commit
      Commits
    
    
            Show all changes
          
          
            2 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      
    File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -1,48 +1,94 @@ | ||
| # frozen_string_literal: true | ||
|  | ||
| require 'json' | ||
|  | ||
| module Helpers | ||
| TOOLS_COMMANDS = { | ||
| yarn: { cmd: 'yarn', run: 'yarn', x: 'npx' }, | ||
| bun: { cmd: 'bun', run: 'bun run', x: 'bunx' }, | ||
| pnpm: { cmd: 'pnpm', run: 'pnpm run', x: 'pnpx' }, | ||
| npm: { cmd: 'npm', run: 'npm run', x: 'npx' }, | ||
| }.freeze | ||
| SUPPORTED_TOOLS = TOOLS_COMMANDS.keys.freeze | ||
| DEFAULT_TOOL = :yarn | ||
|  | ||
| def bundler_cmd | ||
| using_bun? ? "bun" : "yarn" | ||
| TOOLS_COMMANDS.dig(package_manager, :cmd) | ||
| end | ||
|  | ||
| def bundler_run_cmd | ||
| using_bun? ? "bun run" : "yarn" | ||
| TOOLS_COMMANDS.dig(package_manager, :run) | ||
| end | ||
|  | ||
| def bundler_x_cmd | ||
| using_bun? ? "bunx" : "npx" | ||
| TOOLS_COMMANDS.dig(package_manager, :x) | ||
| end | ||
|  | ||
| def using_bun? | ||
| tool_exists?('bun') && (File.exist?('bun.lockb') || | ||
| File.exist?('bun.lock') || | ||
| File.exist?('yarn.lock')) | ||
| package_manager == :bun | ||
| end | ||
|  | ||
| def tool_exists?(tool) | ||
| system "command -v #{tool} > /dev/null" | ||
| def package_manager | ||
| @package_manager ||= tool_determined_by_config_file || tool_determined_by_executable || DEFAULT_TOOL | ||
| end | ||
|  | ||
| def add_package_json_script(name, script, run_script=true) | ||
| if using_bun? | ||
| package_json = JSON.parse(File.read("package.json")) | ||
| package_json["scripts"] ||= {} | ||
| package_json["scripts"][name] = script.gsub('\\"', '"') | ||
| File.write("package.json", JSON.pretty_generate(package_json)) | ||
| run %(bun run #{name}) if run_script | ||
| else | ||
| case `npx -v`.to_f | ||
| when 7.1...8.0 | ||
| def add_package_json_script(name, script, run_script: true) | ||
| case package_manager | ||
| when :yarn | ||
| npx_version = `npx -v`.to_f | ||
|  | ||
| if npx_version >= 7.1 && npx_version < 8.0 | ||
| say "Add #{name} script" | ||
| run %(npm set-script #{name} "#{script}") | ||
| run %(yarn #{name}) if run_script | ||
| when (8.0..) | ||
| elsif npx_version >= 8.0 | ||
| say "Add #{name} script" | ||
| run %(npm pkg set scripts.#{name}="#{script}") | ||
| run %(yarn #{name}) if run_script | ||
| else | ||
| say %(Add "scripts": { "#{name}": "#{script}" } to your package.json), :green | ||
| return | ||
| end | ||
| when :npm | ||
| say "Add #{name} script" | ||
| npx_version = `npx -v`.to_f | ||
|  | ||
| if npx_version >= 7.1 && npx_version < 8.0 | ||
| run %(npm set-script #{name} "#{script}") | ||
| else | ||
| run %(npm pkg set scripts.#{name}="#{script}") | ||
| end | ||
| when :pnpm | ||
| say "Add #{name} script" | ||
| run %(pnpm pkg set scripts.#{name}="#{script}") | ||
| when :bun | ||
| say "Add #{name} script to package.json manually" | ||
| package_json = JSON.parse(File.read("package.json")) | ||
| package_json["scripts"] ||= {} | ||
| package_json["scripts"][name] = script.gsub('\\"', '"') | ||
| File.write("package.json", JSON.pretty_generate(package_json)) | ||
| end | ||
|  | ||
| run %(#{bundler_run_cmd} #{name}) if run_script | ||
| end | ||
|  | ||
| private | ||
|  | ||
| def tool_exists?(tool) | ||
| system "command -v #{tool} > /dev/null" | ||
| end | ||
|  | ||
| def tool_determined_by_config_file | ||
| case | ||
| when File.exist?("yarn.lock") then :yarn | ||
| when File.exist?("bun.lockb") then :bun | ||
| when File.exist?("bun.lock") then :bun | ||
| when File.exist?("pnpm-lock.yaml") then :pnpm | ||
| when File.exist?("package-lock.json") then :npm | ||
| end | ||
| end | ||
|  | ||
| def tool_determined_by_executable | ||
| SUPPORTED_TOOLS.each do |tool| | ||
| return tool if tool_exists?(tool) | ||
| end | ||
| end | ||
| end | ||
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The Bun lockfiles should come before
yarn.lock, since Bun optionally generates ayarn.lockfile.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch, i update