lib/js_dependency.rb in js_dependency-0.1.1 vs lib/js_dependency.rb in js_dependency-0.2.0

- old
+ new

@@ -1,10 +1,11 @@ # frozen_string_literal: true require_relative "js_dependency/version" require_relative "js_dependency/index_creator" require_relative "js_dependency/mermaid/root" +require_relative "js_dependency/cli" require "pathname" module JsDependency class Error < StandardError; end @@ -36,9 +37,45 @@ children_paths(target_pathname, child_analyze_level, index) do |parent_path, child_path| root.add(parent_path, child_path) end output = root.export(name_level: name_level) + output_pathname&.write(output) + output + end + + def self.parents(src_path, target_path, alias_paths: nil, parent_analyze_level: 1, output_path: nil) + output_pathname = Pathname.new(output_path) if output_path + index = JsDependency::IndexCreator.call(src_path, alias_paths: alias_paths) + + target_pathname = if Pathname.new(target_path).relative? && Pathname.new(target_path).exist? + Pathname.new(target_path).realpath + else + Pathname.new(target_path) + end + list = [] + parents_paths(target_pathname, parent_analyze_level, index) do |parent_path, _child_path| + list << parent_path + end + output = list.uniq + output_pathname&.write(output) + output + end + + def self.children(src_path, target_path, alias_paths: nil, child_analyze_level: 1, output_path: nil) + output_pathname = Pathname.new(output_path) if output_path + index = JsDependency::IndexCreator.call(src_path, alias_paths: alias_paths) + + target_pathname = if Pathname.new(target_path).relative? && Pathname.new(target_path).exist? + Pathname.new(target_path).realpath + else + Pathname.new(target_path) + end + list = [] + children_paths(target_pathname, child_analyze_level, index) do |_parent_path, child_path| + list << child_path + end + output = list.uniq output_pathname&.write(output) output end # @param [String] target_path