module SassC::ImportHandler::FileSystemImporter
def exactly_one(paths)
def exactly_one(paths) return if paths.empty? return paths.first if paths.one? raise "It's not clear which file to import. Found:\n#{paths.map { |path| " #{path}" }.join("\n")}" end
def resolve_path(path, from_import)
def resolve_path(path, from_import) ext = File.extname(path) if ['.sass', '.scss', '.css'].include?(ext) if from_import result = exactly_one(try_path("#{without_ext(path)}.import#{ext}")) return result unless result.nil? end return exactly_one(try_path(path)) end if from_import result = exactly_one(try_path_with_ext("#{path}.import")) return result unless result.nil? end result = exactly_one(try_path_with_ext(path)) return result unless result.nil? try_path_as_dir(path, from_import) end
def try_path(path)
def try_path(path) partial = File.join(File.dirname(path), "_#{File.basename(path)}") result = [] result.push(partial) if File.file?(partial) result.push(path) if File.file?(path) result end
def try_path_as_dir(path, from_import)
def try_path_as_dir(path, from_import) return unless File.directory?(path) if from_import result = exactly_one(try_path_with_ext(File.join(path, 'index.import'))) return result unless result.nil? end exactly_one(try_path_with_ext(File.join(path, 'index'))) end
def try_path_with_ext(path)
def try_path_with_ext(path) result = try_path("#{path}.sass") + try_path("#{path}.scss") result.empty? ? try_path("#{path}.css") : result end
def without_ext(path)
def without_ext(path) ext = File.extname(path) path.delete_suffix(ext) end