Class: SvelteOnRails::Installer::NpmSandbox

Inherits:
Object
  • Object
show all
Defined in:
lib/svelte_on_rails/installer/npm_sandbox.rb

Defined Under Namespace

Classes: Result

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(packages, project_root:, sandboxes_root:, title:) ⇒ NpmSandbox

Returns a new instance of NpmSandbox.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/svelte_on_rails/installer/npm_sandbox.rb', line 29

def initialize(packages, project_root:, sandboxes_root:, title:)

  @packages = Array(packages).map(&:to_s).reject(&:empty?)
  @project_root = project_root

  Dir.mkdir(sandboxes_root) unless sandboxes_root.exist?
  next_index = Dir.children(sandboxes_root).filter_map { |name| name[/\Anpm-(\d+)\z/, 1]&.to_i }.max.to_i + 1
  @tmpdir = sandboxes_root.join("npm-#{next_index}")
  @tmpdir.mkpath
  @utils = Utils

  puts "[Sandbox test] #{title}: #{@tmpdir} (#{packages.length} packages)"
  copy_package_files
end

Class Method Details

.call(packages, project_root: Dir.pwd) ⇒ Object

Backward-compatible alias; prefer .dry_run for clarity.



25
26
27
# File 'lib/svelte_on_rails/installer/npm_sandbox.rb', line 25

def self.call(packages, project_root: Dir.pwd)
  dry_run(packages, project_root: project_root)
end

.dry_run(packages, project_root: Dir.pwd, sandboxes_root:, title:) ⇒ Object



20
21
22
# File 'lib/svelte_on_rails/installer/npm_sandbox.rb', line 20

def self.dry_run(packages, project_root: Dir.pwd, sandboxes_root: , title:)
  new(packages, project_root: project_root, sandboxes_root: sandboxes_root, title: title).dry_run
end

Instance Method Details

#dry_runObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/svelte_on_rails/installer/npm_sandbox.rb', line 44

def dry_run
  passed = []
  failed = []
  errors = {}

  return Result.new(success: true, passed: [], failed: [], tmpdir: @tmpdir, errors: {}).to_h if @packages.empty?

  @packages.each_with_index do |pkg, i|
    ok, stderr = test_package(pkg)
    if ok
      passed << pkg
      puts "[Sandbox test]   ✓ #{pkg} (#{i + 1}/#{@packages.length})"
    else
      failed << pkg
      errors[pkg] = stderr
      puts "[Sandbox test]   ✗ #{pkg} (#{i + 1}/#{@packages.length})"
      break
    end
  end

  if failed.empty?
    puts "[Sandbox test]     verify vite build..."
    b = vite_build
    if b[:success]
      puts "[Sandbox test]   ✓ Vite build passed"
      @vite_build_passed = true
    else
      puts "[Sandbox test]   ✗ Vite build failed:"
      puts '-'*80
      puts b[:error]
    end
  end

  Result.new(
    success: failed.empty? && @vite_build_passed,
    passed: passed,
    failed: failed,
    errors: errors
  ).to_h
end