Sha256: 44186c54ea4ee73545e661e4a433c7df10b46bc7aac162bdebf1cfe674516015

Contents?: true

Size: 1.48 KB

Versions: 2

Compression:

Stored size: 1.48 KB

Contents

#!/usr/bin/env ruby

# frozen_string_literal: true

require 'listen'
require 'optparse'

require_relative '../lib/living_document.rb'

parser = OptionParser.new
parser.on('-v', '--version', 'Print the version') do
  puts(LivingDocument::VERSION)
  exit(0)
end
parser.parse!

# rubocop:disable Style/TopLevelMethodDefinition
def evaluate_code_and_update_source_file(file_path)
  puts('Running code...')
  code_in_file = File.read(file_path)
  code_to_write =
    LivingDocument::DocumentEvaluator.new(
      document: code_in_file,
    ).evaluated_document

  $printed_objects_last_run = []
  puts("Writing file! #{Time.now}")
  File.write(file_path, code_to_write)
end
# rubocop:enable Style/TopLevelMethodDefinition

file_path = ARGV[0]

if file_path.nil?
  puts('You must provide a file path argument, e.g. `livdoc personal/ruby.rb`.')
  exit(1)
end

last_file_update = Time.now
listener =
  Listen.to(Dir.pwd, only: /\A#{Regexp.escape(file_path)}\z/) do |_modified, _added, _removed|
    # Don't enter infinitely recursive loop, since the code below triggers file updates.
    # After file has been updated, wait at least 0.5 seconds before listener processes again.
    next if (Time.now - last_file_update) < 0.5

    evaluate_code_and_update_source_file(file_path)

    last_file_update = Time.now
  end

listener.start
system('clear')
evaluate_code_and_update_source_file(file_path)
puts('Waiting for file changes...')

at_exit do
  print('Stopping listener ... ')
  listener.stop
  puts('done.')
end

sleep

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
living_document-1.1.1 exe/livdoc
living_document-1.1.0 exe/livdoc