Class: ViteRuby::Commands

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/vite_ruby/commands.rb

Overview

Public: Encapsulates common tasks, available both programatically and from the CLI and Rake tasks.

Instance Method Summary collapse

Constructor Details

#initialize(vite_ruby) ⇒ Commands

Returns a new instance of Commands.



6
7
8
# File 'lib/vite_ruby/commands.rb', line 6

def initialize(vite_ruby)
  @vite_ruby = vite_ruby
end

Instance Method Details

#build(*args) ⇒ Object

Public: Builds all assets that are managed by Vite, from the entrypoints.



20
21
22
# File 'lib/vite_ruby/commands.rb', line 20

def build(*args)
  builder.build(*args).tap { manifest.refresh }
end

#build_from_task(*args) ⇒ Object

Public: Defaults to production, and exits if the build fails.



11
12
13
14
15
16
17
# File 'lib/vite_ruby/commands.rb', line 11

def build_from_task(*args)
  with_node_env(ENV.fetch("NODE_ENV", "production")) {
    ensure_log_goes_to_stdout {
      build(*args) || exit!
    }
  }
end

#clobberObject

Public: Removes all build cache and previously compiled assets.



25
26
27
28
29
# File 'lib/vite_ruby/commands.rb', line 25

def clobber
  dirs = [config.build_output_dir, config.ssr_output_dir, config.build_cache_dir, config.vite_cache_dir]
  dirs.each { |dir| dir.rmtree if dir.exist? }
  $stdout.puts "Removed vite cache and output dirs:\n\t#{dirs.join("\n\t")}"
end

#install_binstubsObject

Internal: Installs the binstub for the CLI in the appropriate path.



32
33
34
35
# File 'lib/vite_ruby/commands.rb', line 32

def install_binstubs
  `bundle binstub vite_ruby --path #{config.root.join("bin")}`
  `bundle config --delete bin`
end

#legacy_npm_version?Boolean

Internal: Checks if the npm version is 6 or lower.

Returns:

  • (Boolean)


38
39
40
# File 'lib/vite_ruby/commands.rb', line 38

def legacy_npm_version?
  `npm --version`.to_i < 7 rescue false
end

#legacy_yarn_version?Boolean

Internal: Checks if the yarn version is 1.x.

Returns:

  • (Boolean)


43
44
45
# File 'lib/vite_ruby/commands.rb', line 43

def legacy_yarn_version?
  `yarn --version`.to_i < 2 rescue false
end

Internal: Prints information about ViteRuby’s environment.



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/vite_ruby/commands.rb', line 70

def print_info
  config.within_root do
    $stdout.puts "bin/vite present?: #{File.exist? "bin/vite"}"

    $stdout.puts "vite_ruby: #{ViteRuby::VERSION}"
    ViteRuby.framework_libraries.each do |framework, library|
      $stdout.puts "#{library.name}: #{library.version}"
      $stdout.puts "#{framework}: #{Gem.loaded_specs[framework]&.version}"
    end

    $stdout.puts "ruby: #{`ruby --version`}"
    $stdout.puts "node: #{`node --version`}"

    pkg = config.package_manager
    $stdout.puts "#{pkg}: #{`#{pkg} --version` rescue nil}"

    $stdout.puts "\n"
    packages = `npm ls vite vite-plugin-ruby`
    packages_msg = packages.include?("vite@") ? "installed packages:\n#{packages}" : "❌ Check that vite and vite-plugin-ruby have been added as development dependencies and installed."
    $stdout.puts packages_msg

    ViteRuby::CompatibilityCheck.verify_plugin_version(config.root)
  end
end

#verify_installObject

Internal: Verifies if ViteRuby is properly installed.



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/vite_ruby/commands.rb', line 48

def verify_install
  unless File.exist?(config.root.join("bin/vite"))
    warn <<~WARN

      vite binstub not found.
      Have you run `bundle binstub vite_ruby`?
      Make sure the bin directory and bin/vite are not included in .gitignore
    WARN
  end

  config_path = config.root.join(config.config_path)
  unless config_path.exist?
    warn <<~WARN

      Configuration #{config_path} file for vite-plugin-ruby not found.
      Make sure `bundle exec vite install` has run successfully before running dependent tasks.
    WARN
    exit!
  end
end