Class: Discharger::SetupRunner::Commands::YarnCommand

Inherits:
BaseCommand
  • Object
show all
Defined in:
lib/discharger/setup_runner/commands/yarn_command.rb

Instance Attribute Summary

Attributes inherited from BaseCommand

#app_root, #config, #logger

Instance Method Summary collapse

Methods inherited from BaseCommand

#initialize

Constructor Details

This class inherits a constructor from Discharger::SetupRunner::Commands::BaseCommand

Instance Method Details

#can_execute?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/discharger/setup_runner/commands/yarn_command.rb', line 55

def can_execute?
  File.exist?(File.join(app_root, "package.json"))
end

#descriptionObject



59
60
61
# File 'lib/discharger/setup_runner/commands/yarn_command.rb', line 59

def description
  "Install JavaScript dependencies"
end

#executeObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/discharger/setup_runner/commands/yarn_command.rb', line 9

def execute
  log "Installing Node modules"

  # Enable corepack if yarn.lock exists (Yarn 2+)
  if File.exist?(File.join(app_root, "yarn.lock"))
    if system_quiet("which corepack")
      system! "corepack enable"

      package_json_path = File.join(app_root, "package.json")
      if File.exist?(package_json_path)
        begin
          require "json"
          package_json = JSON.parse(File.read(package_json_path))

          if package_json["packageManager"]&.start_with?("yarn@")
            yarn_spec = package_json["packageManager"].split("+").first
            log "Using #{yarn_spec} from package.json"
            system! "corepack use #{yarn_spec}"
          else
            system! "corepack use yarn@stable"
          end
        rescue JSON::ParserError => e
          log "Warning: Could not parse package.json: #{e.message}"
          system! "corepack use yarn@stable"
        end
      else
        system! "corepack use yarn@stable"
      end
    end

    # Install dependencies
    system_quiet("yarn check --check-files > /dev/null 2>&1") || system!("yarn install")
  elsif File.exist?(File.join(app_root, "package-lock.json"))
    # NPM project
    log "Found package-lock.json, using npm"
    system! "npm ci"
  elsif File.exist?(File.join(app_root, "package.json"))
    # Generic package.json - try yarn first, fall back to npm
    if system_quiet("which yarn")
      system! "yarn install"
    else
      system! "npm install"
    end
  end
end