js_dependency 0.1.1 → 0.2.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: 20217229c2c7f845d05b400f356240d4e2db4d46f9b4d1fc6662868bc1bac372
4
- data.tar.gz: d74ccf10be4764621ff17bc2700c56b8947263b5dc35bccaab49101df90eea59
3
+ metadata.gz: d88ba49b09bf39f743538394281ad7d7956abfd3a3a86e7b34fb88c1ad995f98
4
+ data.tar.gz: 75928de5c657b2af3599606343798f17bfc2839e4967b46a9c9274eef0819218
5
5
  SHA512:
6
- metadata.gz: a842902a4bf0331ea00352315682bf4efb8ac6891dfa2fcfedff0d5ecdfada5d8cf1240ebc6e02a5f8b8ac8a6df54d85c47e4140ac581601d43dbb7a16f3fb2d
7
- data.tar.gz: 919130f6a767b0dc51533cacee935c810da0a8460ab74e86536704fea8614289fa45025c8b3d9edd62a48782bc463e632256b3c9868f849bcb93cf006859c035
6
+ metadata.gz: 27bbd73e5ecd0f49ff5c82608abf7f553802fdce21878dd18036e1d675c09f526a92e152c08aab3ec9b9293c4a446d51b27fa8ceb75bd19a074ab097d9e5248d
7
+ data.tar.gz: 5f328e13c00a1a50352c910902a42323caf558cdd22441218cc03048bebc41da85aea3b767948ce951b373037d7404e57ae293d4e85e12d1f930baef21a2d312
@@ -0,0 +1,11 @@
1
+ src_path: ./src
2
+ target_path: ./src/App.vue
3
+ child_analyze_level: 2
4
+ parent_analyze_level: 2
5
+ name_level: 1
6
+ output_path: ./mermaid.txt
7
+ alias_paths:
8
+ "@": ./pages
9
+ excludes:
10
+ - excludedWord1
11
+ - excludedWord2
data/.rubocop_todo.yml CHANGED
@@ -1,25 +1,40 @@
1
1
  # This configuration was generated by
2
2
  # `rubocop --auto-gen-config`
3
- # on 2022-07-18 12:42:10 UTC using RuboCop version 1.31.2.
3
+ # on 2022-07-20 14:57:17 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: 3
9
+ # Offense count: 6
10
10
  # Configuration parameters: IgnoredMethods, CountRepeatedAttributes.
11
11
  Metrics/AbcSize:
12
- Max: 20
12
+ Max: 37
13
13
 
14
- # Offense count: 3
14
+ # Offense count: 5
15
+ # Configuration parameters: IgnoredMethods.
16
+ Metrics/CyclomaticComplexity:
17
+ Max: 15
18
+
19
+ # Offense count: 8
15
20
  # Configuration parameters: CountComments, CountAsOne, ExcludedMethods, IgnoredMethods.
16
21
  Metrics/MethodLength:
17
- Max: 17
22
+ Max: 26
18
23
 
19
24
  # Offense count: 1
25
+ # Configuration parameters: CountComments, CountAsOne.
26
+ Metrics/ModuleLength:
27
+ Max: 114
28
+
29
+ # Offense count: 3
20
30
  # Configuration parameters: CountKeywordArgs, MaxOptionalParameters.
21
31
  Metrics/ParameterLists:
22
- Max: 8
32
+ Max: 9
33
+
34
+ # Offense count: 5
35
+ # Configuration parameters: IgnoredMethods.
36
+ Metrics/PerceivedComplexity:
37
+ Max: 16
23
38
 
24
39
  # Offense count: 1
25
40
  # This cop supports safe autocorrection (--autocorrect).
@@ -27,9 +42,9 @@ Style/IfUnlessModifier:
27
42
  Exclude:
28
43
  - 'lib/js_dependency/index_creator.rb'
29
44
 
30
- # Offense count: 1
45
+ # Offense count: 2
31
46
  # This cop supports safe autocorrection (--autocorrect).
32
47
  # Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, AllowedPatterns, IgnoredPatterns.
33
48
  # URISchemes: http, https
34
49
  Layout/LineLength:
35
- Max: 167
50
+ Max: 182
data/CHANGELOG.md CHANGED
@@ -1,5 +1,19 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.2.2] - 2022-07-21
4
+
5
+ - Add CLI option parameter "exclude".
6
+ - Add yaml configuration option parameter "excludes" that is array of "exclude".
7
+
8
+ ## [0.2.1] - 2022-07-20
9
+
10
+ - Output parents and children cli with line brake and sort.
11
+ - Output mermaid with parent and child string sorting.
12
+
13
+ ## [0.2.0] - 2022-07-19
14
+
15
+ - Add CLI interface.
16
+
3
17
  ## [0.1.1] - 2022-07-19
4
18
 
5
19
  - Bug fix of double quotation mark.
data/Gemfile.lock CHANGED
@@ -1,8 +1,10 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- js_dependency (0.1.1)
4
+ js_dependency (0.2.2)
5
5
  pathname
6
+ thor
7
+ yaml
6
8
 
7
9
  GEM
8
10
  remote: https://rubygems.org/
@@ -74,11 +76,13 @@ GEM
74
76
  simplecov_json_formatter (~> 0.1)
75
77
  simplecov-html (0.12.3)
76
78
  simplecov_json_formatter (0.1.4)
79
+ thor (1.2.1)
77
80
  trollop (2.9.10)
78
81
  unicode-display_width (2.2.0)
79
82
  unparser (0.6.5)
80
83
  diff-lcs (~> 1.3)
81
84
  parser (>= 3.1.0)
85
+ yaml (0.2.0)
82
86
  yarn (0.1.1)
83
87
  parslet (>= 1.2)
84
88
  rack (>= 1.3)
data/README.md CHANGED
@@ -1,3 +1,5 @@
1
+ [![codecov](https://codecov.io/gh/junara/js_dependency/branch/main/graph/badge.svg?token=GRdbS1xqUa)](https://codecov.io/gh/junara/js_dependency)
2
+
1
3
  # JsDependency
2
4
 
3
5
  Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/js_dependency`. To experiment with that code, run `bin/console` for an interactive prompt.
@@ -15,6 +17,56 @@ If bundler is not being used to manage dependencies, install the gem by executin
15
17
  $ gem install js_dependency
16
18
 
17
19
  ## Usage
20
+ ### By Command Line
21
+
22
+ Configuration file is `./.js_dependency.yml`. This file includes parameters for the analysis.
23
+
24
+ ```yaml
25
+ src_path: ./src # Root folder
26
+ target_path: ./src/App.vue # Target file tha you want to analyze
27
+ child_analyze_level: 2 # Output level of child analyze
28
+ parent_analyze_level: 2 # Output level of parent analyze
29
+ name_level: 1 # Output name level
30
+ output_path: ./mermaid.txt # Output file path
31
+ alias_paths: # Alias path
32
+ "@": ./pages # absolute path or relative path of src_path
33
+ excludes: # Exclude words that you want to skip from export.
34
+ - excludeWord1
35
+ - excludeWord2
36
+ ```
37
+
38
+ Each parameter is overridden by the command line.
39
+
40
+ ```
41
+ src_path -> -s
42
+ target_path -> -t
43
+ child_analyze_level -> -c
44
+ parent_analyze_level -> -p
45
+ name_level -> -n
46
+ output_path -> -o
47
+ exclude -> -e
48
+ ```
49
+
50
+ #### Export Mermaid Format
51
+
52
+ ```shell
53
+ js_dependency -s ./src -t ./src/App.vue -o ./mermaid.txt -c 2 -p 2 -n 1
54
+ ```
55
+
56
+ #### Export parents components list
57
+
58
+ ```shell
59
+ js_dependency parents -s ./src -t ./src/App.vue -o ./parents.txt -p 2
60
+ ```
61
+
62
+ #### Export children components list
63
+
64
+ ```shell
65
+ js_dependency childrent -s ./src -t ./src/App.vue -o ./children.txt -c 2
66
+ ```
67
+
68
+
69
+ ### By ruby code
18
70
  If your javascript code is in `./src` and `./src/App.vue` is in the directory, you can analyze `./src/App.vue` dependency like this:
19
71
 
20
72
  ```ruby
data/exe/js_dependency ADDED
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "js_dependency"
5
+
6
+ JsDependency::Cli.start(ARGV)
@@ -31,7 +31,8 @@ Gem::Specification.new do |spec|
31
31
  spec.require_paths = ["lib"]
32
32
 
33
33
  spec.add_dependency "pathname"
34
-
34
+ spec.add_dependency "thor"
35
+ spec.add_dependency "yaml"
35
36
  spec.add_development_dependency "rspec"
36
37
  spec.add_development_dependency "rspec-parameterized"
37
38
  spec.add_development_dependency "rubocop"
@@ -0,0 +1,121 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "thor"
4
+ require "yaml"
5
+
6
+ module JsDependency
7
+ class Cli < Thor
8
+ default_command :export_mermaid
9
+
10
+ desc "export_mermaid", "Output mermaid flowchart string."
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."
13
+ option :output_path, type: :string, aliases: "-o", desc: "Output file path"
14
+ option :child_analyze_level, type: :numeric, aliases: "-c", desc: "Output level of child dependency"
15
+ option :parent_analyze_level, type: :numeric, aliases: "-p", desc: "Output level of parent dependency"
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"
18
+
19
+ def export_mermaid
20
+ pathname = Pathname.new(".js_dependency.yml")
21
+ args = {}
22
+ args = YAML.safe_load(pathname.read) if pathname.exist?
23
+
24
+ src_path = options[:src_path] || args["src_path"]
25
+ target_path = options[:target_path] || args["target_path"]
26
+ child_analyze_level = options[:child_analyze_level] || args["child_analyze_level"] || 2
27
+ parent_analyze_level = options[:parent_analyze_level] || args["parent_analyze_level"] || 2
28
+ output_path = options[:output_path] || args["output_path"] || nil
29
+ alias_paths = args["alias_paths"] || nil
30
+ name_level = options[:name_level] || args["name_level"] || 1
31
+ excludes = if options[:exclude]
32
+ [options[:exclude]]
33
+ elsif args["excludes"]
34
+ args["excludes"]
35
+ end
36
+
37
+ str = JsDependency.export_mermaid(
38
+ src_path,
39
+ target_path,
40
+ child_analyze_level: child_analyze_level,
41
+ parent_analyze_level: parent_analyze_level,
42
+ output_path: output_path,
43
+ alias_paths: alias_paths,
44
+ name_level: name_level,
45
+ excludes: excludes
46
+ )
47
+
48
+ puts str
49
+ end
50
+
51
+ desc "parents", "export parents list"
52
+ option :src_path, type: :string, aliases: "-s", desc: "Root folder."
53
+ option :target_path, type: :string, aliases: "-t", desc: "Target file that you want to analyze."
54
+ option :output_path, type: :string, aliases: "-o", desc: "Output file path"
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"
57
+
58
+ def parents
59
+ pathname = Pathname.new(".js_dependency.yml")
60
+ args = {}
61
+ args = YAML.safe_load(pathname.read) if pathname.exist?
62
+
63
+ src_path = options[:src_path] || args["src_path"]
64
+ target_path = options[:target_path] || args["target_path"]
65
+ parent_analyze_level = options[:parent_analyze_level] || args["parent_analyze_level"] || 1
66
+ output_path = options[:output_path] || args["output_path"] || nil
67
+ alias_paths = args["alias_paths"] || nil
68
+ excludes = if options[:exclude]
69
+ [options[:exclude]]
70
+ elsif args["excludes"]
71
+ args["excludes"]
72
+ end
73
+
74
+ str = JsDependency.parents(
75
+ src_path,
76
+ target_path,
77
+ parent_analyze_level: parent_analyze_level,
78
+ output_path: output_path,
79
+ alias_paths: alias_paths,
80
+ excludes: excludes
81
+ ).sort.uniq.join("\n")
82
+
83
+ puts str
84
+ end
85
+
86
+ desc "children", "export children list"
87
+ option :src_path, type: :string, aliases: "-s", desc: "Root folder."
88
+ option :target_path, type: :string, aliases: "-t", desc: "Target file that you want to analyze."
89
+ option :output_path, type: :string, aliases: "-o", desc: "Output file path"
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"
92
+
93
+ def children
94
+ pathname = Pathname.new(".js_dependency.yml")
95
+ args = {}
96
+ args = YAML.safe_load(pathname.read) if pathname.exist?
97
+
98
+ src_path = options[:src_path] || args["src_path"]
99
+ target_path = options[:target_path] || args["target_path"]
100
+ child_analyze_level = options[:child_analyze_level] || args["child_analyze_level"] || 1
101
+ output_path = options[:output_path] || args["output_path"] || nil
102
+ alias_paths = args["alias_paths"] || nil
103
+ excludes = if options[:exclude]
104
+ [options[:exclude]]
105
+ elsif args["excludes"]
106
+ args["excludes"]
107
+ end
108
+
109
+ str = JsDependency.children(
110
+ src_path,
111
+ target_path,
112
+ child_analyze_level: child_analyze_level,
113
+ output_path: output_path,
114
+ alias_paths: alias_paths,
115
+ excludes: excludes
116
+ ).sort.uniq.join("\n")
117
+
118
+ puts str
119
+ end
120
+ end
121
+ end
@@ -17,7 +17,7 @@ module JsDependency
17
17
 
18
18
  def export(name_level: 1)
19
19
  str = "flowchart #{orientation}\n"
20
- str + @list.sort_by(&:parent).uniq { |link| "#{link.parent}__#{link.child}" }.map do |link|
20
+ str + @list.uniq { |link| "#{link.parent}__#{link.child}" }.sort_by { |link| "#{link.parent}__#{link.child}" }.map do |link|
21
21
  "#{link.parent_module_name(name_level)} --> #{link.child_module_name(name_level)}"
22
22
  end.join("\n")
23
23
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module JsDependency
4
- VERSION = "0.1.1"
4
+ VERSION = "0.2.2"
5
5
  end
data/lib/js_dependency.rb CHANGED
@@ -3,6 +3,7 @@
3
3
  require_relative "js_dependency/version"
4
4
  require_relative "js_dependency/index_creator"
5
5
  require_relative "js_dependency/mermaid/root"
6
+ require_relative "js_dependency/cli"
6
7
  require "pathname"
7
8
 
8
9
  module JsDependency
@@ -16,8 +17,9 @@ module JsDependency
16
17
  # @param [Integer] parent_analyze_level
17
18
  # @param [Integer] name_level
18
19
  # @param [String, nil] output_path
20
+ # @param [Array, nil] excludes
19
21
  # @return [String]
20
- 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)
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)
21
23
  output_pathname = Pathname.new(output_path) if output_path
22
24
  index = JsDependency::IndexCreator.call(src_path, alias_paths: alias_paths)
23
25
 
@@ -29,11 +31,11 @@ module JsDependency
29
31
 
30
32
  root = JsDependency::Mermaid::Root.new(orientation)
31
33
 
32
- parents_paths(target_pathname, parent_analyze_level, index) do |parent_path, child_path|
34
+ parents_paths(target_pathname, parent_analyze_level, index, excludes: excludes) do |parent_path, child_path|
33
35
  root.add(parent_path, child_path)
34
36
  end
35
37
 
36
- children_paths(target_pathname, child_analyze_level, index) do |parent_path, child_path|
38
+ children_paths(target_pathname, child_analyze_level, index, excludes: excludes) do |parent_path, child_path|
37
39
  root.add(parent_path, child_path)
38
40
  end
39
41
 
@@ -42,6 +44,42 @@ module JsDependency
42
44
  output
43
45
  end
44
46
 
47
+ def self.parents(src_path, target_path, alias_paths: nil, parent_analyze_level: 1, output_path: nil, excludes: nil)
48
+ output_pathname = Pathname.new(output_path) if output_path
49
+ index = JsDependency::IndexCreator.call(src_path, alias_paths: alias_paths)
50
+
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
59
+ end
60
+ output = list.uniq
61
+ output_pathname&.write(output.sort.join("\n"))
62
+ output
63
+ end
64
+
65
+ def self.children(src_path, target_path, alias_paths: nil, child_analyze_level: 1, output_path: nil, excludes: nil)
66
+ output_pathname = Pathname.new(output_path) if output_path
67
+ index = JsDependency::IndexCreator.call(src_path, alias_paths: alias_paths)
68
+
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
77
+ end
78
+ output = list.uniq
79
+ output_pathname&.write(output.sort.join("\n"))
80
+ output
81
+ end
82
+
45
83
  # @param [String] target_path
46
84
  # @param [Hash] index
47
85
  # @return [Array]
@@ -55,6 +93,7 @@ module JsDependency
55
93
  list << parent if children.any?(target_pathname.to_s)
56
94
  end
57
95
  end
96
+
58
97
  private_class_method :extract_parent_paths
59
98
 
60
99
  # @param [String] target_path
@@ -68,6 +107,7 @@ module JsDependency
68
107
  end
69
108
  index[target_pathname.to_s] || []
70
109
  end
110
+
71
111
  private_class_method :extract_children_paths
72
112
 
73
113
  # @param [String] src
@@ -75,9 +115,10 @@ module JsDependency
75
115
  def self.create_index(src_path, alias_paths: nil)
76
116
  JsDependency::IndexCreator.call(src_path, alias_paths: alias_paths)
77
117
  end
118
+
78
119
  private_class_method :create_index
79
120
 
80
- def self.parents_paths(target_pathname, analyze_level, index)
121
+ def self.parents_paths(target_pathname, analyze_level, index, excludes: nil)
81
122
  temp_paths = [target_pathname.to_s]
82
123
  analyze_level.times do
83
124
  list = []
@@ -89,15 +130,18 @@ module JsDependency
89
130
  end
90
131
 
91
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
+
92
135
  yield parent_path, temp_pathname.to_s
93
136
  end
94
137
  end
95
138
  temp_paths = list
96
139
  end
97
140
  end
141
+
98
142
  private_class_method :parents_paths
99
143
 
100
- def self.children_paths(target_pathname, analyze_level, index)
144
+ def self.children_paths(target_pathname, analyze_level, index, excludes: nil)
101
145
  temp_paths = [target_pathname.to_s]
102
146
  analyze_level.times do
103
147
  list = []
@@ -109,11 +153,14 @@ module JsDependency
109
153
  end
110
154
 
111
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
+
112
158
  yield temp_pathname.to_s, child_path
113
159
  end
114
160
  end
115
161
  temp_paths = list
116
162
  end
117
163
  end
164
+
118
165
  private_class_method :children_paths
119
166
  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.1.1
4
+ version: 0.2.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-19 00:00:00.000000000 Z
11
+ date: 2022-07-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: pathname
@@ -24,6 +24,34 @@ dependencies:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: thor
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: yaml
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
27
55
  - !ruby/object:Gem::Dependency
28
56
  name: rspec
29
57
  requirement: !ruby/object:Gem::Requirement
@@ -111,10 +139,12 @@ dependencies:
111
139
  description: Analyze dependency using import for JavaScript.
112
140
  email:
113
141
  - jun5araki@gmail.com
114
- executables: []
142
+ executables:
143
+ - js_dependency
115
144
  extensions: []
116
145
  extra_rdoc_files: []
117
146
  files:
147
+ - ".js_dependency.yml.sample"
118
148
  - ".rspec"
119
149
  - ".rubocop.yml"
120
150
  - ".rubocop_todo.yml"
@@ -125,8 +155,10 @@ files:
125
155
  - LICENSE.txt
126
156
  - README.md
127
157
  - Rakefile
158
+ - exe/js_dependency
128
159
  - js_dependency.gemspec
129
160
  - lib/js_dependency.rb
161
+ - lib/js_dependency/cli.rb
130
162
  - lib/js_dependency/extract_import_path.rb
131
163
  - lib/js_dependency/extract_script_tag.rb
132
164
  - lib/js_dependency/index_creator.rb