Skip to content

Commit

Permalink
refactor: Config#within_root, allow skipping chdir for parallelism
Browse files Browse the repository at this point in the history
  • Loading branch information
ElMassimo committed Nov 3, 2022
1 parent 2de3682 commit 770ca43
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 3 deletions.
2 changes: 1 addition & 1 deletion vite_ruby/lib/vite_ruby/builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def last_build_path(ssr:)
# Internal: Returns a digest of all the watched files, allowing to detect
# changes, and skip Vite builds if no files have changed.
def watched_files_digest
Dir.chdir File.expand_path(config.root) do
config.within_root do
files = Dir[*config.watched_paths].reject { |f| File.directory?(f) }
file_ids = files.sort.map { |f| "#{ File.basename(f) }/#{ Digest::SHA1.file(f).hexdigest }" }
Digest::SHA1.hexdigest(file_ids.join('/'))
Expand Down
2 changes: 1 addition & 1 deletion vite_ruby/lib/vite_ruby/commands.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def verify_install

# Internal: Prints information about ViteRuby's environment.
def print_info
Dir.chdir(config.root) do
config.within_root do
$stdout.puts "bin/vite present?: #{ File.exist? 'bin/vite' }"

$stdout.puts "vite_ruby: #{ ViteRuby::VERSION }"
Expand Down
5 changes: 5 additions & 0 deletions vite_ruby/lib/vite_ruby/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ def watched_paths
].freeze
end

# Internal: Changes the current directory to the root dir.
def within_root(&block)
Dir.chdir(File.expand_path(root), &block)
end

private

# Internal: Coerces all the configuration values, in case they were passed
Expand Down
2 changes: 1 addition & 1 deletion vite_ruby/lib/vite_ruby/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def initialize(vite_ruby)

# Public: Executes Vite with the specified arguments.
def run(argv, exec: false)
Dir.chdir(config.root) {
config.within_root {
cmd = command_for(argv)
return Kernel.exec(*cmd) if exec

Expand Down

0 comments on commit 770ca43

Please sign in to comment.