class ViteRuby::CLI::Install

Constants

TEMPLATES_PATH

Public Instance Methods

call(**) click to toggle source
# File lib/vite_ruby/cli/install.rb, line 8
def call(**)
  $stdout.sync = true

  say 'Creating binstub'
  ViteRuby.commands.install_binstubs

  say 'Creating configuration files'
  create_configuration_files

  say 'Installing sample files'
  install_sample_files

  say 'Installing js dependencies'
  install_js_dependencies

  say 'Adding files to .gitignore'
  install_gitignore

  say "\nVite ⚡️ Ruby successfully installed! 🎉"
end

Protected Instance Methods

install_sample_files() click to toggle source

Internal: Create a sample JS file and attempt to inject it in an HTML template.

# File lib/vite_ruby/cli/install.rb, line 49
def install_sample_files
  copy_template 'entrypoints/application.js', to: config.resolved_entrypoints_dir.join('application.js')
end
js_dependencies() click to toggle source

Internal: The JS packages that should be added to the app.

# File lib/vite_ruby/cli/install.rb, line 32
def js_dependencies
  [
    "vite@#{ ViteRuby::DEFAULT_VITE_VERSION }",
    "vite-plugin-ruby@#{ ViteRuby::DEFAULT_PLUGIN_VERSION }",
  ]
end
setup_app_files() click to toggle source

Internal: Setup for a plain Rack application.

# File lib/vite_ruby/cli/install.rb, line 40
def setup_app_files
  copy_template 'config/vite.json', to: config.config_path

  if (rackup_file = root.join('config.ru')).exist?
    inject_line_after_last rackup_file, 'require', 'use(ViteRuby::DevServerProxy, ssl_verify_none: true) if ViteRuby.run_proxy?'
  end
end

Private Instance Methods

copy_template(path, to:) click to toggle source
# File lib/vite_ruby/cli/install.rb, line 67
def copy_template(path, to:)
  cp TEMPLATES_PATH.join(path), to
end
create_configuration_files() click to toggle source

Internal: Creates the Vite and vite-plugin-ruby configuration files.

# File lib/vite_ruby/cli/install.rb, line 72
def create_configuration_files
  copy_template 'config/vite.config.ts', to: root.join('vite.config.ts')
  append root.join('Procfile.dev'), 'vite: bin/vite dev'
  setup_app_files
  ViteRuby.reload_with(config_path: config.config_path)
end
install_gitignore() click to toggle source

Internal: Adds compilation output dirs to git ignore.

# File lib/vite_ruby/cli/install.rb, line 88
  def install_gitignore
    return unless (gitignore_file = root.join('.gitignore')).exist?

    append(gitignore_file, <<~GITIGNORE)

      # Vite Ruby
      /public/vite
      /public/vite-dev
      /public/vite-test
      node_modules
      *.local
      .DS_Store
    GITIGNORE
  end
install_js_dependencies() click to toggle source

Internal: Installs vite and vite-plugin-ruby at the project level.

# File lib/vite_ruby/cli/install.rb, line 80
def install_js_dependencies
  package_json = root.join('package.json')
  write(package_json, '{}') unless package_json.exist?
  deps = js_dependencies.join(' ')
  run_with_capture("#{ npm_install } -D #{ deps }", stdin_data: "\n")
end
npm_install() click to toggle source

Internal: Support all popular package managers.

# File lib/vite_ruby/cli/install.rb, line 120
def npm_install
  return 'yarn add' if root.join('yarn.lock').exist?
  return 'pnpm install' if root.join('pnpm-lock.yaml').exist?

  'npm install'
end
root() click to toggle source

Internal: The root path for the Ruby application.

# File lib/vite_ruby/cli/install.rb, line 104
def root
  @root ||= silent_warnings { config.root }
end
run_with_capture(*args, **options) click to toggle source
# File lib/vite_ruby/cli/install.rb, line 112
def run_with_capture(*args, **options)
  Dir.chdir(root) do
    _, stderr, status = ViteRuby::IO.capture(*args, **options)
    say(stderr) unless status.success? || stderr.to_s.empty?
  end
end
say(*args) click to toggle source
# File lib/vite_ruby/cli/install.rb, line 108
def say(*args)
  $stdout.puts(*args)
end
silent_warnings() { || ... } click to toggle source

Internal: Avoid printing warning about missing vite.json, we will create one.

# File lib/vite_ruby/cli/install.rb, line 128
def silent_warnings
  old_stderr = $stderr
  $stderr = StringIO.new
  yield
ensure
  $stderr = old_stderr
end