js_dependency 0.3.5 → 0.3.8

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: aad89bc5047522fc38815da19020c395e7cd92ec262da2dce9dad5f7ddf40526
4
- data.tar.gz: dde5992cb4ecf9c27a2e645b064584296809c5dcd26d8d46625d88dbb393b9cb
3
+ metadata.gz: a376c442837e1a87365b91b1f13b46e048f271078e691425350237066e1f567d
4
+ data.tar.gz: 606813cc029e62b21c7073409c8312c27104adada8b3e3e33877075fde15d2cc
5
5
  SHA512:
6
- metadata.gz: 6fc657950c57fe7e6960fae9f4c1f1754ca42f223c2989e664ab686212978dd5cd8eb2ca19f610dc73c3492b6a2a17d5eabf6cca6a47bd7dc4c2e81850e04cb9
7
- data.tar.gz: 51f72bb8a23cd36ed25bc3aa9640d244b1c1534347d94d36cda812a7167046aba9c6d68bef5fbe544e9b2f2810fdd21af74ea0c8be0abf79272142b4b458a635
6
+ metadata.gz: da8c831e0eec9e3251597874af4b067efb2079c651e46341beb5a8f34b70b857c2960f134cd0db6fc8e8cf380090b70d87732891d8161ecfbdfe7756179d7cba
7
+ data.tar.gz: 3513921cc2068f220fcd5ca001d59339a361e36fe76c9bf9ca5f4bc4f5ba5cf91ac0f220c72814cad02a44e6ed9ddc11421721a6d20d1f1a010b7bec5422893f
data/.rubocop_todo.yml CHANGED
@@ -1,38 +1,33 @@
1
1
  # This configuration was generated by
2
2
  # `rubocop --auto-gen-config`
3
- # on 2022-07-26 23:22:56 UTC using RuboCop version 1.31.2.
3
+ # on 2022-08-11 12:19:56 UTC using RuboCop version 1.34.1.
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: 5
10
- # Configuration parameters: IgnoredMethods, CountRepeatedAttributes.
9
+ # Offense count: 3
10
+ # Configuration parameters: AllowedMethods, AllowedPatterns, IgnoredMethods, CountRepeatedAttributes.
11
11
  Metrics/AbcSize:
12
- Max: 42
13
-
14
- # Offense count: 1
15
- # Configuration parameters: CountComments, CountAsOne.
16
- Metrics/ClassLength:
17
- Max: 129
12
+ Max: 31
18
13
 
19
- # Offense count: 5
20
- # Configuration parameters: IgnoredMethods.
14
+ # Offense count: 3
15
+ # Configuration parameters: AllowedMethods, AllowedPatterns, IgnoredMethods.
21
16
  Metrics/CyclomaticComplexity:
22
17
  Max: 18
23
18
 
24
- # Offense count: 9
25
- # Configuration parameters: CountComments, CountAsOne, ExcludedMethods, IgnoredMethods.
19
+ # Offense count: 8
20
+ # Configuration parameters: CountComments, CountAsOne, ExcludedMethods, AllowedMethods, AllowedPatterns, IgnoredMethods.
26
21
  Metrics/MethodLength:
27
- Max: 26
22
+ Max: 19
28
23
 
29
24
  # Offense count: 3
30
25
  # Configuration parameters: CountKeywordArgs, MaxOptionalParameters.
31
26
  Metrics/ParameterLists:
32
27
  Max: 9
33
28
 
34
- # Offense count: 5
35
- # Configuration parameters: IgnoredMethods.
29
+ # Offense count: 3
30
+ # Configuration parameters: AllowedMethods, AllowedPatterns, IgnoredMethods.
36
31
  Metrics/PerceivedComplexity:
37
32
  Max: 19
38
33
 
@@ -41,7 +36,7 @@ Style/MultilineBlockChain:
41
36
  Exclude:
42
37
  - 'lib/js_dependency/mermaid/root.rb'
43
38
 
44
- # Offense count: 4
39
+ # Offense count: 6
45
40
  # This cop supports safe autocorrection (--autocorrect).
46
41
  # Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, AllowedPatterns, IgnoredPatterns.
47
42
  # URISchemes: http, https
data/CHANGELOG.md CHANGED
@@ -1,5 +1,19 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.3.8] - 2022-08-11
4
+
5
+ - `.js_dependecy.yaml` is arrowed for configuration file name.
6
+ - Refactor codes.
7
+ - Add test codes.
8
+
9
+ ## [0.3.7] - 2022-08-11
10
+
11
+ - If target is index.js, orphan include in analysis.
12
+
13
+ ## [0.3.6] - 2022-08-11
14
+
15
+ - Add src path in script tag for create index.
16
+
3
17
  ## [0.3.5] - 2022-07-27
4
18
 
5
19
  - Export orphan components list.
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- js_dependency (0.3.5)
4
+ js_dependency (0.3.8)
5
5
  pathname
6
6
  thor
7
7
  yaml
@@ -18,7 +18,7 @@ GEM
18
18
  docile (1.4.0)
19
19
  json (2.6.2)
20
20
  parallel (1.22.1)
21
- parser (3.1.2.0)
21
+ parser (3.1.2.1)
22
22
  ast (~> 2.4.1)
23
23
  parslet (2.0.0)
24
24
  pathname (0.2.0)
@@ -50,17 +50,17 @@ GEM
50
50
  rspec (>= 2.13, < 4)
51
51
  unparser
52
52
  rspec-support (3.11.0)
53
- rubocop (1.32.0)
53
+ rubocop (1.34.1)
54
54
  json (~> 2.3)
55
55
  parallel (~> 1.10)
56
- parser (>= 3.1.0.0)
56
+ parser (>= 3.1.2.1)
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.19.1, < 2.0)
60
+ rubocop-ast (>= 1.20.0, < 2.0)
61
61
  ruby-progressbar (~> 1.7)
62
62
  unicode-display_width (>= 1.4.0, < 3.0)
63
- rubocop-ast (1.19.1)
63
+ rubocop-ast (1.21.0)
64
64
  parser (>= 3.1.1.0)
65
65
  rubocop-performance (1.14.3)
66
66
  rubocop (>= 1.7.0, < 2.0)
data/README.md CHANGED
@@ -6,6 +6,8 @@ Welcome to your new gem! In this directory, you'll find the files you need to be
6
6
 
7
7
  Analyze import dependency of JavaScript code and export mermaid format.
8
8
 
9
+ "import dependency" is analyzed from import statement of JavaScript code and src path that is added to script tag.
10
+
9
11
  ## Installation
10
12
 
11
13
  Install the gem and add to the application's Gemfile by executing:
@@ -8,152 +8,91 @@ module JsDependency
8
8
  default_command :export_mermaid
9
9
 
10
10
  desc "export_mermaid", "Output mermaid flowchart string."
11
- option :src_path, type: :string, aliases: "-s", desc: "Root folder."
12
- option :target_paths, type: :array, 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 :excludes, type: :array, aliases: "-e", desc: "Exclude the word that is included in the path"
11
+ method_option :src_path, type: :string, aliases: "-s", desc: "Root folder."
12
+ method_option :target_paths, type: :array, aliases: "-t", desc: "Target file that you want to analyze."
13
+ method_option :output_path, type: :string, aliases: "-o", desc: "Output file path"
14
+ method_option :child_analyze_level, type: :numeric, aliases: "-c", desc: "Output level of child dependency"
15
+ method_option :parent_analyze_level, type: :numeric, aliases: "-p", desc: "Output level of parent dependency"
16
+ method_option :name_level, type: :numeric, aliases: "-n", desc: "Output name level"
17
+ method_option :excludes, type: :array, aliases: "-e", desc: "Exclude the word that is included in the path"
18
18
 
19
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_paths = options[:target_paths] || (args["target_path"].is_a?(String) ? [args["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[:excludes]&.length&.positive?
32
- options[:excludes]
33
- elsif args["excludes"]
34
- args["excludes"]
35
- end
36
-
37
- str = JsDependency.export_mermaid(
38
- src_path,
39
- target_paths,
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
20
+ args = JsDependency::CliUtils::Yaml.new.args
21
+ config = JsDependency::CliUtils::Config.new(options, args)
22
+ puts JsDependency.export_mermaid(
23
+ config.src_path,
24
+ config.target_paths,
25
+ child_analyze_level: config.child_analyze_level,
26
+ parent_analyze_level: config.parent_analyze_level,
27
+ output_path: config.output_path,
28
+ alias_paths: config.alias_paths,
29
+ name_level: config.name_level,
30
+ excludes: config.excludes
46
31
  )
47
-
48
- puts str
49
32
  end
50
33
 
51
34
  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 :excludes, type: :array, aliases: "-e", desc: "Exclude the word that is included in the path"
35
+ method_option :src_path, type: :string, aliases: "-s", desc: "Root folder."
36
+ method_option :target_path, type: :string, aliases: "-t", desc: "Target file that you want to analyze."
37
+ method_option :output_path, type: :string, aliases: "-o", desc: "Output file path"
38
+ method_option :parent_analyze_level, type: :numeric, aliases: "-p", desc: "Output level of parent dependency"
39
+ method_option :excludes, type: :array, aliases: "-e", desc: "Exclude the word that is included in the path"
57
40
 
58
41
  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[:excludes]&.length&.positive?
69
- options[:excludes]
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
42
+ args = JsDependency::CliUtils::Yaml.new.args
43
+ config = JsDependency::CliUtils::Config.new(options, args)
44
+ puts JsDependency.parents(
45
+ config.src_path,
46
+ config.target_path,
47
+ parent_analyze_level: config.parent_analyze_level,
48
+ output_path: config.output_path,
49
+ alias_paths: config.alias_paths,
50
+ excludes: config.excludes
81
51
  ).join("\n")
82
-
83
- puts str
84
52
  end
85
53
 
86
54
  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 :excludes, type: :array, aliases: "-e", desc: "Exclude the word that is included in the path"
55
+ method_option :src_path, type: :string, aliases: "-s", desc: "Root folder."
56
+ method_option :target_path, type: :string, aliases: "-t", desc: "Target file that you want to analyze."
57
+ method_option :output_path, type: :string, aliases: "-o", desc: "Output file path"
58
+ method_option :child_analyze_level, type: :numeric, aliases: "-c", desc: "Output level of child dependency"
59
+ method_option :excludes, type: :array, aliases: "-e", desc: "Exclude the word that is included in the path"
92
60
 
93
61
  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[:excludes]&.length&.positive?
104
- options[:excludes]
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
62
+ args = JsDependency::CliUtils::Yaml.new.args
63
+ config = JsDependency::CliUtils::Config.new(options, args)
64
+ puts JsDependency.children(
65
+ config.src_path,
66
+ config.target_path,
67
+ child_analyze_level: config.child_analyze_level,
68
+ output_path: config.output_path,
69
+ alias_paths: config.alias_paths,
70
+ excludes: config.excludes
116
71
  ).join("\n")
117
-
118
- puts str
119
72
  end
120
73
 
121
74
  desc "orphan", "export components than is not depended by others"
122
- option :src_path, type: :string, aliases: "-s", desc: "Root folder."
75
+ method_option :src_path, type: :string, aliases: "-s", desc: "Root folder."
123
76
 
124
77
  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
78
+ args = JsDependency::CliUtils::Yaml.new.args
79
+ config = JsDependency::CliUtils::Config.new(options, args)
80
+ puts JsDependency.orphan(
81
+ config.src_path,
82
+ alias_paths: config.alias_paths
135
83
  ).join("\n")
136
-
137
- puts str
138
84
  end
139
85
 
140
86
  desc "leave", "export components than is not depended by others"
141
- option :src_path, type: :string, aliases: "-s", desc: "Root folder."
87
+ method_option :src_path, type: :string, aliases: "-s", desc: "Root folder."
142
88
 
143
89
  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
90
+ args = JsDependency::CliUtils::Yaml.new.args
91
+ config = JsDependency::CliUtils::Config.new(options, args)
92
+ puts JsDependency.leave(
93
+ config.src_path,
94
+ alias_paths: config.alias_paths
154
95
  ).join("\n")
155
-
156
- puts str
157
96
  end
158
97
 
159
98
  desc "version", "show version"
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ module JsDependency
4
+ module CliUtils
5
+ class Config
6
+ attr_reader :src_path, :target_paths, :target_path, :child_analyze_level, :parent_analyze_level, :output_path,
7
+ :alias_paths, :name_level, :excludes
8
+
9
+ # @param [Hash] options
10
+ # @param [Hash] args
11
+ def initialize(options, args)
12
+ @src_path = options[:src_path] || args[:src_path]
13
+ @target_paths = options[:target_paths] || (args[:target_path].is_a?(String) ? [args[:target_path]] : args[:target_path])
14
+ @target_path = options[:target_path] || args[:target_path]
15
+ @child_analyze_level = options[:child_analyze_level] || args[:child_analyze_level] || 2
16
+ @parent_analyze_level = options[:parent_analyze_level] || args[:parent_analyze_level] || 2
17
+ @output_path = options[:output_path] || args[:output_path] || nil
18
+ @alias_paths = args[:alias_paths] || nil
19
+ @name_level = options[:name_level] || args[:name_level] || 1
20
+ @excludes = if options[:excludes]&.length&.positive?
21
+ options[:excludes]
22
+ elsif args[:excludes]
23
+ args[:excludes]
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ module JsDependency
4
+ module CliUtils
5
+ class Yaml
6
+ attr_reader :args,
7
+ :dir_path
8
+
9
+ # @param [String, nil] dir_path
10
+ def initialize(dir_path: nil)
11
+ @dir_path = dir_path || Dir.pwd
12
+ pathname = config_pathname
13
+ @args = if pathname.nil?
14
+ {}
15
+ else
16
+ symbolize_keys(YAML.safe_load(pathname.read))
17
+ end
18
+ end
19
+
20
+ private
21
+
22
+ # @return [Pathname, nil]
23
+ def config_pathname
24
+ dir_pathname = Pathname.new(@dir_path)
25
+ pathname = nil
26
+ %w[.js_dependency.yml .js_dependency.yaml].each do |path|
27
+ pathname = (dir_pathname + path) if (dir_pathname + path).exist?
28
+ end
29
+ pathname
30
+ end
31
+
32
+ # @param [Hash] hash
33
+ # @return [Hash]
34
+ def symbolize_keys(hash)
35
+ hash.transform_keys(&:to_sym)
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,80 @@
1
+ # frozen_string_literal: true
2
+
3
+ module JsDependency
4
+ module Extractor
5
+ class ExtractImportPath
6
+ # @param [String] str
7
+ def initialize(str)
8
+ @str = str
9
+ end
10
+
11
+ # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
12
+ # @return [Array<String>]
13
+ def call
14
+ str = @str
15
+ # import defaultExport from 'module-name';
16
+ paths = str.gsub(/import\s+\S+\s+from\s+"([^"]+)"/).with_object([]) { |_, list| list << Regexp.last_match(1) }
17
+ paths += str.gsub(/import\s+\S+\s+from\s+'([^']+)'/).with_object([]) { |_, list| list << Regexp.last_match(1) }
18
+
19
+ # import * as name from \"module-name\";
20
+ paths += str.gsub(/import\s+\S+\s+as\s+\S+\s+from\s+"([^"]+)"/).with_object([]) do |_, list|
21
+ list << Regexp.last_match(1)
22
+ end
23
+ paths += str.gsub(/import\s+\S+\s+as\s+\S+\s+from\s+'([^']+)'/).with_object([]) do |_, list|
24
+ list << Regexp.last_match(1)
25
+ end
26
+
27
+ # import { export1 } from \"module-name\";
28
+ # import { export1 as alias1 } from "module-name";
29
+ # import { export1 , export2 } from "module-name";
30
+ # import { foo , bar } from "module-name/path/to/specific/un-exported/file";
31
+ # import { export1 , export2 as alias2 , [...] } from "module-name";
32
+ paths += str.gsub(/import\s+\{\s+.+\s+\}\s+from\s+"([^"]+)"/).with_object([]) do |_, list|
33
+ list << Regexp.last_match(1)
34
+ end
35
+ paths += str.gsub(/import\s+\{\s+.+\s+\}\s+from\s+'([^']+)'/).with_object([]) do |_, list|
36
+ list << Regexp.last_match(1)
37
+ end
38
+
39
+ # import defaultExport, { export1 [ , [...] ] } from "module-name";
40
+ paths += str.gsub(/import\s+\S+,\s+\{\s+.+\s+\}\s+from\s+"([^"]+)"/).with_object([]) do |_, list|
41
+ list << Regexp.last_match(1)
42
+ end
43
+ paths += str.gsub(/import\s+\S+,\s+\{\s+.+\s+\}\s+from\s+'([^']+)'/).with_object([]) do |_, list|
44
+ list << Regexp.last_match(1)
45
+ end
46
+
47
+ # import defaultExport, * as name from "module-name";
48
+ paths += str.gsub(/import\s+\S+,\s+.+\s+as\s+\S+\s+from\s+"([^"]+)"/).with_object([]) do |_, list|
49
+ list << Regexp.last_match(1)
50
+ end
51
+ paths += str.gsub(/import\s+\S+,\s+.+\s+as\s+\S+\s+from\s+'([^']+)'/).with_object([]) do |_, list|
52
+ list << Regexp.last_match(1)
53
+ end
54
+
55
+ # import "module-name";
56
+ paths += str.gsub(/import\s+"([^"]+)"/).with_object([]) do |_, list|
57
+ list << Regexp.last_match(1)
58
+ end
59
+ paths += str.gsub(/import\s+'([^']+)'/).with_object([]) do |_, list|
60
+ list << Regexp.last_match(1)
61
+ end
62
+
63
+ # var promise = import("module-name");
64
+ paths += str.gsub(/import\("([^"]+)"\)/).with_object([]) do |_, list|
65
+ list << Regexp.last_match(1)
66
+ end
67
+ paths += str.gsub(/import\('([^']+)'\)/).with_object([]) do |_, list|
68
+ list << Regexp.last_match(1)
69
+ end
70
+ paths.uniq.sort
71
+ end
72
+
73
+ # rubocop:enable Metrics/MethodLength, Metrics/AbcSize
74
+
75
+ def self.call(str)
76
+ new(str).call
77
+ end
78
+ end
79
+ end
80
+ end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ module JsDependency
4
+ module Extractor
5
+ class ExtractScriptTag
6
+ # @param [String] str
7
+ def initialize(str)
8
+ @str = str
9
+ end
10
+
11
+ # @return [Array<String>]
12
+ def call
13
+ str = @str
14
+ scripts = str.gsub(%r{<script>(.+)</script>}m).with_object([]) do |_, list|
15
+ list << Regexp.last_match(1)
16
+ end
17
+
18
+ scripts.uniq.sort.join("\n")
19
+ end
20
+
21
+ # @param [String] str
22
+ # @return [String]
23
+ def self.call(str)
24
+ new(str).call
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ module JsDependency
4
+ module Extractor
5
+ class ExtractSrcPath
6
+ # @param [String] str
7
+ def initialize(str)
8
+ @str = str
9
+ end
10
+
11
+ # Extract JavaScript file (.js) in src path
12
+ # @return [Array<String>]
13
+ def call
14
+ str = @str
15
+ # <script src="module-name.js">
16
+ paths = str.gsub(/<script\s+src="([^']+)">/).with_object([]) { |_, list| list << Regexp.last_match(1) }
17
+ paths += str.gsub(/<script\s+src='([^']+)'>/).with_object([]) { |_, list| list << Regexp.last_match(1) }
18
+
19
+ filter_javascript_paths(paths).uniq.sort
20
+ end
21
+
22
+ def self.call(str)
23
+ new(str).call
24
+ end
25
+
26
+ private
27
+
28
+ # Filter JavaScript file (.js) from Array of String
29
+ # @param [Array<String>] paths
30
+ # @return [Array<String>]
31
+ def filter_javascript_paths(paths)
32
+ paths.filter do |path|
33
+ path.end_with?(".js")
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
@@ -1,7 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative "extract_script_tag"
4
- require_relative "extract_import_path"
3
+ require_relative "extractor/extract_script_tag"
4
+ require_relative "extractor/extract_import_path"
5
+ require_relative "extractor/extract_src_path"
5
6
  require_relative "replace_path_alias"
6
7
  require_relative "pathname_utility"
7
8
 
@@ -45,6 +46,7 @@ module JsDependency
45
46
 
46
47
  src_pathname.glob(pattern).each_with_object({}) do |component_pathname, obj|
47
48
  import_pathnames = import_pathnames_from(component_pathname, alias_paths)
49
+ import_pathnames += src_javascript_pathnames_from(component_pathname, alias_paths)
48
50
 
49
51
  obj[component_pathname.to_s] = import_pathnames.map(&:to_s)
50
52
  if component_pathname.basename.to_s == "index.js"
@@ -60,11 +62,22 @@ module JsDependency
60
62
  def import_pathnames_from(component_pathname, alias_paths)
61
63
  component_dirname = component_pathname.dirname
62
64
  script_str = extract_script_string(component_pathname)
63
- JsDependency::ExtractImportPath.call(script_str).map do |import_path|
65
+ JsDependency::Extractor::ExtractImportPath.call(script_str).map do |import_path|
64
66
  standardize_path(import_path, alias_paths, component_dirname)
65
67
  end
66
68
  end
67
69
 
70
+ # @param [Pathname] component_pathname
71
+ # @param [Array<String>] alias_paths
72
+ # @return [Array<Pathname>]
73
+ def src_javascript_pathnames_from(component_pathname, alias_paths)
74
+ component_dirname = component_pathname.dirname
75
+ str = component_pathname.read
76
+ JsDependency::Extractor::ExtractSrcPath.call(str).map do |src_path|
77
+ standardize_path(src_path, alias_paths, component_dirname)
78
+ end
79
+ end
80
+
68
81
  # @param [String] import_path
69
82
  # @param [Array<String>] alias_paths
70
83
  # @param [Pathname] component_dirname
@@ -91,7 +104,7 @@ module JsDependency
91
104
  extname = pathname.extname
92
105
  return str unless extname == ".vue"
93
106
 
94
- JsDependency::ExtractScriptTag.call(str)
107
+ JsDependency::Extractor::ExtractScriptTag.call(str)
95
108
  end
96
109
 
97
110
  # @param [String] path
@@ -30,11 +30,9 @@ module JsDependency
30
30
  # @param [String] target_path
31
31
  # @return [TrueClass, FalseClass]
32
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
33
+ paths = parent_paths(target_path)
34
+ paths += dir_parent_paths(target_path) if target_path.include?("index.js")
35
+
38
36
  paths.size.zero?
39
37
  end
40
38
 
@@ -44,6 +42,30 @@ module JsDependency
44
42
  def relative_path_or_external_path(path, src_path)
45
43
  JsDependency::PathnameUtility.relative_path_or_external_path(path, src_path)
46
44
  end
45
+
46
+ # Parent paths.
47
+ # @param [String] target_path
48
+ # @return [Array<String>]
49
+ def parent_paths(target_path)
50
+ target_pathname = JsDependency::TargetPathname.new(target_path)
51
+ paths = []
52
+ target_pathname.each_parent_path(1, @index) do |parent_path, _child_path|
53
+ paths << parent_path
54
+ end
55
+ paths
56
+ end
57
+
58
+ # Directory parent paths.
59
+ # @param [String] target_path
60
+ # @return [Array<String>]
61
+ def dir_parent_paths(target_path)
62
+ paths = []
63
+ target_pathname_dir = JsDependency::TargetPathname.new(Pathname.new(target_path).dirname.to_s)
64
+ target_pathname_dir.each_parent_path(1, @index) do |parent_path, _child_path|
65
+ paths << parent_path
66
+ end
67
+ paths
68
+ end
47
69
  end
48
70
  end
49
71
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module JsDependency
4
- VERSION = "0.3.5"
4
+ VERSION = "0.3.8"
5
5
  end
data/lib/js_dependency.rb CHANGED
@@ -9,6 +9,8 @@ require_relative "js_dependency/mermaid/target_pathname"
9
9
  require_relative "js_dependency/source_analysis/leave"
10
10
  require_relative "js_dependency/source_analysis/orphan"
11
11
  require_relative "js_dependency/pathname_utility"
12
+ require_relative "js_dependency/cli_utils/yaml"
13
+ require_relative "js_dependency/cli_utils/config"
12
14
  require "pathname"
13
15
 
14
16
  module JsDependency
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.3.5
4
+ version: 0.3.8
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-26 00:00:00.000000000 Z
11
+ date: 2022-08-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: pathname
@@ -159,8 +159,11 @@ files:
159
159
  - js_dependency.gemspec
160
160
  - lib/js_dependency.rb
161
161
  - lib/js_dependency/cli.rb
162
- - lib/js_dependency/extract_import_path.rb
163
- - lib/js_dependency/extract_script_tag.rb
162
+ - lib/js_dependency/cli_utils/config.rb
163
+ - lib/js_dependency/cli_utils/yaml.rb
164
+ - lib/js_dependency/extractor/extract_import_path.rb
165
+ - lib/js_dependency/extractor/extract_script_tag.rb
166
+ - lib/js_dependency/extractor/extract_src_path.rb
164
167
  - lib/js_dependency/index_creator.rb
165
168
  - lib/js_dependency/mermaid/nodes_link.rb
166
169
  - lib/js_dependency/mermaid/root.rb
@@ -1,77 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module JsDependency
4
- class ExtractImportPath
5
- # @param [String] str
6
- def initialize(str)
7
- @str = str
8
- end
9
-
10
- # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
11
- # @return [Array<String>]
12
- def call
13
- str = @str
14
- # import defaultExport from 'module-name';
15
- paths = str.gsub(/import\s+\S+\s+from\s+"([^"]+)"/).with_object([]) { |_, list| list << Regexp.last_match(1) }
16
- paths += str.gsub(/import\s+\S+\s+from\s+'([^']+)'/).with_object([]) { |_, list| list << Regexp.last_match(1) }
17
-
18
- # import * as name from \"module-name\";
19
- paths += str.gsub(/import\s+\S+\s+as\s+\S+\s+from\s+"([^"]+)"/).with_object([]) do |_, list|
20
- list << Regexp.last_match(1)
21
- end
22
- paths += str.gsub(/import\s+\S+\s+as\s+\S+\s+from\s+'([^']+)'/).with_object([]) do |_, list|
23
- list << Regexp.last_match(1)
24
- end
25
-
26
- # import { export1 } from \"module-name\";
27
- # import { export1 as alias1 } from "module-name";
28
- # import { export1 , export2 } from "module-name";
29
- # import { foo , bar } from "module-name/path/to/specific/un-exported/file";
30
- # import { export1 , export2 as alias2 , [...] } from "module-name";
31
- paths += str.gsub(/import\s+\{\s+.+\s+\}\s+from\s+"([^"]+)"/).with_object([]) do |_, list|
32
- list << Regexp.last_match(1)
33
- end
34
- paths += str.gsub(/import\s+\{\s+.+\s+\}\s+from\s+'([^']+)'/).with_object([]) do |_, list|
35
- list << Regexp.last_match(1)
36
- end
37
-
38
- # import defaultExport, { export1 [ , [...] ] } from "module-name";
39
- paths += str.gsub(/import\s+\S+,\s+\{\s+.+\s+\}\s+from\s+"([^"]+)"/).with_object([]) do |_, list|
40
- list << Regexp.last_match(1)
41
- end
42
- paths += str.gsub(/import\s+\S+,\s+\{\s+.+\s+\}\s+from\s+'([^']+)'/).with_object([]) do |_, list|
43
- list << Regexp.last_match(1)
44
- end
45
-
46
- # import defaultExport, * as name from "module-name";
47
- paths += str.gsub(/import\s+\S+,\s+.+\s+as\s+\S+\s+from\s+"([^"]+)"/).with_object([]) do |_, list|
48
- list << Regexp.last_match(1)
49
- end
50
- paths += str.gsub(/import\s+\S+,\s+.+\s+as\s+\S+\s+from\s+'([^']+)'/).with_object([]) do |_, list|
51
- list << Regexp.last_match(1)
52
- end
53
-
54
- # import "module-name";
55
- paths += str.gsub(/import\s+"([^"]+)"/).with_object([]) do |_, list|
56
- list << Regexp.last_match(1)
57
- end
58
- paths += str.gsub(/import\s+'([^']+)'/).with_object([]) do |_, list|
59
- list << Regexp.last_match(1)
60
- end
61
-
62
- # var promise = import("module-name");
63
- paths += str.gsub(/import\("([^"]+)"\)/).with_object([]) do |_, list|
64
- list << Regexp.last_match(1)
65
- end
66
- paths += str.gsub(/import\('([^']+)'\)/).with_object([]) do |_, list|
67
- list << Regexp.last_match(1)
68
- end
69
- paths.uniq.sort
70
- end
71
- # rubocop:enable Metrics/MethodLength, Metrics/AbcSize
72
-
73
- def self.call(str)
74
- new(str).call
75
- end
76
- end
77
- end
@@ -1,26 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module JsDependency
4
- class ExtractScriptTag
5
- # @param [String] str
6
- def initialize(str)
7
- @str = str
8
- end
9
-
10
- # @return [Array<String>]
11
- def call
12
- str = @str
13
- scripts = str.gsub(%r{<script>(.+)</script>}m).with_object([]) do |_, list|
14
- list << Regexp.last_match(1)
15
- end
16
-
17
- scripts.uniq.sort.join("\n")
18
- end
19
-
20
- # @param [String] str
21
- # @return [String]
22
- def self.call(str)
23
- new(str).call
24
- end
25
- end
26
- end