lib/archival/builder.rb in archival-0.0.8 vs lib/archival/builder.rb in archival-0.0.9

- old
+ new

@@ -1,21 +1,27 @@ # frozen_string_literal: true require 'liquid' require 'tomlrb' require 'redcarpet' +require 'fileutils' +require 'tags/asset' module Archival class DuplicateKeyError < StandardError end + class DuplicateStaticFileError < StandardError + end + class Builder attr_reader :page_templates def initialize(config, *_args) @config = config refresh_config + Asset.helper_port = @config.helper_port end def pages_dir File.join(@config.root, @config.pages_dir) end @@ -219,16 +225,42 @@ # in production (or init), also copy all assets to the dist folder. # in dev, they will be copied as they change. @config.assets_dirs.each do |asset_dir| asset_path = File.join(@config.build_dir, asset_dir) - next if @config.dev_mode || !File.exist?(asset_path) + next if @config.dev_mode && File.exist?(asset_path) - FileUtils.copy_entry File.join(@config.root, asset_dir), asset_path + source_path = File.join(@config.root, asset_dir) + next unless File.exist?(source_path) + + FileUtils.copy_entry source_path, asset_path end + + copy_static end private + + def copy_static + static_dir = File.join(@config.root, @config.static_dir) + + # same for the static dir, but just the content. + return unless File.exist?(static_dir) + + copied_static_files = Set[Dir.children(@config.build_dir)] + Dir.children(static_dir).each do |child| + raise DuplicateStaticFileError if copied_static_files.include?(child) + + copied_static_files << child + asset_path = File.join(@config.build_dir, child) + next if @config.dev_mode && File.exist?(asset_path) + + source_path = File.join(static_dir, child) + next unless File.exist?(source_path) + + FileUtils.copy_entry source_path, asset_path + end + end def dev_mode_content "<script src=\"http://localhost:#{@config.helper_port}/js/archival-helper.js\" type=\"application/javascript\"></script>" # rubocop:disable Layout/LineLength end end