class Catalyst::Builder

Constants

BUILD_COMMAND

Public Instance Methods

build!(environment = nil) click to toggle source
# File lib/catalyst/builder.rb, line 19
def build!(environment = nil)
  Catalyst.check_for_catalyst!

  environment ||= Catalyst.config.environment

  case environment
  when :test
    test_build!
  when :production
    production_build!
  else
    raise ArgumentError,
          'Invalid environment. Must be one of: :test, :production.'
  end
end

Private Instance Methods

asset_paths() click to toggle source
# File lib/catalyst/builder.rb, line 88
def asset_paths
  if ::Catalyst::Config.context_path
    Dir.glob(File.join(::Catalyst::Config.context_path, '**/*.{js,ts,tsx,scss}')) + [
      File.join(Dir.pwd, 'package.json'),
      File.join(Dir.pwd, 'yarn.lock'),
      File.join(Dir.pwd, 'catalyst.config.json')
    ]
  else
    []
  end
end
assets_last_built() click to toggle source
# File lib/catalyst/builder.rb, line 106
def assets_last_built
  if assets_last_built_file_path.nil?
    Time.at(0)
  else
    File.mtime(assets_last_built_file_path) rescue Time.at(0)
  end
end
assets_last_built_file_path() click to toggle source
# File lib/catalyst/builder.rb, line 100
def assets_last_built_file_path
  if defined?(Rails)
    Rails.root.join('tmp/assets-last-built')
  end
end
assets_last_modified() click to toggle source
# File lib/catalyst/builder.rb, line 80
def assets_last_modified
  asset_paths.lazy.select do |path|
    File.exists?(path)
  end.map do |path|
    File.ctime(path)
  end.max || Time.now
end
production_build!() click to toggle source
# File lib/catalyst/builder.rb, line 72
def production_build!
  unless system("NODE_ENV=production #{BUILD_COMMAND}")
    Catalyst.log('Failed to compile assets!')

    exit 1
  end
end
test_build!() click to toggle source
# File lib/catalyst/builder.rb, line 37
    def test_build!
      unless Catalyst.config.running_feature_tests.call
        Catalyst.log('Not running feature tests -- skipping build')
        return
      end

      # Only rebuild the assets if they've been modified since the last time
      # they were built successfully.
      if assets_last_built >= assets_last_modified
        Catalyst.log('Assets have not been modified -- skipping build')
        return
      end

      if system("NODE_ENV=test #{BUILD_COMMAND}")
        unless assets_last_built_file_path.nil?
          begin
            FileUtils.touch(assets_last_built_file_path)
          rescue Errno::ENOENT
          end
        end
      else
        puts <<~MESSAGE
          \e[31m
          ***************************************************************
          *                                                             *
          *          Failed to compile assets with Catalyst!            *
          *  Make sure 'yarn run catalyst build' runs without failing.  *
          *                                                             *
          ***************************************************************\e[0m
        MESSAGE

        exit 1
      end
    end