js_dependency 0.2.3 → 0.3.1

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: 7b8953dd4a8b764b770e0f8cb04d2699f5b5c8bf5248b49b13e7be9292458146
4
- data.tar.gz: ce935f0e5035762546570a0bce1971cf82279ebdfe39af47fbc225c0c836e907
3
+ metadata.gz: 105b4f97bf9e521b3665eaa4b1b3ffb28210cc9a3c5937eacf9e37720df78d09
4
+ data.tar.gz: 020eca2f41c6e7f63ac8f1bd91e7b0191e2496d5e28b62edabca74e58aa85251
5
5
  SHA512:
6
- metadata.gz: 8fca2ad58152cb2f955c7df0bb067f8b6fcbdeecd7bf44716a11eac776c433325b87b7f5ff240c261efe31c81d8484bf2642aa819437c7c56ec8e45bb90a2fb6
7
- data.tar.gz: c0d65e0fddd984a85fcf2c26d4233bec7b9d90f8c999d370c876889b813d182beaf0eecc6aefe815496f4bd4db2fa5a0e9cc12b912c267df0c111a8bb707c053
6
+ metadata.gz: 4274c3354a8ddc358036c5a7246e5fa905947b4760417350ed28c57a02dd10a9fd4e77535c0fe2fdbe91ac4237d5f9a35d7acfc6f23257cb2201078f4608342f
7
+ data.tar.gz: 135352e118f7d89dd0c024be73ed3671ec075272d4d51f8138fe5a954d5f3e115782b559dcd3be67ebec380af3dbc95d2cf073fc3ede700e0fca4c3bee6d2cf2
data/.rubocop_todo.yml CHANGED
@@ -1,40 +1,35 @@
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-24 23:16:43 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: 40
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
35
  # This cop supports safe autocorrection (--autocorrect).
@@ -42,9 +37,14 @@ Style/IfUnlessModifier:
42
37
  Exclude:
43
38
  - 'lib/js_dependency/index_creator.rb'
44
39
 
45
- # Offense count: 2
40
+ # Offense count: 1
41
+ Style/MultilineBlockChain:
42
+ Exclude:
43
+ - 'lib/js_dependency/mermaid/root.rb'
44
+
45
+ # Offense count: 6
46
46
  # This cop supports safe autocorrection (--autocorrect).
47
47
  # Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, AllowedPatterns, IgnoredPatterns.
48
48
  # URISchemes: http, https
49
49
  Layout/LineLength:
50
- Max: 182
50
+ Max: 183
data/CHANGELOG.md CHANGED
@@ -1,5 +1,20 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.3.1] - 2022-07-25
4
+
5
+ - Multiple target paths for JsDependency.export and export_mermaid CLI.
6
+
7
+ ## [0.3.0] - 2022-07-24
8
+
9
+ - Stable mermaid support.
10
+ - Stable `excludes` option.
11
+ - Refactor JsDependency class method using `JsDependency::TargetPathname`.
12
+
13
+ ## [0.2.3.1] - 2022-07-21
14
+
15
+ - Revert for script tag with line brake.
16
+ - Fix exclude pattern.
17
+
3
18
  ## [0.2.3] - 2022-07-21
4
19
 
5
20
  - Fix 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)
4
+ js_dependency (0.3.1)
5
5
  pathname
6
6
  thor
7
7
  yaml
data/README.md CHANGED
@@ -49,10 +49,18 @@ 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
+
56
64
  #### Export parents components list
57
65
 
58
66
  ```shell
@@ -9,7 +9,7 @@ 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_path, 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"
@@ -22,7 +22,7 @@ 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_path] || (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
@@ -36,7 +36,7 @@ module JsDependency
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,
@@ -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
@@ -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
@@ -10,13 +10,15 @@ module JsDependency
10
10
  # @return [Array<String>]
11
11
  def call
12
12
  str = @str
13
- scripts = str.gsub(%r{<script.*>(.+)</script>}m).with_object([]) do |_, list|
13
+ scripts = str.gsub(%r{<script>(.+)</script>}m).with_object([]) do |_, list|
14
14
  list << Regexp.last_match(1)
15
15
  end
16
16
 
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
 
@@ -41,6 +53,9 @@ module JsDependency
41
53
  end
42
54
  end
43
55
 
56
+ # @param [Pathname] component_pathname
57
+ # @param [Array<String>] alias_paths
58
+ # @return [Array<Pathname>]
44
59
  def import_pathnames_from(component_pathname, alias_paths)
45
60
  component_dirname = component_pathname.dirname
46
61
  script_str = extract_script_string(component_pathname)
@@ -49,6 +64,10 @@ module JsDependency
49
64
  end
50
65
  end
51
66
 
67
+ # @param [String] import_path
68
+ # @param [Array<String>] alias_paths
69
+ # @param [Pathname] component_dirname
70
+ # @return [Pathname]
52
71
  def standardize_path(import_path, alias_paths, component_dirname)
53
72
  import_pathname = Pathname.new(replace_path_alias(import_path, alias_paths))
54
73
 
@@ -64,6 +83,8 @@ module JsDependency
64
83
  import_pathname.cleanpath
65
84
  end
66
85
 
86
+ # @param [Pathname] pathname
87
+ # @return [String]
67
88
  def extract_script_string(pathname)
68
89
  str = pathname.read
69
90
  extname = pathname.extname
@@ -72,6 +93,9 @@ module JsDependency
72
93
  JsDependency::ExtractScriptTag.call(str)
73
94
  end
74
95
 
96
+ # @param [String] path
97
+ # @param [Array<String>] alias_paths
98
+ # @return [String]
75
99
  def replace_path_alias(path, alias_paths)
76
100
  JsDependency::ReplacePathAlias.call(path, alias_paths)
77
101
  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"
4
+ VERSION = "0.3.1"
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) }
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) }
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
4
+ version: 0.3.1
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-20 00:00:00.000000000 Z
11
+ date: 2022-07-24 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