js_dependency 0.3.3 → 0.3.5

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: d117ec8b831e67223b243ed652261885f1bbffc4734f93ed2d386166d16d31e2
4
- data.tar.gz: '008fe6a337a677fa62348ca4d76766f02d72affc2b851126f7f22cba45c32c4f'
3
+ metadata.gz: aad89bc5047522fc38815da19020c395e7cd92ec262da2dce9dad5f7ddf40526
4
+ data.tar.gz: dde5992cb4ecf9c27a2e645b064584296809c5dcd26d8d46625d88dbb393b9cb
5
5
  SHA512:
6
- metadata.gz: 891fdc5a595134a7724a4f9bdf061ac6290025e3e624d1f33d362ba74ef22d696a1dd3355f6befdd8a11661a07b531eef7877ff9271b4f4bb1ca01dd83f3c31e
7
- data.tar.gz: 574b0b04401c6b3d4b7a510cb0dbbe972a0539d8e052bed48395cc4c7c1f3ec656bf314f57229e6ac79a2524fbacadf89825d8b30fc410e8e6acb3efd50b438e
6
+ metadata.gz: 6fc657950c57fe7e6960fae9f4c1f1754ca42f223c2989e664ab686212978dd5cd8eb2ca19f610dc73c3492b6a2a17d5eabf6cca6a47bd7dc4c2e81850e04cb9
7
+ data.tar.gz: 51f72bb8a23cd36ed25bc3aa9640d244b1c1534347d94d36cda812a7167046aba9c6d68bef5fbe544e9b2f2810fdd21af74ea0c8be0abf79272142b4b458a635
data/.rubocop_todo.yml CHANGED
@@ -1,20 +1,25 @@
1
1
  # This configuration was generated by
2
2
  # `rubocop --auto-gen-config`
3
- # on 2022-07-26 00:36:01 UTC using RuboCop version 1.31.2.
3
+ # on 2022-07-26 23:22:56 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: 7
9
+ # Offense count: 5
10
10
  # Configuration parameters: IgnoredMethods, CountRepeatedAttributes.
11
11
  Metrics/AbcSize:
12
- Max: 41
12
+ Max: 42
13
+
14
+ # Offense count: 1
15
+ # Configuration parameters: CountComments, CountAsOne.
16
+ Metrics/ClassLength:
17
+ Max: 129
13
18
 
14
19
  # Offense count: 5
15
20
  # Configuration parameters: IgnoredMethods.
16
21
  Metrics/CyclomaticComplexity:
17
- Max: 16
22
+ Max: 18
18
23
 
19
24
  # Offense count: 9
20
25
  # Configuration parameters: CountComments, CountAsOne, ExcludedMethods, IgnoredMethods.
@@ -29,14 +34,14 @@ Metrics/ParameterLists:
29
34
  # Offense count: 5
30
35
  # Configuration parameters: IgnoredMethods.
31
36
  Metrics/PerceivedComplexity:
32
- Max: 17
37
+ Max: 19
33
38
 
34
39
  # Offense count: 1
35
40
  Style/MultilineBlockChain:
36
41
  Exclude:
37
42
  - 'lib/js_dependency/mermaid/root.rb'
38
43
 
39
- # Offense count: 6
44
+ # Offense count: 4
40
45
  # This cop supports safe autocorrection (--autocorrect).
41
46
  # Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, AllowedPatterns, IgnoredPatterns.
42
47
  # URISchemes: http, https
data/CHANGELOG.md CHANGED
@@ -1,5 +1,15 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.3.5] - 2022-07-27
4
+
5
+ - Export orphan components list.
6
+ - Export left components list.
7
+
8
+ ## [0.3.4] - 2022-07-26
9
+
10
+ - Add version CLI command
11
+ - Bug fix excludes option in parents and children commands.
12
+
3
13
  ## [0.3.3] - 2022-07-26
4
14
 
5
15
  - Add special style to target_paths in mermaid output CLI.
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- js_dependency (0.3.3)
4
+ js_dependency (0.3.5)
5
5
  pathname
6
6
  thor
7
7
  yaml
@@ -50,14 +50,14 @@ GEM
50
50
  rspec (>= 2.13, < 4)
51
51
  unparser
52
52
  rspec-support (3.11.0)
53
- rubocop (1.31.2)
53
+ rubocop (1.32.0)
54
54
  json (~> 2.3)
55
55
  parallel (~> 1.10)
56
56
  parser (>= 3.1.0.0)
57
57
  rainbow (>= 2.2.2, < 4.0)
58
58
  regexp_parser (>= 1.8, < 3.0)
59
59
  rexml (>= 3.2.5, < 4.0)
60
- rubocop-ast (>= 1.18.0, < 2.0)
60
+ rubocop-ast (>= 1.19.1, < 2.0)
61
61
  ruby-progressbar (~> 1.7)
62
62
  unicode-display_width (>= 1.4.0, < 3.0)
63
63
  rubocop-ast (1.19.1)
data/README.md CHANGED
@@ -19,6 +19,15 @@ If bundler is not being used to manage dependencies, install the gem by executin
19
19
  ## Usage
20
20
  ### By Command Line
21
21
 
22
+ #### Show version
23
+
24
+ ```shell
25
+ js_dependency version
26
+ # => "X.X.X"
27
+ ```
28
+
29
+ #### yaml configuration file
30
+
22
31
  Configuration file is `./.js_dependency.yml`. This file includes parameters for the analysis.
23
32
 
24
33
  ```yaml
@@ -79,6 +88,21 @@ js_dependency parents -s ./src -t ./src/App.vue -o ./parents.txt -p 2
79
88
  js_dependency childrent -s ./src -t ./src/App.vue -o ./children.txt -c 2
80
89
  ```
81
90
 
91
+ #### Export orphan components list
92
+
93
+ Components that are not used in other components.
94
+
95
+ ```shell
96
+ js_dependency orphan -s ./src
97
+ ```
98
+
99
+ #### Export left components list
100
+
101
+ Components that do not import other components.
102
+
103
+ ```shell
104
+ js_dependency leave -s ./src
105
+ ```
82
106
 
83
107
  ### By ruby code
84
108
  If your javascript code is in `./src` and `./src/App.vue` is in the directory, you can analyze `./src/App.vue` dependency like this:
@@ -28,7 +28,7 @@ module JsDependency
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[:excludes].length.positive?
31
+ excludes = if options[:excludes]&.length&.positive?
32
32
  options[:excludes]
33
33
  elsif args["excludes"]
34
34
  args["excludes"]
@@ -65,7 +65,7 @@ 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[:excludes].length.positive?
68
+ excludes = if options[:excludes]&.length&.positive?
69
69
  options[:excludes]
70
70
  elsif args["excludes"]
71
71
  args["excludes"]
@@ -100,7 +100,7 @@ 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[:excludes].length.positive?
103
+ excludes = if options[:excludes]&.length&.positive?
104
104
  options[:excludes]
105
105
  elsif args["excludes"]
106
106
  args["excludes"]
@@ -117,5 +117,48 @@ module JsDependency
117
117
 
118
118
  puts str
119
119
  end
120
+
121
+ desc "orphan", "export components than is not depended by others"
122
+ option :src_path, type: :string, aliases: "-s", desc: "Root folder."
123
+
124
+ def orphan
125
+ pathname = Pathname.new(".js_dependency.yml")
126
+ args = {}
127
+ args = YAML.safe_load(pathname.read) if pathname.exist?
128
+
129
+ src_path = options[:src_path] || args["src_path"]
130
+ alias_paths = args["alias_paths"] || nil
131
+
132
+ str = JsDependency.orphan(
133
+ src_path,
134
+ alias_paths: alias_paths
135
+ ).join("\n")
136
+
137
+ puts str
138
+ end
139
+
140
+ desc "leave", "export components than is not depended by others"
141
+ option :src_path, type: :string, aliases: "-s", desc: "Root folder."
142
+
143
+ def leave
144
+ pathname = Pathname.new(".js_dependency.yml")
145
+ args = {}
146
+ args = YAML.safe_load(pathname.read) if pathname.exist?
147
+
148
+ src_path = options[:src_path] || args["src_path"]
149
+ alias_paths = args["alias_paths"] || nil
150
+
151
+ str = JsDependency.leave(
152
+ src_path,
153
+ alias_paths: alias_paths
154
+ ).join("\n")
155
+
156
+ puts str
157
+ end
158
+
159
+ desc "version", "show version"
160
+ def version
161
+ puts JsDependency::VERSION
162
+ end
120
163
  end
121
164
  end
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../pathname_utility"
4
+ module JsDependency
5
+ module Mermaid
6
+ class TargetPathname
7
+ attr_accessor :color_css, :font_size_css
8
+
9
+ # @param [String] target_path
10
+ def initialize(target_path)
11
+ @pathname = to_target_pathname(target_path)
12
+ @color_css = "#f9f"
13
+ @font_size_css = "4px"
14
+ end
15
+
16
+ # @param [String] src_path
17
+ # @return [String]
18
+ def mermaid_style(src_path)
19
+ src_pathname = Pathname.new(src_path).realpath
20
+ export_style(parse(@pathname.exist? ? @pathname.relative_path_from(src_pathname) : @pathname).join("_"))
21
+ end
22
+
23
+ private
24
+
25
+ # @param [String] path
26
+ # @return [String]
27
+ def export_style(path)
28
+ "style #{path} stroke:#{@color_css},stroke-width:#{@font_size_css}"
29
+ end
30
+
31
+ # @param [Pathname] pathname
32
+ # @param [Integer] level
33
+ def parse(pathname, level = -1)
34
+ JsDependency::PathnameUtility.parse(pathname, level)
35
+ end
36
+
37
+ # @param [String] target_path
38
+ # @return [Pathname]
39
+ def to_target_pathname(target_path)
40
+ JsDependency::PathnameUtility.to_target_pathname(target_path)
41
+ end
42
+ end
43
+ end
44
+ end
@@ -28,8 +28,23 @@ module JsDependency
28
28
 
29
29
  # @param [Pathname] pathname
30
30
  # @param [Integer] level
31
+ # @return [Array]
31
32
  def self.parse(pathname, level = -1)
32
33
  pathname.each_filename.with_object([]) { |filename, array| array << filename }.reverse[0..level].reverse
33
34
  end
35
+
36
+ # @param [String] path
37
+ # @param [String] src_path
38
+ # @return [String]
39
+ def self.relative_path_or_external_path(path, src_path)
40
+ pathname = Pathname.new(path)
41
+ src_pathname = Pathname.new(src_path)
42
+
43
+ if pathname.exist?
44
+ pathname.realpath.relative_path_from(src_pathname.realpath.to_s).to_s
45
+ else
46
+ pathname.to_s
47
+ end
48
+ end
34
49
  end
35
50
  end
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../pathname_utility"
4
+ module JsDependency
5
+ module SourceAnalysis
6
+ # Components have no dependencies.
7
+ class Leave
8
+ # @param [Hash] index
9
+ # @param [String] src_path
10
+ def initialize(index, src_path)
11
+ @index = index
12
+ @src_path = src_path
13
+ end
14
+
15
+ # @return [Array<String>]
16
+ def call
17
+ left_index = @index.filter do |_target_path, child_paths|
18
+ blank?(child_paths)
19
+ end
20
+
21
+ left_index = left_index.transform_keys do |target_path|
22
+ relative_path_or_external_path(target_path, @src_path)
23
+ end
24
+ left_index.keys.uniq.sort
25
+ end
26
+
27
+ private
28
+
29
+ # @param [Array<String>] paths
30
+ # @return [TrueClass, FalseClass]
31
+ def blank?(paths)
32
+ paths.nil? || paths.empty?
33
+ end
34
+
35
+ # @param [String] path
36
+ # @param [String] src_path
37
+ # @return [String]
38
+ def relative_path_or_external_path(path, src_path)
39
+ JsDependency::PathnameUtility.relative_path_or_external_path(path, src_path)
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,49 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../pathname_utility"
4
+
5
+ module JsDependency
6
+ module SourceAnalysis
7
+ # Components is not depended on.
8
+ class Orphan
9
+ # @param [Hash] index
10
+ # @param [String] src_path
11
+ def initialize(index, src_path)
12
+ @index = index
13
+ @src_path = src_path
14
+ end
15
+
16
+ # @return [Array<String>]
17
+ def call
18
+ orphan_index = @index.filter do |target_path, _child_paths|
19
+ orphan?(target_path)
20
+ end
21
+
22
+ orphan_index = orphan_index.transform_keys do |target_path|
23
+ relative_path_or_external_path(target_path, @src_path)
24
+ end
25
+ orphan_index.keys.uniq.sort
26
+ end
27
+
28
+ private
29
+
30
+ # @param [String] target_path
31
+ # @return [TrueClass, FalseClass]
32
+ def orphan?(target_path)
33
+ target_pathname = JsDependency::TargetPathname.new(target_path)
34
+ paths = []
35
+ target_pathname.each_parent_path(1, @index) do |parent_path, _child_path|
36
+ paths << parent_path
37
+ end
38
+ paths.size.zero?
39
+ end
40
+
41
+ # @param [String] path
42
+ # @param [String] src_path
43
+ # @return [String]
44
+ def relative_path_or_external_path(path, src_path)
45
+ JsDependency::PathnameUtility.relative_path_or_external_path(path, src_path)
46
+ end
47
+ end
48
+ end
49
+ end
@@ -7,13 +7,7 @@ module JsDependency
7
7
 
8
8
  # @param [String] target_path
9
9
  def initialize(target_path)
10
- @pathname = if Pathname.new(target_path).relative? && Pathname.new(target_path).exist?
11
- Pathname.new(target_path).realpath
12
- else
13
- Pathname.new(target_path)
14
- end
15
- @color_css = "#f9f"
16
- @font_size_css = "4px"
10
+ @pathname = to_target_pathname(target_path)
17
11
  end
18
12
 
19
13
  # @param [Integer] analyze_level
@@ -50,19 +44,8 @@ module JsDependency
50
44
  end
51
45
  end
52
46
 
53
- def mermaid_style(src_path)
54
- src_pathname = Pathname.new(src_path).realpath
55
- export_style(parse(@pathname.exist? ? @pathname.relative_path_from(src_pathname) : @pathname).join("_"))
56
- end
57
-
58
47
  private
59
48
 
60
- # @param [String] path
61
- # @return [String]
62
- def export_style(path)
63
- "style #{path} stroke:#{@color_css},stroke-width:#{@font_size_css}"
64
- end
65
-
66
49
  # @param [String] target_path
67
50
  # @param [Hash] index
68
51
  # @return [Array]
@@ -84,11 +67,5 @@ module JsDependency
84
67
  def to_target_pathname(target_path)
85
68
  JsDependency::PathnameUtility.to_target_pathname(target_path)
86
69
  end
87
-
88
- # @param [Pathname] pathname
89
- # @param [Integer] level
90
- def parse(pathname, level = -1)
91
- JsDependency::PathnameUtility.parse(pathname, level)
92
- end
93
70
  end
94
71
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module JsDependency
4
- VERSION = "0.3.3"
4
+ VERSION = "0.3.5"
5
5
  end
data/lib/js_dependency.rb CHANGED
@@ -5,6 +5,10 @@ require_relative "js_dependency/index_creator"
5
5
  require_relative "js_dependency/mermaid/root"
6
6
  require_relative "js_dependency/cli"
7
7
  require_relative "js_dependency/target_pathname"
8
+ require_relative "js_dependency/mermaid/target_pathname"
9
+ require_relative "js_dependency/source_analysis/leave"
10
+ require_relative "js_dependency/source_analysis/orphan"
11
+ require_relative "js_dependency/pathname_utility"
8
12
  require "pathname"
9
13
 
10
14
  module JsDependency
@@ -18,6 +22,22 @@ module JsDependency
18
22
  JsDependency::IndexCreator.call(src_path, alias_paths: alias_paths, excludes: excludes)
19
23
  end
20
24
 
25
+ # @param [String] src_path
26
+ # @param [Hash, nil] alias_paths
27
+ # @return [Array<String>]
28
+ def self.leave(src_path, alias_paths: nil)
29
+ index = JsDependency::IndexCreator.call(src_path, alias_paths: alias_paths)
30
+ JsDependency::SourceAnalysis::Leave.new(index, src_path).call
31
+ end
32
+
33
+ # @param [String] src_path
34
+ # @param [Hash, nil] alias_paths
35
+ # @return [Array<String>]
36
+ def self.orphan(src_path, alias_paths: nil)
37
+ index = JsDependency::IndexCreator.call(src_path, alias_paths: alias_paths)
38
+ JsDependency::SourceAnalysis::Orphan.new(index, src_path).call
39
+ end
40
+
21
41
  # @param [String] src_path
22
42
  # @param [Array<String>] target_paths
23
43
  # @param [String] orientation
@@ -35,10 +55,10 @@ module JsDependency
35
55
  nodes = []
36
56
  styles = []
37
57
  target_paths.each do |target_path|
38
- target_pathname = JsDependency::TargetPathname.new(target_path)
39
- styles += [target_pathname.mermaid_style(src_path)]
58
+ styles += [JsDependency::Mermaid::TargetPathname.new(target_path).mermaid_style(src_path)]
40
59
  mermaid_root = JsDependency::Mermaid::Root.new(orientation)
41
60
 
61
+ target_pathname = JsDependency::TargetPathname.new(target_path)
42
62
  target_pathname.each_parent_path(parent_analyze_level, index) do |parent_path, child_path|
43
63
  mermaid_root.add(parent_path, child_path)
44
64
  end
@@ -72,7 +92,7 @@ module JsDependency
72
92
  paths << parent_path
73
93
  end
74
94
  output = paths.uniq.sort.map do |path|
75
- 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
+ JsDependency::PathnameUtility.relative_path_or_external_path(path, src_path)
76
96
  end
77
97
  output_pathname&.write(output.sort.join("\n"))
78
98
  output
@@ -96,7 +116,7 @@ module JsDependency
96
116
  paths << child_path
97
117
  end
98
118
  output = paths.uniq.sort.map do |path|
99
- Pathname.new(path).exist? ? Pathname.new(path).relative_path_from(Pathname.new(src_path).realpath.to_s).to_s : Pathname.new(path).to_s
119
+ JsDependency::PathnameUtility.relative_path_or_external_path(path, src_path)
100
120
  end
101
121
  output_pathname&.write(output.sort.join("\n"))
102
122
  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.3
4
+ version: 0.3.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - junara
@@ -164,8 +164,11 @@ files:
164
164
  - lib/js_dependency/index_creator.rb
165
165
  - lib/js_dependency/mermaid/nodes_link.rb
166
166
  - lib/js_dependency/mermaid/root.rb
167
+ - lib/js_dependency/mermaid/target_pathname.rb
167
168
  - lib/js_dependency/pathname_utility.rb
168
169
  - lib/js_dependency/replace_path_alias.rb
170
+ - lib/js_dependency/source_analysis/leave.rb
171
+ - lib/js_dependency/source_analysis/orphan.rb
169
172
  - lib/js_dependency/target_pathname.rb
170
173
  - lib/js_dependency/version.rb
171
174
  - sig/js_dependency.rbs