Module: ViteRuby::CLI::FileUtils

Defined in:
lib/vite_ruby/cli/file_utils.rb

Overview

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.

Class Method Summary collapse

Class Method Details

.append(path, contents) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Adds a new line at the bottom of the file.

Since:

  • 1.2.11



34
35
36
# File 'lib/vite_ruby/cli/file_utils.rb', line 34

def append(path, contents)
  append_unless_present(path, contents, pattern: contents)
end

.append_unless_present(path, contents, pattern:) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Adds a new line at the bottom of the file, unless any line matches the pattern.



41
42
43
44
45
46
47
48
49
# File 'lib/vite_ruby/cli/file_utils.rb', line 41

def append_unless_present(path, contents, pattern:)
  content = read_lines(path)
  return if content.any? { |line| line.include?(pattern) }

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

  write(path, content)
end

.cp(source, destination) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Copies source into destination.

Since:

  • 1.2.11



25
26
27
28
# 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) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Inject contents in path after target.

Since:

  • 1.2.11



74
75
76
# File 'lib/vite_ruby/cli/file_utils.rb', line 74

def inject_line_after(path, target, contents)
  _inject_line_after(path, target, contents, method(:index))
end

.inject_line_after_last(path, target, contents) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Inject contents in path after last target.

Since:

  • 1.2.11



82
83
84
# File 'lib/vite_ruby/cli/file_utils.rb', line 82

def inject_line_after_last(path, target, contents)
  _inject_line_after(path, target, contents, method(:rindex))
end

.inject_line_before(path, target, contents) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Inject contents in path before target.

Since:

  • 1.2.11



66
67
68
# File 'lib/vite_ruby/cli/file_utils.rb', line 66

def inject_line_before(path, target, contents)
  _inject_line_before(path, target, contents, method(:index))
end

.replace_first_line(path, target, replacement) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Replace first line in path that contains target with replacement.

Since:

  • 1.2.11



55
56
57
58
59
60
# File 'lib/vite_ruby/cli/file_utils.rb', line 55

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) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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

Since:

  • 1.2.11



14
15
16
17
18
19
# 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