class Jekyll::Typescript::Manager

manages typescript

Constants

SyntaxError

Attributes

pages[RW]
site[RW]

Public Instance Methods

add_page(page) click to toggle source

Typescript hook run after a page has been rendered. This is used to add a a page to typescripts memory if that page is needed for typescript compilation.

# File lib/jekyll/typescript/manager.rb, line 72
def add_page(page)
  @pages << page if copy_ext? page.ext
end
copy_ext?(ext) click to toggle source

whether :ext is associated with an extension tsc will need in the compilation directory, eg. .js

# File lib/jekyll/typescript/manager.rb, line 30
def copy_ext?(ext)
  typescript_ext?(ext) || copy_extensions.include?(ext)
end
copy_file?(page) click to toggle source

whether :page has a copy_file extension.

# File lib/jekyll/typescript/manager.rb, line 42
def copy_file?(page)
  copy_ext? File.extname(page.name).downcase
end
page_to_output_path(page) click to toggle source
# File lib/jekyll/typescript/manager.rb, line 76
def page_to_output_path(page)
  # TODO only change the extension of .ts files.
  File.join(temp_dir,
            File.dirname(page.relative_path),
            File.basename(page.relative_path, '.*') + '.js')
end
post_render(*args) click to toggle source

Once all the site files have been processed, compile and replace the content of any typescript files.

# File lib/jekyll/typescript/manager.rb, line 86
def post_render(*args)
  setup

  Jekyll.logger.debug('Typescript', 'clearing out temporary build directory.')
  FileUtils.rm_rf(Dir.glob(File.join(temp_dir, '*')))

  populate_temp_dir
  @pages.select.each do |page|
    next unless typescript_ext? page.ext

    command = compile_command(in_temp_dir(page.relative_path))
    Jekyll.logger.debug('Typescript') {
      "running compile command: #{Shellwords.join(command[1..])}" }
    compile_output = IO.popen(command, &:read).chomp # spawn a cmd & read process output.

    unless $?.success?
      puts compile_output
      raise SyntaxError, "typescript failed to convert: #{page.path}\n"
    end

    page.output = File.read(page_to_output_path(page))
  end
end
pre_render(site, _) click to toggle source

Typescript hook run before the site is rendered. This is used to reset the typescript pages array and to assign the site instance used for the compilation.

# File lib/jekyll/typescript/manager.rb, line 63
def pre_render(site, _)
  self.site = site
  @pages = []
end
setup() click to toggle source
# File lib/jekyll/typescript/manager.rb, line 46
def setup
  unless @setup
    FileUtils.mkdir_p(temp_dir)
    @setup = true
  end
end
static_files() click to toggle source
# File lib/jekyll/typescript/manager.rb, line 53
def static_files
  @static_files ||= []
end
typescript_ext?(ext) click to toggle source

whether :ext is associated with a typescript extension.

# File lib/jekyll/typescript/manager.rb, line 23
def typescript_ext?(ext)
  ts_extensions.include? ext
end
typescript_file?(page) click to toggle source

whether :page has a typescript extension.

# File lib/jekyll/typescript/manager.rb, line 36
def typescript_file?(page)
  typescript_ext? File.extname(page.name).downcase
end

Private Instance Methods

compile_command(*args) click to toggle source

get a tsc compile command for use with popen, append :args to the end of it.

# File lib/jekyll/typescript/manager.rb, line 137
def compile_command(*args)
  [ENV, *tsc_command, '--pretty', '--rootDir', temp_dir, *tsconfig_args, *args]
end
in_temp_dir(path) click to toggle source

return :path but from the typescript temporary directory.

# File lib/jekyll/typescript/manager.rb, line 114
def in_temp_dir(path)
  File.join(temp_dir, path)
end
populate_temp_dir() click to toggle source

copy all of the pages in pages to this plugins temporary directory.

# File lib/jekyll/typescript/manager.rb, line 120
def populate_temp_dir
  Dir.chdir(temp_dir) do
    (@pages + static_files).each do |page|
      if page.is_a?(StaticFile)
        FileUtils.mkdir_p('./' + page.instance_variable_get(:@dir))
        FileUtils.copy(site.in_source_dir(page.relative_path),
                       './' + page.relative_path)
      else
        FileUtils.mkdir_p('./' + page.dir) # make temp container for file
        File.open(page.relative_path, 'w') { |fd| fd.write(page.content) }
      end
    end
  end
end
tsconfig_args() click to toggle source

value of the tsconfig.json file as an array of command flags.

# File lib/jekyll/typescript/manager.rb, line 143
def tsconfig_args
  unless @tsconfig_args
    config_file = 'tsconfig.json'

    @tsconfig_args = if File.exist?(config_file)
                       parse_tsconfig(dumb_read_json(config_file))
                     else
                       Jekyll.logger.warn('Typescript', "no config file found at #{config_file}")
                       []
                     end
  end

  @tsconfig_args
end