lib/prez/files.rb in prez-0.0.1 vs lib/prez/files.rb in prez-0.0.2

- old
+ new

@@ -5,43 +5,63 @@ super "Could not find file name '#{name}' with extension '#{extension}'" end end class Paths - attr_reader :extension, :paths + attr_reader :extension_alias, :extensions, :paths - def initialize(extension, dirname) - @extension = extension + def initialize(extension_alias, extensions, dirname, options = {}) + @extension_alias = extension_alias + @extensions = extensions + @binary = options.fetch :binary, false @paths = [].tap do |paths| paths << File.expand_path(".") paths << File.expand_path(File.join(".", dirname)) paths << File.expand_path(File.join("../../../vendor", dirname), __FILE__) end end def find(name) paths.each do |path| - file = File.join path, "#{name}.#{extension}" + extensions.each do |extension| + files = [File.join(path, "#{name}.#{extension}")] - if File.exists?(file) - return file + if name.end_with?(".#{extension}") + files << File.join(path, name) + end + + files.each do |file| + if File.exists?(file) + return file + end + end end end - raise Prez::Files::MissingError.new(name, extension) + raise Prez::Files::MissingError.new(name, extension_alias) end + + def binary? + @binary + end end class << self SEARCH_PATHS = { - "js" => Prez::Files::Paths.new("js", "javascripts"), - "css" => Prez::Files::Paths.new("css", "stylesheets") + "js" => Prez::Files::Paths.new("js", ["js.coffee", "coffee", "js"], "javascripts"), + "css" => Prez::Files::Paths.new("css", ["css.scss", "scss", "css"], "stylesheets"), + "font" => Prez::Files::Paths.new("font", ["eot", "svg", "ttf", "woff", "woff2"], "fonts", binary: true) } def contents(name, extension) file = find name, extension - File.read file + + if SEARCH_PATHS[extension].binary? + File.read file, mode: "rb" + else + File.read file + end end def find(name, extension) unless SEARCH_PATHS[extension] raise Prez::Files::MissingError.new(name, extension)