module ViteRuby::CLI::FileUtils

NOTE: Extracted from dry-cli version 0.6.0, which later removed this file as it was refactored and extracted into the more complete (and complex) dry-files.

Public Class Methods

append(path, contents) click to toggle source

Adds a new line at the bottom of the file.

@since 1.2.11 @api private

# File lib/vite_ruby/cli/file_utils.rb, line 34
def append(path, contents)
  content = read_lines(path)
  return if content.join.include?(contents)

  content << "\n" unless content.last&.end_with?("\n")
  content << "#{ contents }\n"

  write(path, content)
end
cp(source, destination) click to toggle source

Copies source into destination.

@since 1.2.11 @api private

# File lib/vite_ruby/cli/file_utils.rb, line 25
def cp(source, destination)
  mkdir_p(destination)
  FileUtils.cp(source, destination) unless File.exist?(destination)
end
inject_line_after(path, target, contents) click to toggle source

Inject `contents` in `path` after `target`.

@since 1.2.11 @api private

# File lib/vite_ruby/cli/file_utils.rb, line 67
def inject_line_after(path, target, contents)
  _inject_line_after(path, target, contents, method(:index))
end
inject_line_after_last(path, target, contents) click to toggle source

Inject `contents` in `path` after last `target`.

@since 1.2.11 @api private

# File lib/vite_ruby/cli/file_utils.rb, line 75
def inject_line_after_last(path, target, contents)
  _inject_line_after(path, target, contents, method(:rindex))
end
inject_line_before(path, target, contents) click to toggle source

Inject `contents` in `path` before `target`.

@since 1.2.11 @api private

# File lib/vite_ruby/cli/file_utils.rb, line 59
def inject_line_before(path, target, contents)
  _inject_line_before(path, target, contents, method(:index))
end
replace_first_line(path, target, replacement) click to toggle source

Replace first line in `path` that contains `target` with `replacement`.

@since 1.2.11 @api private

# File lib/vite_ruby/cli/file_utils.rb, line 48
def replace_first_line(path, target, replacement)
  content = read_lines(path)
  content[index(content, path, target)] = "#{ replacement }\n"

  write(path, content)
end
write(path, *content) click to toggle source

Creates a new file or rewrites the contents of an existing file.

@since 1.2.11 @api private

# File lib/vite_ruby/cli/file_utils.rb, line 14
def write(path, *content)
  mkdir_p(path)
  File.open(path, File::CREAT | File::WRONLY | File::TRUNC) do |file|
    file.write(Array(content).flatten.join)
  end
end

Private Class Methods

_inject_line_after(path, target, contents, finder) click to toggle source

@since 1.2.11 @api private

# File lib/vite_ruby/cli/file_utils.rb, line 122
def _inject_line_after(path, target, contents, finder)
  content = read_lines(path)
  return if content.join.include?(contents)

  i = finder.call(content, path, target)

  content.insert(i + 1, "#{ contents }\n")
  write(path, content)
end
_inject_line_before(path, target, contents, finder) click to toggle source

@since 1.2.11 @api private

# File lib/vite_ruby/cli/file_utils.rb, line 110
def _inject_line_before(path, target, contents, finder)
  content = read_lines(path)
  return if content.join.include?(contents)

  i = finder.call(content, path, target)

  content.insert(i, "#{ contents }\n")
  write(path, content)
end
index(content, path, target) click to toggle source

@since 1.2.11 @api private

# File lib/vite_ruby/cli/file_utils.rb, line 96
def index(content, path, target)
  content.index { |line| line.include?(target) } ||
    raise(ArgumentError, "Cannot find `#{ target }' inside `#{ path }'.")
end
mkdir_p(path) click to toggle source

Creates all parent directories for the given file path.

@since 1.2.11 @api private

# File lib/vite_ruby/cli/file_utils.rb, line 85
def mkdir_p(path)
  Pathname.new(path).dirname.mkpath
end
read_lines(path) click to toggle source

Returns an array with lines in the specified file, empty if it doesn't exist.

# File lib/vite_ruby/cli/file_utils.rb, line 90
def read_lines(path)
  File.exist?(path) ? File.readlines(path) : []
end
rindex(content, path, target) click to toggle source

@since 1.2.11 @api private

# File lib/vite_ruby/cli/file_utils.rb, line 103
def rindex(content, path, target)
  content.rindex { |line| line.include?(target) } ||
    raise(ArgumentError, "Cannot find `#{ target }' inside `#{ path }'.")
end