vuesfc2js 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: b257873bfd219b9f17aef64fb0f135c0293ac28ac89f2b6c6c352094b88c026a
4
+ data.tar.gz: 7ef5d51b1cb407ad7c106c599320e2110f2ba968f3fb9cff8d63ea570abfe5c8
5
+ SHA512:
6
+ metadata.gz: dd57b8f0dabb66666b0c81fe21915164dc9db428afb2a9a3de53e04e5fe71769385c3c5b4f4da4d88d837b86533f177ed2df68c390c42116fe1461a811519dc6
7
+ data.tar.gz: e40d633a64eda8c1403274f99735ef01799a1960132701628846cd39298f84423ebaa8c7661fba10e2f3f8b776f606ad2ad11a823de3dcff96c232083f08a9e2
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,22 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.5
3
+
4
+ Style/Documentation:
5
+ Enabled: false
6
+
7
+ Style/StringLiterals:
8
+ Enabled: true
9
+ EnforcedStyle: double_quotes
10
+
11
+ Style/StringLiteralsInInterpolation:
12
+ Enabled: true
13
+ EnforcedStyle: double_quotes
14
+
15
+ Layout/LineLength:
16
+ Max: 130
17
+
18
+ require:
19
+ - rubocop-performance
20
+ - rubocop-rspec
21
+ - rubocop-rake
22
+
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2022-07-10
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,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in vuesfc2js.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
data/Gemfile.lock ADDED
@@ -0,0 +1,109 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ vuesfc2js (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ activesupport (7.0.3)
10
+ concurrent-ruby (~> 1.0, >= 1.0.2)
11
+ i18n (>= 1.6, < 2)
12
+ minitest (>= 5.1)
13
+ tzinfo (~> 2.0)
14
+ ast (2.4.2)
15
+ binding_ninja (0.2.3)
16
+ codecov (0.6.0)
17
+ simplecov (>= 0.15, < 0.22)
18
+ coderay (1.1.3)
19
+ concurrent-ruby (1.1.10)
20
+ diff-lcs (1.5.0)
21
+ docile (1.4.0)
22
+ factory_bot (6.2.1)
23
+ activesupport (>= 5.0.0)
24
+ i18n (1.10.0)
25
+ concurrent-ruby (~> 1.0)
26
+ json (2.6.2)
27
+ minitest (5.16.2)
28
+ parallel (1.22.1)
29
+ parser (3.1.2.0)
30
+ ast (~> 2.4.1)
31
+ proc_to_ast (0.1.0)
32
+ coderay
33
+ parser
34
+ unparser
35
+ rainbow (3.1.1)
36
+ rake (13.0.6)
37
+ regexp_parser (2.5.0)
38
+ rexml (3.2.5)
39
+ rspec (3.11.0)
40
+ rspec-core (~> 3.11.0)
41
+ rspec-expectations (~> 3.11.0)
42
+ rspec-mocks (~> 3.11.0)
43
+ rspec-core (3.11.0)
44
+ rspec-support (~> 3.11.0)
45
+ rspec-expectations (3.11.0)
46
+ diff-lcs (>= 1.2.0, < 2.0)
47
+ rspec-support (~> 3.11.0)
48
+ rspec-mocks (3.11.1)
49
+ diff-lcs (>= 1.2.0, < 2.0)
50
+ rspec-support (~> 3.11.0)
51
+ rspec-parameterized (0.5.2)
52
+ binding_ninja (>= 0.2.3)
53
+ parser
54
+ proc_to_ast
55
+ rspec (>= 2.13, < 4)
56
+ unparser
57
+ rspec-support (3.11.0)
58
+ rubocop (1.31.2)
59
+ json (~> 2.3)
60
+ parallel (~> 1.10)
61
+ parser (>= 3.1.0.0)
62
+ rainbow (>= 2.2.2, < 4.0)
63
+ regexp_parser (>= 1.8, < 3.0)
64
+ rexml (>= 3.2.5, < 4.0)
65
+ rubocop-ast (>= 1.18.0, < 2.0)
66
+ ruby-progressbar (~> 1.7)
67
+ unicode-display_width (>= 1.4.0, < 3.0)
68
+ rubocop-ast (1.19.1)
69
+ parser (>= 3.1.1.0)
70
+ rubocop-performance (1.14.2)
71
+ rubocop (>= 1.7.0, < 2.0)
72
+ rubocop-ast (>= 0.4.0)
73
+ rubocop-rake (0.6.0)
74
+ rubocop (~> 1.0)
75
+ rubocop-rspec (2.12.1)
76
+ rubocop (~> 1.31)
77
+ ruby-progressbar (1.11.0)
78
+ simplecov (0.21.2)
79
+ docile (~> 1.1)
80
+ simplecov-html (~> 0.11)
81
+ simplecov_json_formatter (~> 0.1)
82
+ simplecov-html (0.12.3)
83
+ simplecov_json_formatter (0.1.4)
84
+ tzinfo (2.0.4)
85
+ concurrent-ruby (~> 1.0)
86
+ unicode-display_width (2.2.0)
87
+ unparser (0.6.5)
88
+ diff-lcs (~> 1.3)
89
+ parser (>= 3.1.0)
90
+
91
+ PLATFORMS
92
+ arm64-darwin-21
93
+ x86_64-linux
94
+
95
+ DEPENDENCIES
96
+ codecov
97
+ factory_bot
98
+ rake (~> 13.0)
99
+ rspec (~> 3.0)
100
+ rspec-parameterized
101
+ rubocop (~> 1.21)
102
+ rubocop-performance
103
+ rubocop-rake
104
+ rubocop-rspec
105
+ simplecov
106
+ vuesfc2js!
107
+
108
+ BUNDLED WITH
109
+ 2.3.12
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,151 @@
1
+
2
+ # Vuesfc2js
3
+
4
+ Gem for converting `.vue` file into `.js` file for analyze dependency analysis.
5
+
6
+ I tried following dependency analysis tools thant is converted by `vuesfc2js`:
7
+
8
+ * [pahen/madge: Create graphs from your CommonJS, AMD or ES6 module dependencies](https://github.com/pahen/madge)
9
+ * [Himenon/code-dependency: A work efficiency tool for visualizing code dependencies on a browser.](https://github.com/Himenon/code-dependency)
10
+
11
+
12
+
13
+ ## Installation
14
+
15
+ Install the gem and add to the application's Gemfile by executing:
16
+
17
+ $ bundle add vuesfc2js
18
+
19
+ If bundler is not being used to manage dependencies, install the gem by executing:
20
+
21
+ $ gem install vuesfc2js
22
+
23
+ ## Usage
24
+
25
+ Many dependency analysis application assume import and export statement in JavaScript file.
26
+
27
+ In `.vue` file JavaScript is included by `<script>` tag and these applications are not aware of this.
28
+
29
+ Idea is very simple. Just extract script container and replace `.vue` file into `.js` file.
30
+
31
+ See following example:
32
+
33
+ Before conversion.
34
+
35
+ `TestComponent.vue`
36
+
37
+ ```vue
38
+ <template>
39
+
40
+ </template>
41
+ <script>
42
+ import Vue from 'vue';
43
+ import HogeComponent from './HogeComponent.vue';
44
+ </script>
45
+ <style>
46
+ </style>
47
+ ```
48
+
49
+ After conversion.
50
+
51
+ `TestComponent.js`
52
+
53
+ ```javascript
54
+ import Vue from 'vue';
55
+ import HogeComponent from './HogeComponent.js';
56
+ ```
57
+
58
+ And using alias case
59
+
60
+ Before conversion
61
+
62
+ ```javascript
63
+ {
64
+ @: "/path/to/vue/project/packs"
65
+ }
66
+ ```
67
+
68
+ ```vue
69
+ <template>
70
+
71
+ </template>
72
+ <script>
73
+ import FugaComponent from '@/../component/FugaComponent.vue';
74
+ </script>
75
+ <style>
76
+ </style>
77
+ ```
78
+
79
+
80
+ After conversion
81
+
82
+ ```javascript
83
+ import FugaComponent from '/path/to/vue/project/packs/../component/FugaComponent.js';
84
+ ```
85
+
86
+ And `.vue` and alias (like '@') in .js is also converted.
87
+
88
+ ### Convert Vue project directory
89
+
90
+ Your project directory is absolute path `'/path/to/vue/project'`,
91
+
92
+ Prepare the destination directory absolute path `'/path/to'` to save converted files.
93
+
94
+ Optionally, you can specify alias absolute path by like `{ "@" => '/path/to/vue/project/packs' }`.
95
+
96
+ First: install this gem.
97
+
98
+ ```shell
99
+ gem install vuesfc2js
100
+ ```
101
+
102
+ Second: start irb to execute ruby script interactively.
103
+
104
+ ```shell
105
+ irb
106
+ ```
107
+
108
+ Finally: just type two line of ruby script.
109
+
110
+ ```ruby
111
+ require 'vuesfc2js'
112
+ Vuesfc2js.convert_vue_project('/path/to/vue/project', '/path/to/dst', src_path_alias: { "@" => '/path/to/vue/project/packs' })
113
+ ```
114
+
115
+ You got `.js` files in '/path/to/dst/project'.
116
+
117
+ ## Usage example
118
+
119
+ Assuming your project is converted into `/path/to/dst/project`, like following:
120
+
121
+ * [pahen/madge: Create graphs from your CommonJS, AMD or ES6 module dependencies](https://github.com/pahen/madge)
122
+
123
+ ```shell
124
+ madge /path/to/dst/project
125
+ ```
126
+
127
+ * [Himenon/code-dependency: A work efficiency tool for visualizing code dependencies on a browser.](https://github.com/Himenon/code-dependency)
128
+
129
+ ```shell
130
+ code-dependency --source /path/to/dst/project
131
+ ```
132
+
133
+
134
+
135
+ ## Development
136
+
137
+ 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.
138
+
139
+ 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).
140
+
141
+ ## Contributing
142
+
143
+ Bug reports and pull requests are welcome on GitHub at https://github.com/junara/vuesfc2js. 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]/vuesfc2js/blob/main/CODE_OF_CONDUCT.md).
144
+
145
+ ## License
146
+
147
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
148
+
149
+ ## Code of Conduct
150
+
151
+ Everyone interacting in the Vuesfc2js project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/vuesfc2js/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,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Vuesfc2js
4
+ module Conversion
5
+ module Base
6
+ def self.included(base)
7
+ base.extend(ClassMethods)
8
+ end
9
+
10
+ module ClassMethods
11
+ # @param [String] str
12
+ def call(str)
13
+ new(str).call
14
+ end
15
+ end
16
+
17
+ # @param [String] str
18
+ def initialize(str)
19
+ @str = str
20
+ end
21
+
22
+ # @return [String]
23
+ def call
24
+ raise NotImplementedError, "call is not implemented"
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "./base"
4
+
5
+ module Vuesfc2js
6
+ module Conversion
7
+ class ReplaceDotVue2Js
8
+ include Vuesfc2js::Conversion::Base
9
+ # @return [String]
10
+ def call
11
+ replace_dot_vue2js(@str)
12
+ end
13
+
14
+ private
15
+
16
+ def replace_dot_vue2js(str)
17
+ str.gsub(/\.vue"/, '.js"').gsub(/\.vue'/, ".js'")
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "./base"
4
+
5
+ module Vuesfc2js
6
+ module Conversion
7
+ class ReplaceFilenameDotVue2Js
8
+ include Vuesfc2js::Conversion::Base
9
+ # @return [String]
10
+ def call
11
+ replace_filename_dot_vue2js(@str)
12
+ end
13
+
14
+ private
15
+
16
+ # @param [String] filename
17
+ # @return [String]
18
+ def replace_filename_dot_vue2js(filename)
19
+ if File.extname(filename) == ".vue"
20
+ [File.dirname(filename), "#{File.basename(filename, ".vue")}.js"].join("/")
21
+ else
22
+ [File.dirname(filename), File.basename(filename)].join("/")
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Vuesfc2js
4
+ module Conversion
5
+ class ReplacePathAlias
6
+ # @param [String] str
7
+ # @param [Hash, nil] path_alias
8
+ def initialize(str, path_alias = nil)
9
+ @str = str
10
+ @path_alias = path_alias
11
+ end
12
+
13
+ # @param [String] str
14
+ # @param [Hash, nil] path_alias
15
+ # @return [String]
16
+ def self.call(str, path_alias = nil)
17
+ return str if path_alias.nil? || path_alias.empty?
18
+
19
+ new(str, path_alias).call
20
+ end
21
+
22
+ # @return [String]
23
+ def call
24
+ replace_path_alias(@str, @path_alias)
25
+ end
26
+
27
+ private
28
+
29
+ # @param [String] str
30
+ # @param [nil, Hash] path_alias
31
+ def replace_path_alias(str, path_alias)
32
+ path_alias.each do |(key, value)|
33
+ str = str.gsub(/"#{key}/, "\"#{value}")
34
+ str = str.gsub(/'#{key}/, "'#{value}")
35
+ end
36
+ str
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "./base"
4
+
5
+ module Vuesfc2js
6
+ module Conversion
7
+ class ScriptExtraction
8
+ include Vuesfc2js::Conversion::Base
9
+
10
+ # @return [String]
11
+ def call
12
+ extract_script(@str)
13
+ end
14
+
15
+ private
16
+
17
+ def extract_script(str)
18
+ str.scan(%r{<script.*>\n(.*)</script>}m).join
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Vuesfc2js
4
+ module Error
5
+ class NotExistError < StandardError; end
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Vuesfc2js
4
+ module Error
5
+ class NotImplementedError < StandardError; end
6
+ end
7
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Vuesfc2js
4
+ VERSION = "0.1.0"
5
+ end
data/lib/vuesfc2js.rb ADDED
@@ -0,0 +1,126 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "fileutils"
4
+ require_relative "vuesfc2js/version"
5
+ require_relative "vuesfc2js/error/not_exist_error"
6
+ require_relative "vuesfc2js/error/not_implemented_error"
7
+ require_relative "vuesfc2js/conversion/script_extraction"
8
+ require_relative "vuesfc2js/conversion/replace_dot_vue2js"
9
+ require_relative "vuesfc2js/conversion/replace_filename_dot_vue2js"
10
+ require_relative "vuesfc2js/conversion/replace_path_alias"
11
+
12
+ module Vuesfc2js
13
+ module_function
14
+
15
+ # Convert code like SFC to js.
16
+ # @param [String] str
17
+ # @param [nil, Hash] path_alias
18
+ # @param [String] extname
19
+ # @return [String]
20
+ def convert_str(str, path_alias: nil, extname: ".vue")
21
+ str = extname == ".vue" ? Conversion::ScriptExtraction.call(str) : str
22
+ str = Conversion::ReplaceDotVue2Js.call(str)
23
+ path_alias.nil? ? str : Conversion::ReplacePathAlias.call(str, path_alias)
24
+ end
25
+
26
+ # Get filename having js extname from vue extname file.
27
+ # @param [String] filename
28
+ # @return [String]
29
+ def filename(filename)
30
+ Conversion::ReplaceFilenameDotVue2Js.call(filename)
31
+ end
32
+
33
+ # Convert a file.
34
+ # @param [String] filename
35
+ # @param [nil, Hash] path_alias
36
+ # @param [Array<String>] extnames
37
+ # @param [FalseClass, TrueClass] replace if replace .vue file to .js file, set true.
38
+ def convert_file(filename, path_alias: nil, extnames: %w[.vue .js], replace: false)
39
+ raise "Only .vue or .js is permitted" if (extnames - %w[.vue .js]).size >= 1
40
+
41
+ extname = File.extname(filename)
42
+ return unless extnames.include?(extname)
43
+
44
+ raise Error::NotExistError, "#{filename} is not existed." unless File.exist?(filename)
45
+
46
+ str = File.read(filename)
47
+ str = convert_str(str, path_alias: path_alias, extname: extname)
48
+
49
+ File.write(filename(filename), str)
50
+
51
+ File.delete(filename) if replace && File.extname(filename) == ".vue"
52
+ filename
53
+ end
54
+
55
+ # Convert a directory.
56
+ # @param [String] dir
57
+ # @param [nil, Hash] path_alias
58
+ # @param [Array<String>] extnames
59
+ # @param [FalseClass, TrueClass] replace if replace .vue file to .js file, set true.
60
+ def convert_directory(dir, path_alias: nil, extnames: %w[.vue .js], replace: false)
61
+ raise Error::NotExistError, "#{dir} is not existed." unless Dir.exist?(dir)
62
+
63
+ Dir.glob((Pathname.new(dir) + Pathname.new("**/*")).to_s).each do |filename|
64
+ convert_file(filename, path_alias: path_alias, extnames: extnames, replace: replace)
65
+ end
66
+ end
67
+
68
+ # @param [String] src
69
+ # @param [String] dst
70
+ # @param [nil, Hash] src_path_alias
71
+ # @param [Array<String>] extnames
72
+ # @param [FalseClass, TrueClass] replace if replace .vue file to .js file, set true.
73
+ def convert_vue_project(src, dst, src_path_alias: nil, extnames: %w[.vue .js], replace: false)
74
+ check_params(src, dst)
75
+ src_pathname = Pathname.new(src).expand_path
76
+ dst_pathname = Pathname.new(dst)
77
+ new_src_pathname = dst_pathname + src_pathname.basename
78
+ path_alias = shift_path_alias(src_path_alias, new_src_pathname, src_pathname)
79
+ p "Converting #{src_pathname} to #{new_src_pathname}..."
80
+ cp_r(src_pathname, dst_pathname)
81
+ convert_directory(new_src_pathname.to_s, path_alias: path_alias, extnames: extnames, replace: replace)
82
+ end
83
+
84
+ def check_params(src, dst)
85
+ check_src(src)
86
+ check_dst(dst)
87
+ end
88
+ private_class_method :check_params
89
+
90
+ def check_src(src)
91
+ src_pathname = Pathname.new(src).expand_path
92
+ raise "#{src} must be absolute path." if src_pathname.relative?
93
+ raise "#{src} must be directory." unless src_pathname.directory?
94
+ end
95
+ private_class_method :check_src
96
+
97
+ # @param [String] dst
98
+ def check_dst(dst)
99
+ dst_pathname = Pathname.new(dst)
100
+ raise "#{dst} is already existed." if dst_pathname.exist? || dst_pathname.directory?
101
+ end
102
+ private_class_method :check_dst
103
+
104
+ # Copy directory like cp -r.
105
+ # @param [Pathname] src_pathname
106
+ # @param [Pathname] dst_pathname
107
+ def cp_r(src_pathname, dst_pathname)
108
+ dst_pathname.mkdir
109
+ FileUtils.cp_r(src_pathname.to_s, dst_pathname.to_s)
110
+ end
111
+ private_class_method :cp_r
112
+
113
+ def shift_path_alias(src_path_alias, new_src_pathname, src_pathname)
114
+ return if src_path_alias.nil?
115
+
116
+ src_path_alias.transform_values do |value|
117
+ alias_pathname = Pathname.new(value)
118
+ raise "#{value} must be absolute." if alias_pathname.relative?
119
+ raise "#{value} must be directory." unless alias_pathname.directory?
120
+
121
+ new_alias_pathname = new_src_pathname + alias_pathname.relative_path_from(src_pathname)
122
+ new_alias_pathname.expand_path.to_s
123
+ end
124
+ end
125
+ private_class_method :shift_path_alias
126
+ end
data/sig/vuesfc2js.rbs ADDED
@@ -0,0 +1,4 @@
1
+ module Vuesfc2js
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
data/vuesfc2js.gemspec ADDED
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/vuesfc2js/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "vuesfc2js"
7
+ spec.version = Vuesfc2js::VERSION
8
+ spec.authors = ["junara"]
9
+ spec.email = ["jun5araki@gmail.com"]
10
+
11
+ spec.summary = "Extract script part from Vue SFC and convert to JavaScript."
12
+ spec.description = "Extract script part from Vue SFC and convert to JavaScript."
13
+ spec.homepage = "https://github.com/junara/vuesfc2js"
14
+ spec.license = "MIT"
15
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.5.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/vuesfc2js"
21
+
22
+ spec.files = Dir.chdir(__dir__) do
23
+ `git ls-files -z`.split("\x0").reject do |f|
24
+ (f == __FILE__) || f.match(%r{\A(?:(?:bin|test|spec|features)/|\.(?:git|travis|circleci)|appveyor)})
25
+ end
26
+ end
27
+ spec.bindir = "exe"
28
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
29
+ spec.require_paths = ["lib"]
30
+
31
+ spec.add_development_dependency "factory_bot"
32
+ spec.add_development_dependency "rspec"
33
+ spec.add_development_dependency "rspec-parameterized"
34
+ spec.add_development_dependency "rubocop"
35
+ spec.add_development_dependency "rubocop-performance"
36
+ spec.add_development_dependency "rubocop-rake"
37
+ spec.add_development_dependency "rubocop-rspec"
38
+
39
+ spec.metadata["rubygems_mfa_required"] = "true"
40
+ end
metadata ADDED
@@ -0,0 +1,166 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: vuesfc2js
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-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: factory_bot
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
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: Extract script part from Vue SFC and convert to JavaScript.
112
+ email:
113
+ - jun5araki@gmail.com
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - ".rspec"
119
+ - ".rubocop.yml"
120
+ - CHANGELOG.md
121
+ - CODE_OF_CONDUCT.md
122
+ - Gemfile
123
+ - Gemfile.lock
124
+ - LICENSE.txt
125
+ - README.md
126
+ - Rakefile
127
+ - lib/vuesfc2js.rb
128
+ - lib/vuesfc2js/conversion/base.rb
129
+ - lib/vuesfc2js/conversion/replace_dot_vue2js.rb
130
+ - lib/vuesfc2js/conversion/replace_filename_dot_vue2js.rb
131
+ - lib/vuesfc2js/conversion/replace_path_alias.rb
132
+ - lib/vuesfc2js/conversion/script_extraction.rb
133
+ - lib/vuesfc2js/error/not_exist_error.rb
134
+ - lib/vuesfc2js/error/not_implemented_error.rb
135
+ - lib/vuesfc2js/version.rb
136
+ - sig/vuesfc2js.rbs
137
+ - vuesfc2js.gemspec
138
+ homepage: https://github.com/junara/vuesfc2js
139
+ licenses:
140
+ - MIT
141
+ metadata:
142
+ homepage_uri: https://github.com/junara/vuesfc2js
143
+ source_code_uri: https://github.com/junara/vuesfc2js
144
+ changelog_uri: https://github.com/junara/vuesfc2js/blob/main/CHANGELOG.md
145
+ documentation_uri: https://rubydoc.info/gems/vuesfc2js
146
+ rubygems_mfa_required: 'true'
147
+ post_install_message:
148
+ rdoc_options: []
149
+ require_paths:
150
+ - lib
151
+ required_ruby_version: !ruby/object:Gem::Requirement
152
+ requirements:
153
+ - - ">="
154
+ - !ruby/object:Gem::Version
155
+ version: 2.5.0
156
+ required_rubygems_version: !ruby/object:Gem::Requirement
157
+ requirements:
158
+ - - ">="
159
+ - !ruby/object:Gem::Version
160
+ version: '0'
161
+ requirements: []
162
+ rubygems_version: 3.1.6
163
+ signing_key:
164
+ specification_version: 4
165
+ summary: Extract script part from Vue SFC and convert to JavaScript.
166
+ test_files: []