Module: FormatStaged::IOMixin

Included in:
Job
Defined in:
lib/format-staged/io.rb

Overview

Mixin that provides IO methods

Instance Method Summary collapse

Instance Method Details

#fail!(message) ⇒ Object



61
62
63
# File 'lib/format-staged/io.rb', line 61

def fail!(message)
  abort "đŸ’Ŗ  #{message.red}"
end

#get_output(*args, lines: true, silent: false) ⇒ Object



9
10
11
12
13
14
15
16
17
18
# File 'lib/format-staged/io.rb', line 9

def get_output(*args, lines: true, silent: false)
  puts "> #{args.join(' ')}" if @verbose

  r = IO.popen(args, err: :err)
  output = read_output(r, lines: lines, silent: silent)

  raise 'Failed to run command' unless $CHILD_STATUS.success?

  output
end

#get_status(*args) ⇒ Object



20
21
22
23
24
25
26
27
28
29
# File 'lib/format-staged/io.rb', line 20

def get_status(*args)
  puts "> #{args.join(' ')}" if @verbose
  result = system(*args)

  raise 'Failed to run command' if result.nil?

  puts "? #{$CHILD_STATUS.exitstatus}" if @verbose

  $CHILD_STATUS.exitstatus
end

#info(message) ⇒ Object



69
70
71
# File 'lib/format-staged/io.rb', line 69

def info(message)
  puts message.blue
end

#pipe_command(*args, source: nil) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/format-staged/io.rb', line 31

def pipe_command(*args, source: nil)
  puts (source.nil? ? '> ' : '| ') + args.join(' ') if @verbose
  r, w = IO.pipe

  opts = {}
  opts[:in] = source unless source.nil?
  opts[:out] = w
  opts[:err] = :err

  pid = spawn(*args, **opts)

  w.close
  source&.close

  [pid, r]
end

#read_output(output, lines: true, silent: false) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/format-staged/io.rb', line 48

def read_output(output, lines: true, silent: false)
  result = output.read
  splits = result.split("\n")
  if @verbose && !silent
    splits.each do |line|
      puts "< #{line}"
    end
  end
  output.close

  lines ? splits : result
end

#verbose_info(message) ⇒ Object



73
74
75
# File 'lib/format-staged/io.rb', line 73

def verbose_info(message)
  puts "â„šī¸  #{message}" if verbose
end

#warning(message) ⇒ Object



65
66
67
# File 'lib/format-staged/io.rb', line 65

def warning(message)
  warn "âš ī¸  #{message.yellow}"
end