class Guard::Jest::Server

Constants

CLEAR
CR

Attributes

cmd[R]
last_result[R]
options[R]
pending[R]
pid[R]
stdin[R]
stdout[R]

Public Class Methods

new(options = {}) click to toggle source
# File lib/guard/jest/server.rb, line 17
def initialize(options = {})
    reload(options)
    @work_in_progress = Concurrent::AtomicBoolean.new(false)
    @pending = Concurrent::Array.new
end

Public Instance Methods

alive?() click to toggle source
# File lib/guard/jest/server.rb, line 56
def alive?
    return false unless pid
    Process.kill(0, pid)
    return true
rescue Errno::ESRCH # "No such process"
    return false
rescue Errno::EPERM # "Operation not permitted, but it at least exists
    return true
else
    return true
end
busy?() click to toggle source
# File lib/guard/jest/server.rb, line 34
def busy?
    @work_in_progress.true?
end
failed?() click to toggle source
# File lib/guard/jest/server.rb, line 30
def failed?
    @pid && !alive?
end
reload(options) click to toggle source
# File lib/guard/jest/server.rb, line 79
def reload(options)
    @options = options
    @directory = options[:directory]
    @cmd = options[:jest_cmd] + ' --json --watch'
    @cmd << ' --silent' if options[:silent]
    @cmd << " --config #{options[:config_file]}" if options[:config_file]
    if alive?
        stop
        start
    end
    self
end
run(request) click to toggle source
# File lib/guard/jest/server.rb, line 23
def run(request)
    start unless alive?
    pending << request
    work_fifo_queue
    self
end
start() click to toggle source
# File lib/guard/jest/server.rb, line 46
def start
    @threads = []
    @work_in_progress.make_true
    @directory ? Dir.chdir(@directory) { spawn } : spawn
    @threads << Thread.new do
        @stdout.each { |line| record_result(line) }
    end
    self
end
stop() click to toggle source
# File lib/guard/jest/server.rb, line 68
def stop
    return unless alive?
    stdin.write('q')
    sleep(0.1)
    return unless alive?
    Process.kill("TERM", pid)
    sleep(0.1)
    Process.kill("KILL", pid) if alive?
    @pid = nil
end
update_snapshots() click to toggle source
# File lib/guard/jest/server.rb, line 42
def update_snapshots
    stdin.write('u')
end
wait_until_not_busy() click to toggle source
# File lib/guard/jest/server.rb, line 38
def wait_until_not_busy
    sleep(0.1) while busy?
end

Private Instance Methods

log(line) click to toggle source
# File lib/guard/jest/server.rb, line 119
def log(line)
    return if options[:silent] || line =~ IGNORED_LINES
    line.sub!(CLEAR, '') # stop Jest from clearing console history
    Jest.logger.info(line.chomp)
end
record_result(line) click to toggle source
# File lib/guard/jest/server.rb, line 125
def record_result(line)
    # looks vaguely jsonish if it starts with {"

    unless line =~ /\{\"numFailedTestSuites\"\:\d+/
        log(line)
        return
    end

    begin
        line.sub!(/\A.*?\{/, '{')
        line.sub!(/\}.*?\z/, '}')
        json = JSON.parse(line)
        result = @pending.pop
        if result
            stdin.write('o') # revert back to watching changed files
        else
            result = RunRequest.new
        end
        result.satisfy(json)
        @work_in_progress.make_false
        work_fifo_queue
    rescue e
        Jest.logger.warn e.class.to_s + ": " + e.message
        puts e.backtrace.join("\n")
    end
end
run_all() click to toggle source
# File lib/guard/jest/server.rb, line 104
def run_all
    @work_in_progress.make_true
    stdin.write('a')
end
run_paths(paths) click to toggle source
# File lib/guard/jest/server.rb, line 109
def run_paths(paths)
    @work_in_progress.make_true
    stdin.write('p')
    # the sleep values simply "seem to work ok" and may need refinement
    sleep(0.1)
    stdin.write(paths.join('|'))
    sleep(0.1)
    stdin.write(CR)
end
spawn() click to toggle source
# File lib/guard/jest/server.rb, line 152
def spawn
    Jest.logger.info "Starting Jest with #{cmd}"
    @stdout, @stdin, @pid = PTY.spawn(cmd)
end
work_fifo_queue() click to toggle source
# File lib/guard/jest/server.rb, line 94
def work_fifo_queue
    return if busy? || pending.none?
    request = pending.first
    if request.all?
        run_all
    else
        run_paths(request.paths)
    end
end