js_dependency 0.3.0 → 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: df7ebbe8ac02b295b1ec9d1f9829e7e94868933af1c935f1f5094f4e74f1e876
4
- data.tar.gz: 388b4b017e4b1986b86f221a0bdc19490a9a9c8ca13eaea0847d032e094d6c31
3
+ metadata.gz: 105b4f97bf9e521b3665eaa4b1b3ffb28210cc9a3c5937eacf9e37720df78d09
4
+ data.tar.gz: 020eca2f41c6e7f63ac8f1bd91e7b0191e2496d5e28b62edabca74e58aa85251
5
5
  SHA512:
6
- metadata.gz: 753bccf5874fbe9edd2784d6a999393c13bac53d1da213c861c4d6c4f93fa48d70c6c7bebacaa863aa894e3a38174f2d3a547809c8ee1d6181f793d76d949ab6
7
- data.tar.gz: e449209ee8d7eaa9328db4b94cd28ef61352ace72e776d4297572a385d45bc8e2f5267dd05a638797f04b84268efd6cb61784ca6f4df939053d7172f7d7da8b0
6
+ metadata.gz: 4274c3354a8ddc358036c5a7246e5fa905947b4760417350ed28c57a02dd10a9fd4e77535c0fe2fdbe91ac4237d5f9a35d7acfc6f23257cb2201078f4608342f
7
+ data.tar.gz: 135352e118f7d89dd0c024be73ed3671ec075272d4d51f8138fe5a954d5f3e115782b559dcd3be67ebec380af3dbc95d2cf073fc3ede700e0fca4c3bee6d2cf2
data/.rubocop_todo.yml CHANGED
@@ -1,22 +1,22 @@
1
1
  # This configuration was generated by
2
2
  # `rubocop --auto-gen-config`
3
- # on 2022-07-24 12:55:17 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: 4
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: 4
14
+ # Offense count: 5
15
15
  # Configuration parameters: IgnoredMethods.
16
16
  Metrics/CyclomaticComplexity:
17
- Max: 15
17
+ Max: 16
18
18
 
19
- # Offense count: 7
19
+ # Offense count: 9
20
20
  # Configuration parameters: CountComments, CountAsOne, ExcludedMethods, IgnoredMethods.
21
21
  Metrics/MethodLength:
22
22
  Max: 26
@@ -26,10 +26,10 @@ Metrics/MethodLength:
26
26
  Metrics/ParameterLists:
27
27
  Max: 9
28
28
 
29
- # Offense count: 4
29
+ # Offense count: 5
30
30
  # Configuration parameters: IgnoredMethods.
31
31
  Metrics/PerceivedComplexity:
32
- Max: 16
32
+ Max: 17
33
33
 
34
34
  # Offense count: 1
35
35
  # This cop supports safe autocorrection (--autocorrect).
@@ -42,9 +42,9 @@ Style/MultilineBlockChain:
42
42
  Exclude:
43
43
  - 'lib/js_dependency/mermaid/root.rb'
44
44
 
45
- # Offense count: 1
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,9 @@
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
+
3
7
  ## [0.3.0] - 2022-07-24
4
8
 
5
9
  - Stable mermaid support.
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- js_dependency (0.3.0)
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,
@@ -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,11 +7,16 @@ 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
 
17
+ # @param [String] src
18
+ # @param [Array<String>] alias_paths
19
+ # @param [Array<String>] excludes
15
20
  def self.call(src, alias_paths: nil, excludes: nil)
16
21
  index = new(src, alias_paths: alias_paths).call
17
22
  index.transform_values do |value|
@@ -19,6 +24,7 @@ module JsDependency
19
24
  end
20
25
  end
21
26
 
27
+ # @return [Hash]
22
28
  def call
23
29
  src_pathname = Pathname.new(@src).relative? ? Pathname.new(@src).realpath : Pathname.new(@src)
24
30
  raise Error, "#{@src} is not directory." unless src_pathname.directory?
@@ -31,6 +37,9 @@ module JsDependency
31
37
 
32
38
  private
33
39
 
40
+ # @param [Pathname] src_pathname
41
+ # @param [Array<String>] alias_paths
42
+ # @return [Hash]
34
43
  def index_from(src_pathname, alias_paths)
35
44
  pattern = %w[**/*.vue **/*.js **/*.jsx]
36
45
 
@@ -44,6 +53,9 @@ module JsDependency
44
53
  end
45
54
  end
46
55
 
56
+ # @param [Pathname] component_pathname
57
+ # @param [Array<String>] alias_paths
58
+ # @return [Array<Pathname>]
47
59
  def import_pathnames_from(component_pathname, alias_paths)
48
60
  component_dirname = component_pathname.dirname
49
61
  script_str = extract_script_string(component_pathname)
@@ -52,6 +64,10 @@ module JsDependency
52
64
  end
53
65
  end
54
66
 
67
+ # @param [String] import_path
68
+ # @param [Array<String>] alias_paths
69
+ # @param [Pathname] component_dirname
70
+ # @return [Pathname]
55
71
  def standardize_path(import_path, alias_paths, component_dirname)
56
72
  import_pathname = Pathname.new(replace_path_alias(import_path, alias_paths))
57
73
 
@@ -67,6 +83,8 @@ module JsDependency
67
83
  import_pathname.cleanpath
68
84
  end
69
85
 
86
+ # @param [Pathname] pathname
87
+ # @return [String]
70
88
  def extract_script_string(pathname)
71
89
  str = pathname.read
72
90
  extname = pathname.extname
@@ -75,6 +93,9 @@ module JsDependency
75
93
  JsDependency::ExtractScriptTag.call(str)
76
94
  end
77
95
 
96
+ # @param [String] path
97
+ # @param [Array<String>] alias_paths
98
+ # @return [String]
78
99
  def replace_path_alias(path, alias_paths)
79
100
  JsDependency::ReplacePathAlias.call(path, alias_paths)
80
101
  end
@@ -5,25 +5,35 @@ 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
11
  @parent = Pathname.new(parent)
10
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
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
24
  mermaid_str(@child, level)
19
25
  end
20
26
 
21
27
  private
22
28
 
29
+ # @param [Pathname] pathname
30
+ # @param [Integer] level
23
31
  def mermaid_str(pathname, level = 0)
24
32
  "#{parse(pathname).join("_")}[\"#{parse(pathname, level).join("/")}\"]"
25
33
  end
26
34
 
35
+ # @param [Pathname] pathname
36
+ # @param [Integer] level
27
37
  def parse(pathname, level = -1)
28
38
  pathname.each_filename.with_object([]) { |filename, array| array << filename }.reverse[0..level].reverse
29
39
  end
@@ -6,16 +6,34 @@ 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
 
21
+ # @param [Integer] name_level
22
+ # @param [nil, String] src_path
23
+ # @return [String]
18
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)
19
37
  nodes_links = if src_path
20
38
  src_pathname = Pathname.new(src_path).realpath
21
39
  @list.map do |nodes_link|
@@ -25,12 +43,11 @@ module JsDependency
25
43
  else
26
44
  @list
27
45
  end
28
- str = "flowchart #{orientation}\n"
29
- str + nodes_links.uniq do |link|
46
+ nodes_links.uniq do |link|
30
47
  "#{link.parent}__#{link.child}"
31
48
  end.sort_by { |link| "#{link.parent}__#{link.child}" }.map do |link|
32
49
  "#{link.parent_module_name(name_level)} --> #{link.child_module_name(name_level)}"
33
- end.join("\n")
50
+ end
34
51
  end
35
52
  end
36
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
 
@@ -14,6 +16,8 @@ module JsDependency
14
16
  pathname
15
17
  end
16
18
 
19
+ # @param [String] target_path
20
+ # @return [Pathname]
17
21
  def self.to_target_pathname(target_path)
18
22
  if Pathname.new(target_path).relative? && Pathname.new(target_path).exist?
19
23
  Pathname.new(target_path).realpath
@@ -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
@@ -63,6 +63,8 @@ module JsDependency
63
63
  index[target_path] || []
64
64
  end
65
65
 
66
+ # @param [String] target_path
67
+ # @return [Pathname]
66
68
  def to_target_pathname(target_path)
67
69
  if Pathname.new(target_path).relative? && Pathname.new(target_path).exist?
68
70
  Pathname.new(target_path).realpath
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module JsDependency
4
- VERSION = "0.3.0"
4
+ VERSION = "0.3.1"
5
5
  end
data/lib/js_dependency.rb CHANGED
@@ -19,7 +19,7 @@ module JsDependency
19
19
  end
20
20
 
21
21
  # @param [String] src_path
22
- # @param [String] target_path
22
+ # @param [Array<String>] target_paths
23
23
  # @param [String] orientation
24
24
  # @param [Hash, nil] alias_paths
25
25
  # @param [Integer] child_analyze_level
@@ -28,23 +28,27 @@ module JsDependency
28
28
  # @param [String, nil] output_path
29
29
  # @param [Array, nil] excludes
30
30
  # @return [String]
31
- 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)
32
32
  output_pathname = Pathname.new(output_path) if output_path
33
33
  index = JsDependency::IndexCreator.call(src_path, alias_paths: alias_paths, excludes: excludes)
34
34
 
35
- target_pathname = JsDependency::TargetPathname.new(target_path)
35
+ nodes = []
36
+ target_paths.each do |target_path|
37
+ target_pathname = JsDependency::TargetPathname.new(target_path)
36
38
 
37
- mermaid_root = JsDependency::Mermaid::Root.new(orientation)
39
+ mermaid_root = JsDependency::Mermaid::Root.new(orientation)
38
40
 
39
- target_pathname.each_parent_path(parent_analyze_level, index) do |parent_path, child_path|
40
- mermaid_root.add(parent_path, child_path)
41
- 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
42
44
 
43
- target_pathname.each_child_path(child_analyze_level, index) do |parent_path, child_path|
44
- mermaid_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)
45
49
  end
46
50
 
47
- output = mermaid_root.export(name_level: name_level, src_path: src_path)
51
+ output = (["flowchart LR"] + nodes.uniq).join("\n")
48
52
  output_pathname&.write(output)
49
53
  output
50
54
  end
@@ -62,12 +66,12 @@ module JsDependency
62
66
  index = JsDependency::IndexCreator.call(src_path, alias_paths: alias_paths, excludes: excludes)
63
67
 
64
68
  target_pathname = JsDependency::TargetPathname.new(target_path)
65
- list = []
69
+ paths = []
66
70
  target_pathname.each_parent_path(parent_analyze_level, index) do |parent_path, _child_path|
67
- list << parent_path
71
+ paths << parent_path
68
72
  end
69
- output = list.uniq.sort.map do |path|
70
- Pathname.new(path).relative_path_from(Pathname.new(src_path).realpath.to_s)
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
71
75
  end
72
76
  output_pathname&.write(output.sort.join("\n"))
73
77
  output
@@ -86,12 +90,12 @@ module JsDependency
86
90
  index = JsDependency::IndexCreator.call(src_path, alias_paths: alias_paths, excludes: excludes)
87
91
 
88
92
  target_pathname = JsDependency::TargetPathname.new(target_path)
89
- list = []
93
+ paths = []
90
94
  target_pathname.each_child_path(child_analyze_level, index) do |_parent_path, child_path|
91
- list << child_path
95
+ paths << child_path
92
96
  end
93
- output = list.uniq.sort.map do |path|
94
- Pathname.new(path).relative_path_from(Pathname.new(src_path).realpath.to_s)
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
95
99
  end
96
100
  output_pathname&.write(output.sort.join("\n"))
97
101
  output
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: js_dependency
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - junara