js_dependency 0.2.3.1 → 0.3.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9a59fced735fccc952d4f31d5dc27e41637ef5ee67bc5f5b7eb8038f5458808c
4
- data.tar.gz: b4e713344b1a58d6dac1143068a20cf1e43bad0158c50c4ef28c1e8cd0b7ddc8
3
+ metadata.gz: 2bcea60e75e368a74db588a569dcc71dac789988395940b9ae2ec8f8be965cef
4
+ data.tar.gz: 3760510a51085c9842eaf0a2b49989c3c400b130bde282d6150f4d986c38c36b
5
5
  SHA512:
6
- metadata.gz: f2432b1e03352673908567cfc7f0ec5798bae146201db548c48899b1601e6d79680a3bef33daec2b82fa00bc541ff0b099757d65ded91f9801f026dcc5d52a56
7
- data.tar.gz: 9f952a5dc433c9e687c12505dbb47136cbf8da671f5981cf008d1f002d282c3195f14976a94a911364c581adc55f36c68cb1e0d0459961328a49fa44b5e8bc4f
6
+ metadata.gz: ba4acdf96d74072a3e439647bdd5817f622d11393961127af5ee88d112a53c4d4231a31bf2a08129dc889cc59bf69e7057e0074554d3572cf9396457dc98e2cd
7
+ data.tar.gz: 285681ef4ebea7f2eb9a6cb8214316d0f7899bc275723bcf35be8bde33c2ae5824f3ba0be7e64d163fc3120971073f7c6bbe19bfc265eff0c44722ee2fd7d018
data/.rubocop_todo.yml CHANGED
@@ -1,50 +1,44 @@
1
1
  # This configuration was generated by
2
2
  # `rubocop --auto-gen-config`
3
- # on 2022-07-20 23:10:21 UTC using RuboCop version 1.31.2.
3
+ # on 2022-07-25 14:50:54 UTC using RuboCop version 1.31.2.
4
4
  # The point is for the user to remove these configuration records
5
5
  # one by one as the offenses are removed from the code base.
6
6
  # Note that changes in the inspected code, or installation of new
7
7
  # versions of RuboCop, may require this file to be generated again.
8
8
 
9
- # Offense count: 6
9
+ # Offense count: 7
10
10
  # Configuration parameters: IgnoredMethods, CountRepeatedAttributes.
11
11
  Metrics/AbcSize:
12
- Max: 37
12
+ Max: 41
13
13
 
14
- # Offense count: 6
14
+ # Offense count: 5
15
15
  # Configuration parameters: IgnoredMethods.
16
16
  Metrics/CyclomaticComplexity:
17
- Max: 15
17
+ Max: 16
18
18
 
19
- # Offense count: 8
19
+ # Offense count: 9
20
20
  # Configuration parameters: CountComments, CountAsOne, ExcludedMethods, IgnoredMethods.
21
21
  Metrics/MethodLength:
22
22
  Max: 26
23
23
 
24
- # Offense count: 1
25
- # Configuration parameters: CountComments, CountAsOne.
26
- Metrics/ModuleLength:
27
- Max: 114
28
-
29
24
  # Offense count: 3
30
25
  # Configuration parameters: CountKeywordArgs, MaxOptionalParameters.
31
26
  Metrics/ParameterLists:
32
27
  Max: 9
33
28
 
34
- # Offense count: 6
29
+ # Offense count: 5
35
30
  # Configuration parameters: IgnoredMethods.
36
31
  Metrics/PerceivedComplexity:
37
- Max: 16
32
+ Max: 17
38
33
 
39
34
  # Offense count: 1
40
- # This cop supports safe autocorrection (--autocorrect).
41
- Style/IfUnlessModifier:
35
+ Style/MultilineBlockChain:
42
36
  Exclude:
43
- - 'lib/js_dependency/index_creator.rb'
37
+ - 'lib/js_dependency/mermaid/root.rb'
44
38
 
45
- # Offense count: 2
39
+ # Offense count: 7
46
40
  # This cop supports safe autocorrection (--autocorrect).
47
41
  # Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, AllowedPatterns, IgnoredPatterns.
48
42
  # URISchemes: http, https
49
43
  Layout/LineLength:
50
- Max: 182
44
+ Max: 183
data/CHANGELOG.md CHANGED
@@ -1,5 +1,19 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.3.2] - 2022-07-26
4
+
5
+ - Multiple excludes option for CLI.
6
+
7
+ ## [0.3.1] - 2022-07-25
8
+
9
+ - Multiple target paths for JsDependency.export and export_mermaid CLI.
10
+
11
+ ## [0.3.0] - 2022-07-24
12
+
13
+ - Stable mermaid support.
14
+ - Stable `excludes` option.
15
+ - Refactor JsDependency class method using `JsDependency::TargetPathname`.
16
+
3
17
  ## [0.2.3.1] - 2022-07-21
4
18
 
5
19
  - Revert for script tag with line brake.
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- js_dependency (0.2.3.1)
4
+ js_dependency (0.3.2)
5
5
  pathname
6
6
  thor
7
7
  yaml
data/README.md CHANGED
@@ -49,10 +49,24 @@ exclude -> -e
49
49
 
50
50
  #### Export Mermaid Format
51
51
 
52
+ ##### Single target path
53
+
52
54
  ```shell
53
55
  js_dependency -s ./src -t ./src/App.vue -o ./mermaid.txt -c 2 -p 2 -n 1
54
56
  ```
55
57
 
58
+ ##### Multiple target paths
59
+
60
+ ```shell
61
+ js_dependency -s ./src -t ./src/App.vue ./src/components/Sub.vue -o ./mermaid.txt -c 2 -p 2 -n 1
62
+ ```
63
+
64
+ ##### Exclude path which iclude exclude words `-e` from output
65
+
66
+ ```shell
67
+ js_dependency -s ./src -t ./src/App.vue -o ./mermaid.txt -c 2 -p 2 -n 1 -e excludeWord1 excludeWord2
68
+ ```
69
+
56
70
  #### Export parents components list
57
71
 
58
72
  ```shell
@@ -9,12 +9,12 @@ module JsDependency
9
9
 
10
10
  desc "export_mermaid", "Output mermaid flowchart string."
11
11
  option :src_path, type: :string, aliases: "-s", desc: "Root folder."
12
- option :target_path, type: :string, aliases: "-t", desc: "Target file that you want to analyze."
12
+ option :target_paths, type: :array, aliases: "-t", desc: "Target file that you want to analyze."
13
13
  option :output_path, type: :string, aliases: "-o", desc: "Output file path"
14
14
  option :child_analyze_level, type: :numeric, aliases: "-c", desc: "Output level of child dependency"
15
15
  option :parent_analyze_level, type: :numeric, aliases: "-p", desc: "Output level of parent dependency"
16
16
  option :name_level, type: :numeric, aliases: "-n", desc: "Output name level"
17
- option :exclude, type: :string, aliases: "-e", desc: "Exclude the word that is included in the path"
17
+ option :excludes, type: :array, aliases: "-e", desc: "Exclude the word that is included in the path"
18
18
 
19
19
  def export_mermaid
20
20
  pathname = Pathname.new(".js_dependency.yml")
@@ -22,21 +22,21 @@ module JsDependency
22
22
  args = YAML.safe_load(pathname.read) if pathname.exist?
23
23
 
24
24
  src_path = options[:src_path] || args["src_path"]
25
- target_path = options[:target_path] || args["target_path"]
25
+ target_paths = options[:target_paths] || (args["target_path"].is_a?(String) ? [args["target_path"]] : args["target_path"])
26
26
  child_analyze_level = options[:child_analyze_level] || args["child_analyze_level"] || 2
27
27
  parent_analyze_level = options[:parent_analyze_level] || args["parent_analyze_level"] || 2
28
28
  output_path = options[:output_path] || args["output_path"] || nil
29
29
  alias_paths = args["alias_paths"] || nil
30
30
  name_level = options[:name_level] || args["name_level"] || 1
31
- excludes = if options[:exclude]
32
- [options[:exclude]]
31
+ excludes = if options[:excludes].length.positive?
32
+ options[:excludes]
33
33
  elsif args["excludes"]
34
34
  args["excludes"]
35
35
  end
36
36
 
37
37
  str = JsDependency.export_mermaid(
38
38
  src_path,
39
- target_path,
39
+ target_paths,
40
40
  child_analyze_level: child_analyze_level,
41
41
  parent_analyze_level: parent_analyze_level,
42
42
  output_path: output_path,
@@ -53,7 +53,7 @@ module JsDependency
53
53
  option :target_path, type: :string, aliases: "-t", desc: "Target file that you want to analyze."
54
54
  option :output_path, type: :string, aliases: "-o", desc: "Output file path"
55
55
  option :parent_analyze_level, type: :numeric, aliases: "-p", desc: "Output level of parent dependency"
56
- option :exclude, type: :string, aliases: "-e", desc: "Exclude the word that is included in the path"
56
+ option :excludes, type: :array, aliases: "-e", desc: "Exclude the word that is included in the path"
57
57
 
58
58
  def parents
59
59
  pathname = Pathname.new(".js_dependency.yml")
@@ -65,8 +65,8 @@ module JsDependency
65
65
  parent_analyze_level = options[:parent_analyze_level] || args["parent_analyze_level"] || 1
66
66
  output_path = options[:output_path] || args["output_path"] || nil
67
67
  alias_paths = args["alias_paths"] || nil
68
- excludes = if options[:exclude]
69
- [options[:exclude]]
68
+ excludes = if options[:excludes].length.positive?
69
+ options[:excludes]
70
70
  elsif args["excludes"]
71
71
  args["excludes"]
72
72
  end
@@ -78,7 +78,7 @@ module JsDependency
78
78
  output_path: output_path,
79
79
  alias_paths: alias_paths,
80
80
  excludes: excludes
81
- ).sort.uniq.join("\n")
81
+ ).join("\n")
82
82
 
83
83
  puts str
84
84
  end
@@ -88,7 +88,7 @@ module JsDependency
88
88
  option :target_path, type: :string, aliases: "-t", desc: "Target file that you want to analyze."
89
89
  option :output_path, type: :string, aliases: "-o", desc: "Output file path"
90
90
  option :child_analyze_level, type: :numeric, aliases: "-c", desc: "Output level of child dependency"
91
- option :exclude, type: :string, aliases: "-e", desc: "Exclude the word that is included in the path"
91
+ option :excludes, type: :array, aliases: "-e", desc: "Exclude the word that is included in the path"
92
92
 
93
93
  def children
94
94
  pathname = Pathname.new(".js_dependency.yml")
@@ -100,8 +100,8 @@ module JsDependency
100
100
  child_analyze_level = options[:child_analyze_level] || args["child_analyze_level"] || 1
101
101
  output_path = options[:output_path] || args["output_path"] || nil
102
102
  alias_paths = args["alias_paths"] || nil
103
- excludes = if options[:exclude]
104
- [options[:exclude]]
103
+ excludes = if options[:excludes].length.positive?
104
+ options[:excludes]
105
105
  elsif args["excludes"]
106
106
  args["excludes"]
107
107
  end
@@ -113,7 +113,7 @@ module JsDependency
113
113
  output_path: output_path,
114
114
  alias_paths: alias_paths,
115
115
  excludes: excludes
116
- ).sort.uniq.join("\n")
116
+ ).join("\n")
117
117
 
118
118
  puts str
119
119
  end
@@ -17,6 +17,8 @@ module JsDependency
17
17
  scripts.uniq.sort.join("\n")
18
18
  end
19
19
 
20
+ # @param [String] str
21
+ # @return [String]
20
22
  def self.call(str)
21
23
  new(str).call
22
24
  end
@@ -7,15 +7,24 @@ require_relative "pathname_utility"
7
7
 
8
8
  module JsDependency
9
9
  class IndexCreator
10
+ # @param [String] src
11
+ # @param [Array<String>] alias_paths
10
12
  def initialize(src, alias_paths: nil)
11
13
  @src = src
12
14
  @alias_paths = alias_paths
13
15
  end
14
16
 
15
- def self.call(src, alias_paths: nil)
16
- new(src, alias_paths: alias_paths).call
17
+ # @param [String] src
18
+ # @param [Array<String>] alias_paths
19
+ # @param [Array<String>] excludes
20
+ def self.call(src, alias_paths: nil, excludes: nil)
21
+ index = new(src, alias_paths: alias_paths).call
22
+ index.transform_values do |value|
23
+ value.reject { |path| excludes&.any? { |exclude| path.include?(exclude) } }
24
+ end
17
25
  end
18
26
 
27
+ # @return [Hash]
19
28
  def call
20
29
  src_pathname = Pathname.new(@src).relative? ? Pathname.new(@src).realpath : Pathname.new(@src)
21
30
  raise Error, "#{@src} is not directory." unless src_pathname.directory?
@@ -28,6 +37,9 @@ module JsDependency
28
37
 
29
38
  private
30
39
 
40
+ # @param [Pathname] src_pathname
41
+ # @param [Array<String>] alias_paths
42
+ # @return [Hash]
31
43
  def index_from(src_pathname, alias_paths)
32
44
  pattern = %w[**/*.vue **/*.js **/*.jsx]
33
45
 
@@ -35,12 +47,13 @@ module JsDependency
35
47
  import_pathnames = import_pathnames_from(component_pathname, alias_paths)
36
48
 
37
49
  obj[component_pathname.to_s] = import_pathnames.map(&:to_s)
38
- if component_pathname.basename.to_s == "index.js"
39
- obj[component_pathname.dirname.to_s] = import_pathnames.map(&:to_s)
40
- end
50
+ obj[component_pathname.dirname.to_s] = import_pathnames.map(&:to_s) if component_pathname.basename.to_s == "index.js"
41
51
  end
42
52
  end
43
53
 
54
+ # @param [Pathname] component_pathname
55
+ # @param [Array<String>] alias_paths
56
+ # @return [Array<Pathname>]
44
57
  def import_pathnames_from(component_pathname, alias_paths)
45
58
  component_dirname = component_pathname.dirname
46
59
  script_str = extract_script_string(component_pathname)
@@ -49,6 +62,10 @@ module JsDependency
49
62
  end
50
63
  end
51
64
 
65
+ # @param [String] import_path
66
+ # @param [Array<String>] alias_paths
67
+ # @param [Pathname] component_dirname
68
+ # @return [Pathname]
52
69
  def standardize_path(import_path, alias_paths, component_dirname)
53
70
  import_pathname = Pathname.new(replace_path_alias(import_path, alias_paths))
54
71
 
@@ -64,6 +81,8 @@ module JsDependency
64
81
  import_pathname.cleanpath
65
82
  end
66
83
 
84
+ # @param [Pathname] pathname
85
+ # @return [String]
67
86
  def extract_script_string(pathname)
68
87
  str = pathname.read
69
88
  extname = pathname.extname
@@ -72,6 +91,9 @@ module JsDependency
72
91
  JsDependency::ExtractScriptTag.call(str)
73
92
  end
74
93
 
94
+ # @param [String] path
95
+ # @param [Array<String>] alias_paths
96
+ # @return [String]
75
97
  def replace_path_alias(path, alias_paths)
76
98
  JsDependency::ReplacePathAlias.call(path, alias_paths)
77
99
  end
@@ -5,30 +5,37 @@ module JsDependency
5
5
  class NodesLink
6
6
  attr_reader :parent, :child
7
7
 
8
+ # @param [String] parent
9
+ # @param [String] child
8
10
  def initialize(parent, child)
9
- @parent = parent
10
- @child = child
11
+ @parent = Pathname.new(parent)
12
+ @child = Pathname.new(child)
11
13
  end
12
14
 
15
+ # @param [Integer] level
16
+ # @return [String]
13
17
  def parent_module_name(level = 0)
14
- parse(Pathname.new(@parent), level).join("/")
18
+ mermaid_str(@parent, level)
15
19
  end
16
20
 
21
+ # @param [Integer] level
22
+ # @return [String]
17
23
  def child_module_name(level = 0)
18
- parse(Pathname.new(@child), level).join("/")
24
+ mermaid_str(@child, level)
19
25
  end
20
26
 
21
27
  private
22
28
 
23
- def parse(pathname, level)
24
- return [pathname.to_s] unless pathname.exist?
29
+ # @param [Pathname] pathname
30
+ # @param [Integer] level
31
+ def mermaid_str(pathname, level = 0)
32
+ "#{parse(pathname).join("_")}[\"#{parse(pathname, level).join("/")}\"]"
33
+ end
25
34
 
26
- array = [pathname.basename]
27
- level.times do
28
- array.unshift(pathname.parent.basename.to_s)
29
- pathname = pathname.parent
30
- end
31
- array
35
+ # @param [Pathname] pathname
36
+ # @param [Integer] level
37
+ def parse(pathname, level = -1)
38
+ pathname.each_filename.with_object([]) { |filename, array| array << filename }.reverse[0..level].reverse
32
39
  end
33
40
  end
34
41
  end
@@ -6,20 +6,48 @@ module JsDependency
6
6
  class Root
7
7
  attr_accessor :orientation
8
8
 
9
+ # @param [String] orientation
9
10
  def initialize(orientation = "LR")
10
11
  @orientation = orientation
11
12
  @list = []
12
13
  end
13
14
 
15
+ # @param [String] parent
16
+ # @param [String] child
14
17
  def add(parent, child)
15
18
  @list << NodesLink.new(parent, child)
16
19
  end
17
20
 
18
- def export(name_level: 1)
19
- str = "flowchart #{orientation}\n"
20
- str + @list.uniq { |link| "#{link.parent}__#{link.child}" }.sort_by { |link| "#{link.parent}__#{link.child}" }.map do |link|
21
+ # @param [Integer] name_level
22
+ # @param [nil, String] src_path
23
+ # @return [String]
24
+ def export(name_level: 1, src_path: nil)
25
+ ([export_header] + export_nodes(name_level: name_level, src_path: src_path)).join("\n")
26
+ end
27
+
28
+ # @return [String]
29
+ def export_header
30
+ "flowchart #{orientation}"
31
+ end
32
+
33
+ # @param [Integer] name_level
34
+ # @param [nil, String] src_path
35
+ # @return [Array]
36
+ def export_nodes(name_level: 1, src_path: nil)
37
+ nodes_links = if src_path
38
+ src_pathname = Pathname.new(src_path).realpath
39
+ @list.map do |nodes_link|
40
+ NodesLink.new(nodes_link.parent.exist? ? nodes_link.parent.relative_path_from(src_pathname.to_s) : nodes_link.parent.to_s,
41
+ nodes_link.child.exist? ? nodes_link.child.relative_path_from(src_pathname.to_s) : nodes_link.child.to_s)
42
+ end
43
+ else
44
+ @list
45
+ end
46
+ nodes_links.uniq do |link|
47
+ "#{link.parent}__#{link.child}"
48
+ end.sort_by { |link| "#{link.parent}__#{link.child}" }.map do |link|
21
49
  "#{link.parent_module_name(name_level)} --> #{link.child_module_name(name_level)}"
22
- end.join("\n")
50
+ end
23
51
  end
24
52
  end
25
53
  end
@@ -2,6 +2,8 @@
2
2
 
3
3
  module JsDependency
4
4
  module PathnameUtility
5
+ # @param [Pathname] pathname
6
+ # @return [Pathname]
5
7
  def self.complement_extname(pathname)
6
8
  return pathname if pathname.exist? || pathname.extname != ""
7
9
 
@@ -13,5 +15,15 @@ module JsDependency
13
15
 
14
16
  pathname
15
17
  end
18
+
19
+ # @param [String] target_path
20
+ # @return [Pathname]
21
+ def self.to_target_pathname(target_path)
22
+ if Pathname.new(target_path).relative? && Pathname.new(target_path).exist?
23
+ Pathname.new(target_path).realpath
24
+ else
25
+ Pathname.new(target_path)
26
+ end
27
+ end
16
28
  end
17
29
  end
@@ -36,6 +36,7 @@ module JsDependency
36
36
  str
37
37
  end
38
38
 
39
+ # @return [Array<String>]
39
40
  def safe_list
40
41
  ["@rails/ujs"]
41
42
  end
@@ -0,0 +1,76 @@
1
+ # frozen_string_literal: true
2
+
3
+ module JsDependency
4
+ class TargetPathname
5
+ # @param [String] target_path
6
+ def initialize(target_path)
7
+ @pathname = if Pathname.new(target_path).relative? && Pathname.new(target_path).exist?
8
+ Pathname.new(target_path).realpath
9
+ else
10
+ Pathname.new(target_path)
11
+ end
12
+ end
13
+
14
+ # @param [Integer] analyze_level
15
+ # @param [Hash] index
16
+ def each_parent_path(analyze_level, index)
17
+ temp_paths = [@pathname.to_s]
18
+ analyze_level.times do
19
+ list = []
20
+ temp_paths.each do |temp_path|
21
+ temp_pathname = to_target_pathname(temp_path)
22
+
23
+ list += extract_parent_paths(temp_pathname.to_s, index).each do |parent_path|
24
+ yield parent_path, temp_pathname.to_s
25
+ end
26
+ end
27
+ temp_paths = list
28
+ end
29
+ end
30
+
31
+ # @param [Integer] analyze_level
32
+ # @param [Hash] index
33
+ def each_child_path(analyze_level, index)
34
+ temp_paths = [@pathname.to_s]
35
+ analyze_level.times do
36
+ list = []
37
+ temp_paths.each do |temp_path|
38
+ temp_pathname = to_target_pathname(temp_path)
39
+
40
+ list += extract_children_paths(temp_pathname.to_s, index).each do |child_path|
41
+ yield temp_pathname.to_s, child_path
42
+ end
43
+ end
44
+ temp_paths = list
45
+ end
46
+ end
47
+
48
+ private
49
+
50
+ # @param [String] target_path
51
+ # @param [Hash] index
52
+ # @return [Array]
53
+ def extract_parent_paths(target_path, index)
54
+ index.each_with_object([]) do |(parent, children), list|
55
+ list << parent if children.any?(target_path)
56
+ end
57
+ end
58
+
59
+ # @param [String] target_path
60
+ # @param [Hash] index
61
+ # @return [Array]
62
+ def extract_children_paths(target_path, index)
63
+ index[target_path] || []
64
+ end
65
+
66
+ # @param [String] target_path
67
+ # @return [Pathname]
68
+ def to_target_pathname(target_path)
69
+ if Pathname.new(target_path).relative? && Pathname.new(target_path).exist?
70
+ Pathname.new(target_path).realpath
71
+ else
72
+ Pathname.new(target_path)
73
+ end
74
+ end
75
+ end
76
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module JsDependency
4
- VERSION = "0.2.3.1"
4
+ VERSION = "0.3.2"
5
5
  end
data/lib/js_dependency.rb CHANGED
@@ -4,13 +4,22 @@ require_relative "js_dependency/version"
4
4
  require_relative "js_dependency/index_creator"
5
5
  require_relative "js_dependency/mermaid/root"
6
6
  require_relative "js_dependency/cli"
7
+ require_relative "js_dependency/target_pathname"
7
8
  require "pathname"
8
9
 
9
10
  module JsDependency
10
11
  class Error < StandardError; end
11
12
 
12
13
  # @param [String] src_path
13
- # @param [String] target_path
14
+ # @param [Hash, nil] alias_paths
15
+ # @param [Array, nil] excludes
16
+ # @return [Hash]
17
+ def self.export_index(src_path, alias_paths: nil, excludes: nil)
18
+ JsDependency::IndexCreator.call(src_path, alias_paths: alias_paths, excludes: excludes)
19
+ end
20
+
21
+ # @param [String] src_path
22
+ # @param [Array<String>] target_paths
14
23
  # @param [String] orientation
15
24
  # @param [Hash, nil] alias_paths
16
25
  # @param [Integer] child_analyze_level
@@ -19,148 +28,76 @@ module JsDependency
19
28
  # @param [String, nil] output_path
20
29
  # @param [Array, nil] excludes
21
30
  # @return [String]
22
- def self.export_mermaid(src_path, target_path, orientation: "LR", alias_paths: nil, child_analyze_level: 1, parent_analyze_level: 1, name_level: 1, output_path: nil, excludes: nil)
31
+ def self.export_mermaid(src_path, target_paths, orientation: "LR", alias_paths: nil, child_analyze_level: 1, parent_analyze_level: 1, name_level: 1, output_path: nil, excludes: nil)
23
32
  output_pathname = Pathname.new(output_path) if output_path
24
- index = JsDependency::IndexCreator.call(src_path, alias_paths: alias_paths)
33
+ index = JsDependency::IndexCreator.call(src_path, alias_paths: alias_paths, excludes: excludes)
25
34
 
26
- target_pathname = if Pathname.new(target_path).relative? && Pathname.new(target_path).exist?
27
- Pathname.new(target_path).realpath
28
- else
29
- Pathname.new(target_path)
30
- end
35
+ nodes = []
36
+ target_paths.each do |target_path|
37
+ target_pathname = JsDependency::TargetPathname.new(target_path)
31
38
 
32
- root = JsDependency::Mermaid::Root.new(orientation)
39
+ mermaid_root = JsDependency::Mermaid::Root.new(orientation)
33
40
 
34
- parents_paths(target_pathname, parent_analyze_level, index, excludes: excludes) do |parent_path, child_path|
35
- root.add(parent_path, child_path)
36
- end
41
+ target_pathname.each_parent_path(parent_analyze_level, index) do |parent_path, child_path|
42
+ mermaid_root.add(parent_path, child_path)
43
+ end
37
44
 
38
- children_paths(target_pathname, child_analyze_level, index, excludes: excludes) do |parent_path, child_path|
39
- root.add(parent_path, child_path)
45
+ target_pathname.each_child_path(child_analyze_level, index) do |parent_path, child_path|
46
+ mermaid_root.add(parent_path, child_path)
47
+ end
48
+ nodes += mermaid_root.export_nodes(name_level: name_level, src_path: src_path)
40
49
  end
41
50
 
42
- output = root.export(name_level: name_level)
51
+ output = (["flowchart LR"] + nodes.uniq).join("\n")
43
52
  output_pathname&.write(output)
44
53
  output
45
54
  end
46
55
 
56
+ # @param [String] src_path
57
+ # @param [String] target_path
58
+ # @param [String] orientation
59
+ # @param [Hash, nil] alias_paths
60
+ # @param [Integer] parent_analyze_level
61
+ # @param [String, nil] output_path
62
+ # @param [Array, nil] excludes
63
+ # @return [Array<Pathname>]
47
64
  def self.parents(src_path, target_path, alias_paths: nil, parent_analyze_level: 1, output_path: nil, excludes: nil)
48
65
  output_pathname = Pathname.new(output_path) if output_path
49
- index = JsDependency::IndexCreator.call(src_path, alias_paths: alias_paths)
66
+ index = JsDependency::IndexCreator.call(src_path, alias_paths: alias_paths, excludes: excludes)
50
67
 
51
- target_pathname = if Pathname.new(target_path).relative? && Pathname.new(target_path).exist?
52
- Pathname.new(target_path).realpath
53
- else
54
- Pathname.new(target_path)
55
- end
56
- list = []
57
- parents_paths(target_pathname, parent_analyze_level, index, excludes: excludes) do |parent_path, _child_path|
58
- list << parent_path
68
+ target_pathname = JsDependency::TargetPathname.new(target_path)
69
+ paths = []
70
+ target_pathname.each_parent_path(parent_analyze_level, index) do |parent_path, _child_path|
71
+ paths << parent_path
72
+ end
73
+ output = paths.uniq.sort.map do |path|
74
+ Pathname.new(path).exist? ? Pathname.new(path).relative_path_from(Pathname.new(src_path).realpath.to_s).to_s : Pathname.new(path).to_s
59
75
  end
60
- output = list.uniq
61
76
  output_pathname&.write(output.sort.join("\n"))
62
77
  output
63
78
  end
64
79
 
80
+ # @param [String] src_path
81
+ # @param [String] target_path
82
+ # @param [String] orientation
83
+ # @param [Hash, nil] alias_paths
84
+ # @param [Integer] child_analyze_level
85
+ # @param [String, nil] output_path
86
+ # @param [Array, nil] excludes
87
+ # @return [Array<Pathname>]
65
88
  def self.children(src_path, target_path, alias_paths: nil, child_analyze_level: 1, output_path: nil, excludes: nil)
66
89
  output_pathname = Pathname.new(output_path) if output_path
67
- index = JsDependency::IndexCreator.call(src_path, alias_paths: alias_paths)
90
+ index = JsDependency::IndexCreator.call(src_path, alias_paths: alias_paths, excludes: excludes)
68
91
 
69
- target_pathname = if Pathname.new(target_path).relative? && Pathname.new(target_path).exist?
70
- Pathname.new(target_path).realpath
71
- else
72
- Pathname.new(target_path)
73
- end
74
- list = []
75
- children_paths(target_pathname, child_analyze_level, index, excludes: excludes) do |_parent_path, child_path|
76
- list << child_path
92
+ target_pathname = JsDependency::TargetPathname.new(target_path)
93
+ paths = []
94
+ target_pathname.each_child_path(child_analyze_level, index) do |_parent_path, child_path|
95
+ paths << child_path
96
+ end
97
+ output = paths.uniq.sort.map do |path|
98
+ Pathname.new(path).exist? ? Pathname.new(path).relative_path_from(Pathname.new(src_path).realpath.to_s).to_s : Pathname.new(path).to_s
77
99
  end
78
- output = list.uniq
79
100
  output_pathname&.write(output.sort.join("\n"))
80
101
  output
81
102
  end
82
-
83
- # @param [String] target_path
84
- # @param [Hash] index
85
- # @return [Array]
86
- def self.extract_parent_paths(target_path, index)
87
- target_pathname = if Pathname.new(target_path).exist?
88
- Pathname.new(target_path).realpath
89
- else
90
- Pathname.new(target_path)
91
- end
92
- index.each_with_object([]) do |(parent, children), list|
93
- list << parent if children.any?(target_pathname.to_s)
94
- end
95
- end
96
-
97
- private_class_method :extract_parent_paths
98
-
99
- # @param [String] target_path
100
- # @param [Hash] index
101
- # @return [Array]
102
- def self.extract_children_paths(target_path, index)
103
- target_pathname = if Pathname.new(target_path).exist?
104
- Pathname.new(target_path).realpath
105
- else
106
- Pathname.new(target_path)
107
- end
108
- index[target_pathname.to_s] || []
109
- end
110
-
111
- private_class_method :extract_children_paths
112
-
113
- # @param [String] src
114
- # @param [nil, Hash] alias_paths
115
- def self.create_index(src_path, alias_paths: nil)
116
- JsDependency::IndexCreator.call(src_path, alias_paths: alias_paths)
117
- end
118
-
119
- private_class_method :create_index
120
-
121
- def self.parents_paths(target_pathname, analyze_level, index, excludes: nil)
122
- temp_paths = [target_pathname.to_s]
123
- analyze_level.times do
124
- list = []
125
- temp_paths.each do |temp_path|
126
- temp_pathname = if Pathname.new(temp_path).relative? && Pathname.new(temp_path).exist?
127
- Pathname.new(temp_path).realpath
128
- else
129
- Pathname.new(temp_path)
130
- end
131
-
132
- list += extract_parent_paths(temp_pathname.to_s, index).each do |parent_path|
133
- next if excludes&.any? { |ignore| parent_path.to_s.include?(ignore) || temp_pathname.to_s.include?(ignore) }
134
-
135
- yield parent_path, temp_pathname.to_s
136
- end
137
- end
138
- temp_paths = list
139
- end
140
- end
141
-
142
- private_class_method :parents_paths
143
-
144
- def self.children_paths(target_pathname, analyze_level, index, excludes: nil)
145
- temp_paths = [target_pathname.to_s]
146
- analyze_level.times do
147
- list = []
148
- temp_paths.each do |temp_path|
149
- temp_pathname = if Pathname.new(temp_path).relative? && Pathname.new(temp_path).exist?
150
- Pathname.new(temp_path).realpath
151
- else
152
- Pathname.new(temp_path)
153
- end
154
-
155
- list += extract_children_paths(temp_pathname.to_s, index).each do |child_path|
156
- next if excludes&.any? { |ignore| child_path.to_s.include?(ignore) || temp_pathname.to_s.include?(ignore) }
157
-
158
- yield temp_pathname.to_s, child_path
159
- end
160
- end
161
- temp_paths = list
162
- end
163
- end
164
-
165
- private_class_method :children_paths
166
103
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: js_dependency
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.3.1
4
+ version: 0.3.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - junara
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-07-21 00:00:00.000000000 Z
11
+ date: 2022-07-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: pathname
@@ -166,6 +166,7 @@ files:
166
166
  - lib/js_dependency/mermaid/root.rb
167
167
  - lib/js_dependency/pathname_utility.rb
168
168
  - lib/js_dependency/replace_path_alias.rb
169
+ - lib/js_dependency/target_pathname.rb
169
170
  - lib/js_dependency/version.rb
170
171
  - sig/js_dependency.rbs
171
172
  homepage: https://github.com/junara/js_dependency