js_dependency 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 68994e61c025f75290a28d6d14e1004c54c09ad32d54f3c93fc5626d9621fcd2
4
+ data.tar.gz: f8dcb17dc254e873d217ecce20ec2e739d1c53c9935cbc6e163941d53a033f2a
5
+ SHA512:
6
+ metadata.gz: 46891feebfed8de0c5fe551b3e176e48d38f17359c6322fd68893c6a0b101394981b5e97385079069c20b93df198c5e8abbb211a3d482609101ad5db06a81197
7
+ data.tar.gz: ab70ef7054409bc108c68ed0b376feaa110c59543769c91f232940b75cfe81917ca2ca4a7c2c3881b89dd3247fa49d8f03c27bf4b70f7bc14b14e14a8f31b7d1
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,21 @@
1
+ inherit_from: .rubocop_todo.yml
2
+
3
+ AllCops:
4
+ TargetRubyVersion: 2.7
5
+ NewCops: enable
6
+
7
+ Style/Documentation:
8
+ Enabled: false
9
+
10
+ Style/StringLiterals:
11
+ Enabled: true
12
+ EnforcedStyle: double_quotes
13
+
14
+ Style/StringLiteralsInInterpolation:
15
+ Enabled: true
16
+ EnforcedStyle: double_quotes
17
+
18
+ require:
19
+ - rubocop-performance
20
+ - rubocop-rspec
21
+ - rubocop-rake
data/.rubocop_todo.yml ADDED
@@ -0,0 +1,35 @@
1
+ # This configuration was generated by
2
+ # `rubocop --auto-gen-config`
3
+ # on 2022-07-18 12:42:10 UTC using RuboCop version 1.31.2.
4
+ # The point is for the user to remove these configuration records
5
+ # one by one as the offenses are removed from the code base.
6
+ # Note that changes in the inspected code, or installation of new
7
+ # versions of RuboCop, may require this file to be generated again.
8
+
9
+ # Offense count: 3
10
+ # Configuration parameters: IgnoredMethods, CountRepeatedAttributes.
11
+ Metrics/AbcSize:
12
+ Max: 20
13
+
14
+ # Offense count: 3
15
+ # Configuration parameters: CountComments, CountAsOne, ExcludedMethods, IgnoredMethods.
16
+ Metrics/MethodLength:
17
+ Max: 17
18
+
19
+ # Offense count: 1
20
+ # Configuration parameters: CountKeywordArgs, MaxOptionalParameters.
21
+ Metrics/ParameterLists:
22
+ Max: 8
23
+
24
+ # Offense count: 1
25
+ # This cop supports safe autocorrection (--autocorrect).
26
+ Style/IfUnlessModifier:
27
+ Exclude:
28
+ - 'lib/js_dependency/index_creator.rb'
29
+
30
+ # Offense count: 1
31
+ # This cop supports safe autocorrection (--autocorrect).
32
+ # Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, AllowedPatterns, IgnoredPatterns.
33
+ # URISchemes: http, https
34
+ Layout/LineLength:
35
+ Max: 167
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2022-07-16
4
+
5
+ - Initial release
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at jun5araki@gmail.com. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/Gemfile ADDED
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in js_dependency.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "rspec", "~> 3.0"
11
+
12
+ gem "codecov", require: false, group: "test"
13
+ gem "rubocop", "~> 1.21"
14
+ gem "simplecov", require: false, group: :test
15
+ gem "yarn"
data/Gemfile.lock ADDED
@@ -0,0 +1,105 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ js_dependency (0.1.0)
5
+ pathname
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ ast (2.4.2)
11
+ binding_ninja (0.2.3)
12
+ codecov (0.6.0)
13
+ simplecov (>= 0.15, < 0.22)
14
+ coderay (1.1.3)
15
+ diff-lcs (1.5.0)
16
+ docile (1.4.0)
17
+ json (2.6.2)
18
+ parallel (1.22.1)
19
+ parser (3.1.2.0)
20
+ ast (~> 2.4.1)
21
+ parslet (2.0.0)
22
+ pathname (0.2.0)
23
+ proc_to_ast (0.1.0)
24
+ coderay
25
+ parser
26
+ unparser
27
+ rack (2.2.4)
28
+ rainbow (3.1.1)
29
+ rake (13.0.6)
30
+ regexp_parser (2.5.0)
31
+ rexml (3.2.5)
32
+ rspec (3.11.0)
33
+ rspec-core (~> 3.11.0)
34
+ rspec-expectations (~> 3.11.0)
35
+ rspec-mocks (~> 3.11.0)
36
+ rspec-core (3.11.0)
37
+ rspec-support (~> 3.11.0)
38
+ rspec-expectations (3.11.0)
39
+ diff-lcs (>= 1.2.0, < 2.0)
40
+ rspec-support (~> 3.11.0)
41
+ rspec-mocks (3.11.1)
42
+ diff-lcs (>= 1.2.0, < 2.0)
43
+ rspec-support (~> 3.11.0)
44
+ rspec-parameterized (0.5.2)
45
+ binding_ninja (>= 0.2.3)
46
+ parser
47
+ proc_to_ast
48
+ rspec (>= 2.13, < 4)
49
+ unparser
50
+ rspec-support (3.11.0)
51
+ rubocop (1.31.2)
52
+ json (~> 2.3)
53
+ parallel (~> 1.10)
54
+ parser (>= 3.1.0.0)
55
+ rainbow (>= 2.2.2, < 4.0)
56
+ regexp_parser (>= 1.8, < 3.0)
57
+ rexml (>= 3.2.5, < 4.0)
58
+ rubocop-ast (>= 1.18.0, < 2.0)
59
+ ruby-progressbar (~> 1.7)
60
+ unicode-display_width (>= 1.4.0, < 3.0)
61
+ rubocop-ast (1.19.1)
62
+ parser (>= 3.1.1.0)
63
+ rubocop-performance (1.14.3)
64
+ rubocop (>= 1.7.0, < 2.0)
65
+ rubocop-ast (>= 0.4.0)
66
+ rubocop-rake (0.6.0)
67
+ rubocop (~> 1.0)
68
+ rubocop-rspec (2.12.1)
69
+ rubocop (~> 1.31)
70
+ ruby-progressbar (1.11.0)
71
+ simplecov (0.21.2)
72
+ docile (~> 1.1)
73
+ simplecov-html (~> 0.11)
74
+ simplecov_json_formatter (~> 0.1)
75
+ simplecov-html (0.12.3)
76
+ simplecov_json_formatter (0.1.4)
77
+ trollop (2.9.10)
78
+ unicode-display_width (2.2.0)
79
+ unparser (0.6.5)
80
+ diff-lcs (~> 1.3)
81
+ parser (>= 3.1.0)
82
+ yarn (0.1.1)
83
+ parslet (>= 1.2)
84
+ rack (>= 1.3)
85
+ trollop (>= 1.16)
86
+
87
+ PLATFORMS
88
+ arm64-darwin-21
89
+ x86_64-linux
90
+
91
+ DEPENDENCIES
92
+ codecov
93
+ js_dependency!
94
+ rake (~> 13.0)
95
+ rspec (~> 3.0)
96
+ rspec-parameterized
97
+ rubocop (~> 1.21)
98
+ rubocop-performance
99
+ rubocop-rake
100
+ rubocop-rspec
101
+ simplecov
102
+ yarn
103
+
104
+ BUNDLED WITH
105
+ 2.3.16
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022 junara
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,37 @@
1
+ # JsDependency
2
+
3
+ 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.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Install the gem and add to the application's Gemfile by executing:
10
+
11
+ $ bundle add js_dependency
12
+
13
+ If bundler is not being used to manage dependencies, install the gem by executing:
14
+
15
+ $ gem install js_dependency
16
+
17
+ ## Usage
18
+
19
+ TODO: Write usage instructions here
20
+
21
+ ## Development
22
+
23
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
24
+
25
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
26
+
27
+ ## Contributing
28
+
29
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/js_dependency. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/js_dependency/blob/main/CODE_OF_CONDUCT.md).
30
+
31
+ ## License
32
+
33
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
34
+
35
+ ## Code of Conduct
36
+
37
+ Everyone interacting in the JsDependency project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/js_dependency/blob/main/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
@@ -0,0 +1,45 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/js_dependency/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "js_dependency"
7
+ spec.version = JsDependency::VERSION
8
+ spec.authors = ["junara"]
9
+ spec.email = ["jun5araki@gmail.com"]
10
+
11
+ spec.summary = "Analyze dependency using import for JavaScript."
12
+ spec.description = spec.summary.to_s
13
+ spec.homepage = "https://github.com/junara/#{spec.name}"
14
+ spec.license = "MIT"
15
+ spec.required_ruby_version = ">= 2.7.0"
16
+
17
+ spec.metadata["homepage_uri"] = spec.homepage
18
+ spec.metadata["source_code_uri"] = spec.homepage
19
+ spec.metadata["changelog_uri"] = "#{spec.homepage}/blob/main/CHANGELOG.md"
20
+ spec.metadata["documentation_uri"] = "https://rubydoc.info/gems/#{spec.name}"
21
+
22
+ # Specify which files should be added to the gem when it is released.
23
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
24
+ spec.files = Dir.chdir(__dir__) do
25
+ `git ls-files -z`.split("\x0").reject do |f|
26
+ (f == __FILE__) || f.match(%r{\A(?:(?:bin|test|spec|features)/|\.(?:git|travis|circleci)|appveyor)})
27
+ end
28
+ end
29
+ spec.bindir = "exe"
30
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
31
+ spec.require_paths = ["lib"]
32
+
33
+ spec.add_dependency "pathname"
34
+
35
+ spec.add_development_dependency "rspec"
36
+ spec.add_development_dependency "rspec-parameterized"
37
+ spec.add_development_dependency "rubocop"
38
+ spec.add_development_dependency "rubocop-performance"
39
+ spec.add_development_dependency "rubocop-rake"
40
+ spec.add_development_dependency "rubocop-rspec"
41
+
42
+ # For more information and examples about making a new gem, check out our
43
+ # guide at: https://bundler.io/guides/creating_gem.html
44
+ spec.metadata["rubygems_mfa_required"] = "true"
45
+ end
@@ -0,0 +1,77 @@
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, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
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, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
72
+
73
+ def self.call(str)
74
+ new(str).call
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,24 @@
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
+ def self.call(str)
21
+ new(str).call
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,79 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "extract_script_tag"
4
+ require_relative "extract_import_path"
5
+ require_relative "replace_path_alias"
6
+ require_relative "pathname_utility"
7
+
8
+ module JsDependency
9
+ class IndexCreator
10
+ def initialize(src, alias_paths: nil)
11
+ @src = src
12
+ @alias_paths = alias_paths
13
+ end
14
+
15
+ def self.call(src, alias_paths: nil)
16
+ new(src, alias_paths: alias_paths).call
17
+ end
18
+
19
+ def call
20
+ src_pathname = Pathname.new(@src).relative? ? Pathname.new(@src).realpath : Pathname.new(@src)
21
+ raise Error, "#{@src} is not directory." unless src_pathname.directory?
22
+
23
+ alias_paths = @alias_paths&.transform_values do |path|
24
+ Pathname.new(path).relative? ? src_pathname + Pathname.new(path) : Pathname.new(path)
25
+ end
26
+ index_from(src_pathname, alias_paths)
27
+ end
28
+
29
+ private
30
+
31
+ def index_from(src_pathname, alias_paths)
32
+ pattern = %w[**/*.vue **/*.js **/*.jsx]
33
+
34
+ src_pathname.glob(pattern).each_with_object({}) do |component_pathname, obj|
35
+ import_pathnames = import_pathnames_from(component_pathname, alias_paths)
36
+
37
+ obj[component_pathname.to_s] = import_pathnames.map(&:to_s)
38
+ if component_pathname.basename.to_s == "index.js"
39
+ obj[component_pathname.dirname.to_s] = import_pathnames.map(&:to_s)
40
+ end
41
+ end
42
+ end
43
+
44
+ def import_pathnames_from(component_pathname, alias_paths)
45
+ component_dirname = component_pathname.dirname
46
+ script_str = extract_script_string(component_pathname)
47
+ JsDependency::ExtractImportPath.call(script_str).map do |import_path|
48
+ standardize_path(import_path, alias_paths, component_dirname)
49
+ end
50
+ end
51
+
52
+ def standardize_path(import_path, alias_paths, component_dirname)
53
+ import_pathname = Pathname.new(replace_path_alias(import_path, alias_paths))
54
+
55
+ # Convert relative path to absolute path
56
+ if import_pathname.relative? &&
57
+ JsDependency::PathnameUtility.complement_extname((component_dirname + Pathname.new(import_path))).exist?
58
+ import_pathname = (component_dirname + Pathname.new(import_path))
59
+ end
60
+
61
+ # Complement extname (vue, js, jsx) if it is not exist
62
+ import_pathname = JsDependency::PathnameUtility.complement_extname(import_pathname)
63
+
64
+ import_pathname.cleanpath
65
+ end
66
+
67
+ def extract_script_string(pathname)
68
+ str = pathname.read
69
+ extname = pathname.extname
70
+ return str unless extname == ".vue"
71
+
72
+ JsDependency::ExtractScriptTag.call(str)
73
+ end
74
+
75
+ def replace_path_alias(path, alias_paths)
76
+ JsDependency::ReplacePathAlias.call(path, alias_paths)
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ module JsDependency
4
+ module Mermaid
5
+ class NodesLink
6
+ attr_reader :parent, :child
7
+
8
+ def initialize(parent, child)
9
+ @parent = parent
10
+ @child = child
11
+ end
12
+
13
+ def parent_module_name(level = 0)
14
+ parse(Pathname.new(@parent), level).join("/")
15
+ end
16
+
17
+ def child_module_name(level = 0)
18
+ parse(Pathname.new(@child), level).join("/")
19
+ end
20
+
21
+ private
22
+
23
+ def parse(pathname, level)
24
+ return [pathname.to_s] unless pathname.exist?
25
+
26
+ array = [pathname.basename]
27
+ level.times do
28
+ array.unshift(pathname.parent.basename.to_s)
29
+ pathname = pathname.parent
30
+ end
31
+ array
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "nodes_link"
4
+ module JsDependency
5
+ module Mermaid
6
+ class Root
7
+ attr_accessor :orientation
8
+
9
+ def initialize(orientation = "LR")
10
+ @orientation = orientation
11
+ @list = []
12
+ end
13
+
14
+ def add(parent, child)
15
+ @list << NodesLink.new(parent, child)
16
+ end
17
+
18
+ def export(name_level: 1)
19
+ str = "flowchart #{orientation}\n"
20
+ str + @list.sort_by(&:parent).uniq { |link| "#{link.parent}__#{link.child}" }.map do |link|
21
+ "#{link.parent_module_name(name_level)} --> #{link.child_module_name(name_level)}"
22
+ end.join("\n")
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module JsDependency
4
+ module PathnameUtility
5
+ def self.complement_extname(pathname)
6
+ return pathname if pathname.exist? || pathname.extname != ""
7
+
8
+ %w[.js .jsx .vue].each do |ext|
9
+ next unless pathname.sub_ext(ext).file?
10
+
11
+ return pathname.sub_ext(ext)
12
+ end
13
+
14
+ pathname
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ module JsDependency
4
+ class ReplacePathAlias
5
+ # @param [String] str
6
+ # @param [Hash, nil] alias_paths
7
+ def initialize(str, alias_paths = nil)
8
+ @str = str
9
+ @alias_paths = alias_paths
10
+ end
11
+
12
+ # @param [String] str
13
+ # @param [Hash, nil] alias_paths
14
+ # @return [String]
15
+ def self.call(str, alias_paths = nil)
16
+ return str if alias_paths.nil? || alias_paths.empty?
17
+
18
+ new(str, alias_paths).call
19
+ end
20
+
21
+ # @return [String]
22
+ def call
23
+ return @str if safe_list.include?(@str)
24
+
25
+ replace_alias_paths(@str, @alias_paths)
26
+ end
27
+
28
+ private
29
+
30
+ # @param [String] str
31
+ # @param [nil, Hash] alias_paths
32
+ def replace_alias_paths(str, alias_paths)
33
+ alias_paths.each do |(key, value)|
34
+ str = str.gsub(/^#{key}/, value.to_s)
35
+ end
36
+ str
37
+ end
38
+
39
+ def safe_list
40
+ ["@rails/ujs"]
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module JsDependency
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,119 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "js_dependency/version"
4
+ require_relative "js_dependency/index_creator"
5
+ require_relative "js_dependency/mermaid/root"
6
+ require "pathname"
7
+
8
+ module JsDependency
9
+ class Error < StandardError; end
10
+
11
+ # @param [String] src_path
12
+ # @param [String] target_path
13
+ # @param [String] orientation
14
+ # @param [Hash, nil] alias_paths
15
+ # @param [Integer] child_analyze_level
16
+ # @param [Integer] parent_analyze_level
17
+ # @param [Integer] name_level
18
+ # @param [String, nil] output_path
19
+ # @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)
21
+ output_pathname = Pathname.new(output_path) if output_path
22
+ index = JsDependency::IndexCreator.call(src_path, alias_paths: alias_paths)
23
+
24
+ target_pathname = if Pathname.new(target_path).relative? && Pathname.new(target_path).exist?
25
+ Pathname.new(target_path).realpath
26
+ else
27
+ Pathname.new(target_path)
28
+ end
29
+
30
+ root = JsDependency::Mermaid::Root.new(orientation)
31
+
32
+ parents_paths(target_pathname, parent_analyze_level, index) do |parent_path, child_path|
33
+ root.add(parent_path, child_path)
34
+ end
35
+
36
+ children_paths(target_pathname, child_analyze_level, index) do |parent_path, child_path|
37
+ root.add(parent_path, child_path)
38
+ end
39
+
40
+ output = root.export(name_level: name_level)
41
+ output_pathname&.write(output)
42
+ output
43
+ end
44
+
45
+ # @param [String] target_path
46
+ # @param [Hash] index
47
+ # @return [Array]
48
+ def self.extract_parent_paths(target_path, index)
49
+ target_pathname = if Pathname.new(target_path).exist?
50
+ Pathname.new(target_path).realpath
51
+ else
52
+ Pathname.new(target_path)
53
+ end
54
+ index.each_with_object([]) do |(parent, children), list|
55
+ list << parent if children.any?(target_pathname.to_s)
56
+ end
57
+ end
58
+ private_class_method :extract_parent_paths
59
+
60
+ # @param [String] target_path
61
+ # @param [Hash] index
62
+ # @return [Array]
63
+ def self.extract_children_paths(target_path, index)
64
+ target_pathname = if Pathname.new(target_path).exist?
65
+ Pathname.new(target_path).realpath
66
+ else
67
+ Pathname.new(target_path)
68
+ end
69
+ index[target_pathname.to_s] || []
70
+ end
71
+ private_class_method :extract_children_paths
72
+
73
+ # @param [String] src
74
+ # @param [nil, Hash] alias_paths
75
+ def self.create_index(src_path, alias_paths: nil)
76
+ JsDependency::IndexCreator.call(src_path, alias_paths: alias_paths)
77
+ end
78
+ private_class_method :create_index
79
+
80
+ def self.parents_paths(target_pathname, analyze_level, index)
81
+ temp_paths = [target_pathname.to_s]
82
+ analyze_level.times do
83
+ list = []
84
+ temp_paths.each do |temp_path|
85
+ temp_pathname = if Pathname.new(temp_path).relative? && Pathname.new(temp_path).exist?
86
+ Pathname.new(temp_path).realpath
87
+ else
88
+ Pathname.new(temp_path)
89
+ end
90
+
91
+ list += extract_parent_paths(temp_pathname.to_s, index).each do |parent_path|
92
+ yield parent_path, temp_pathname.to_s
93
+ end
94
+ end
95
+ temp_paths = list
96
+ end
97
+ end
98
+ private_class_method :parents_paths
99
+
100
+ def self.children_paths(target_pathname, analyze_level, index)
101
+ temp_paths = [target_pathname.to_s]
102
+ analyze_level.times do
103
+ list = []
104
+ temp_paths.each do |temp_path|
105
+ temp_pathname = if Pathname.new(temp_path).relative? && Pathname.new(temp_path).exist?
106
+ Pathname.new(temp_path).realpath
107
+ else
108
+ Pathname.new(temp_path)
109
+ end
110
+
111
+ list += extract_children_paths(temp_pathname.to_s, index).each do |child_path|
112
+ yield temp_pathname.to_s, child_path
113
+ end
114
+ end
115
+ temp_paths = list
116
+ end
117
+ end
118
+ private_class_method :children_paths
119
+ end
@@ -0,0 +1,4 @@
1
+ module JsDependency
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,167 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: js_dependency
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - junara
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2022-07-18 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: pathname
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
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: rspec-parameterized
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rubocop
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rubocop-performance
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rubocop-rake
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rubocop-rspec
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ description: Analyze dependency using import for JavaScript.
112
+ email:
113
+ - jun5araki@gmail.com
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - ".rspec"
119
+ - ".rubocop.yml"
120
+ - ".rubocop_todo.yml"
121
+ - CHANGELOG.md
122
+ - CODE_OF_CONDUCT.md
123
+ - Gemfile
124
+ - Gemfile.lock
125
+ - LICENSE.txt
126
+ - README.md
127
+ - Rakefile
128
+ - js_dependency.gemspec
129
+ - lib/js_dependency.rb
130
+ - lib/js_dependency/extract_import_path.rb
131
+ - lib/js_dependency/extract_script_tag.rb
132
+ - lib/js_dependency/index_creator.rb
133
+ - lib/js_dependency/mermaid/nodes_link.rb
134
+ - lib/js_dependency/mermaid/root.rb
135
+ - lib/js_dependency/pathname_utility.rb
136
+ - lib/js_dependency/replace_path_alias.rb
137
+ - lib/js_dependency/version.rb
138
+ - sig/js_dependency.rbs
139
+ homepage: https://github.com/junara/js_dependency
140
+ licenses:
141
+ - MIT
142
+ metadata:
143
+ homepage_uri: https://github.com/junara/js_dependency
144
+ source_code_uri: https://github.com/junara/js_dependency
145
+ changelog_uri: https://github.com/junara/js_dependency/blob/main/CHANGELOG.md
146
+ documentation_uri: https://rubydoc.info/gems/js_dependency
147
+ rubygems_mfa_required: 'true'
148
+ post_install_message:
149
+ rdoc_options: []
150
+ require_paths:
151
+ - lib
152
+ required_ruby_version: !ruby/object:Gem::Requirement
153
+ requirements:
154
+ - - ">="
155
+ - !ruby/object:Gem::Version
156
+ version: 2.7.0
157
+ required_rubygems_version: !ruby/object:Gem::Requirement
158
+ requirements:
159
+ - - ">="
160
+ - !ruby/object:Gem::Version
161
+ version: '0'
162
+ requirements: []
163
+ rubygems_version: 3.3.7
164
+ signing_key:
165
+ specification_version: 4
166
+ summary: Analyze dependency using import for JavaScript.
167
+ test_files: []