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
37
38
39
40
41
42
# 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) ⇒ 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



67
68
69
# 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) ⇒ 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



75
76
77
# 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) ⇒ 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



59
60
61
# 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) ⇒ 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



48
49
50
51
52
53
# 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) ⇒ 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