class ViteRuby::Commands

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

Public Class Methods

new(vite_ruby) click to toggle source
# File lib/vite_ruby/commands.rb, line 6
def initialize(vite_ruby)
  @vite_ruby = vite_ruby
end

Public Instance Methods

build(*args) click to toggle source

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

# File lib/vite_ruby/commands.rb, line 20
def build(*args)
  builder.build(*args).tap { manifest.refresh }
end
build_from_task(*args) click to toggle source

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

# 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
clean(keep_up_to: 2, age_in_seconds: 3600) click to toggle source

Public: Cleanup old assets in the output directory.

keep_up_to - Max amount of backups to preserve. age_in_seconds - Amount of time to look back in order to preserve them.

NOTE: By default keeps the last version, or 2 if created in the past hour.

Examples:

To force only 1 backup to be kept: clean(1, 0)
To only keep files created within the last 10 minutes: clean(0, 600)
# File lib/vite_ruby/commands.rb, line 48
def clean(keep_up_to: 2, age_in_seconds: 3600)
  return false unless may_clean?

  versions
    .each_with_index
    .drop_while { |(mtime, _), index|
      max_age = [0, Time.now - Time.at(mtime)].max
      max_age < age_in_seconds || index < keep_up_to
    }
    .each do |(_, files), _|
      clean_files(files)
    end
  true
end
clean_from_task(args) click to toggle source

Public: Receives arguments from a rake task.

# File lib/vite_ruby/commands.rb, line 32
def clean_from_task(args)
  ensure_log_goes_to_stdout {
    clean(keep_up_to: Integer(args.keep || 2), age_in_seconds: Integer(args.age || 3600))
  }
end
clobber() click to toggle source

Public: Removes all build cache and previously compiled assets.

# File lib/vite_ruby/commands.rb, line 25
def clobber
  dirs = [config.build_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_binstubs() click to toggle source

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

# File lib/vite_ruby/commands.rb, line 64
def install_binstubs
  `bundle binstub vite_ruby --path #{ config.root.join('bin') }`
end
print_info() click to toggle source

Internal: Prints information about ViteRuby's environment.

verify_install() click to toggle source

Internal: Verifies if ViteRuby is properly installed.

# File lib/vite_ruby/commands.rb, line 69
  def verify_install
    unless File.exist?(config.root.join('bin/vite'))
      warn <<~WARN

        vite binstub not found.
        Have you run `bundle binstub vite`?
        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

Private Instance Methods

clean_files(files) click to toggle source
# File lib/vite_ruby/commands.rb, line 126
def clean_files(files)
  files.select { |file| File.file?(file) }.each do |file|
    File.delete(file)
    logger.info("Removed #{ file }")
  end
end
current_version_files() click to toggle source
# File lib/vite_ruby/commands.rb, line 141
def current_version_files
  Dir.glob(manifest.refresh.values.map { |value| config.build_output_dir.join("#{ value['file'] }*") })
end
ensure_log_goes_to_stdout() { || ... } click to toggle source
# File lib/vite_ruby/commands.rb, line 153
def ensure_log_goes_to_stdout
  old_logger, original_sync = logger, $stdout.sync

  $stdout.sync = true
  self.logger = Logger.new($stdout, formatter: proc { |_, _, progname, msg| progname == 'vite' ? msg : "#{ msg }\n" })
  yield
ensure
  self.logger, $stdout.sync = old_logger, original_sync
end
may_clean?() click to toggle source
# File lib/vite_ruby/commands.rb, line 122
def may_clean?
  config.build_output_dir.exist? && config.manifest_path.exist?
end
versions() click to toggle source
# File lib/vite_ruby/commands.rb, line 133
def versions
  all_files = Dir.glob("#{ config.build_output_dir }/**/*")
  entries = all_files - [config.manifest_path] - current_version_files
  entries.reject { |file| File.directory?(file) }
    .group_by { |file| File.mtime(file).utc.to_i }
    .sort.reverse
end
with_node_env(env) { || ... } click to toggle source
# File lib/vite_ruby/commands.rb, line 145
def with_node_env(env)
  original = ENV['NODE_ENV']
  ENV['NODE_ENV'] = env
  yield
ensure
  ENV['NODE_ENV'] = original
end