npm_extractor 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: ee218e1b1a49682045cab6aeff12038c7f0d75955fb7d224424fedccb7331529
4
+ data.tar.gz: f59e7f9d0172ca39e60a3891539c3b24b630676b4f0e98c7191d9d1cac0c1ef4
5
+ SHA512:
6
+ metadata.gz: 5b19aea65f315a2d30542c1f35132c964af59ff3eda2cfda01c45c0912d89719aaeeec96e5c24b2782367400a791bbadfb23aec51a95d97202e8fc5dad604386
7
+ data.tar.gz: 46460ac426fbc23cae612fb34eb51569198f2e063f48f21cd8e3d1bc48a7c16c703892c3b4dcd0a08dc49cca4c45843457f8df3bb65a0fb62042b911520c5adf
@@ -0,0 +1,7 @@
1
+ {
2
+ "image": "mcr.microsoft.com/devcontainers/universal:2",
3
+ "features": {
4
+ "ghcr.io/devcontainers/features/ruby:1": {},
5
+ "ghcr.io/roul/devcontainer-features/bitwarden-cli:1": {}
6
+ }
7
+ }
data/.gitignore ADDED
@@ -0,0 +1,56 @@
1
+ *.gem
2
+ *.rbc
3
+ /.config
4
+ /coverage/
5
+ /InstalledFiles
6
+ /pkg/
7
+ /spec/reports/
8
+ /spec/examples.txt
9
+ /test/tmp/
10
+ /test/version_tmp/
11
+ /tmp/
12
+
13
+ # Used by dotenv library to load environment variables.
14
+ # .env
15
+
16
+ # Ignore Byebug command history file.
17
+ .byebug_history
18
+
19
+ ## Specific to RubyMotion:
20
+ .dat*
21
+ .repl_history
22
+ build/
23
+ *.bridgesupport
24
+ build-iPhoneOS/
25
+ build-iPhoneSimulator/
26
+
27
+ ## Specific to RubyMotion (use of CocoaPods):
28
+ #
29
+ # We recommend against adding the Pods directory to your .gitignore. However
30
+ # you should judge for yourself, the pros and cons are mentioned at:
31
+ # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
32
+ #
33
+ # vendor/Pods/
34
+
35
+ ## Documentation cache and generated files:
36
+ /.yardoc/
37
+ /_yardoc/
38
+ /doc/
39
+ /rdoc/
40
+
41
+ ## Environment normalization:
42
+ /.bundle/
43
+ /vendor/bundle
44
+ /lib/bundler/man/
45
+
46
+ # for a library or gem, you might want to ignore these files since the code is
47
+ # intended to run in multiple environments; otherwise, check them in:
48
+ # Gemfile.lock
49
+ # .ruby-version
50
+ # .ruby-gemset
51
+
52
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
53
+ .rvmrc
54
+
55
+ # Used by RuboCop. Remote config files pulled in from inherit_from directive.
56
+ # .rubocop-https?--*
data/.rubocop.yml ADDED
@@ -0,0 +1,14 @@
1
+ AllCops:
2
+ TargetRubyVersion: 3.1
3
+ SuggestExtensions: false
4
+ NewCops: enable
5
+
6
+ Style/StringLiterals:
7
+ EnforcedStyle: double_quotes
8
+
9
+ Style/StringLiteralsInInterpolation:
10
+ EnforcedStyle: double_quotes
11
+
12
+ plugins:
13
+ - rubocop-minitest
14
+ - rubocop-rake
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2025-11-10
4
+
5
+ - Initial release
@@ -0,0 +1,132 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our
6
+ community a harassment-free experience for everyone, regardless of age, body
7
+ size, visible or invisible disability, ethnicity, sex characteristics, gender
8
+ identity and expression, level of experience, education, socio-economic status,
9
+ nationality, personal appearance, race, caste, color, religion, or sexual
10
+ identity and orientation.
11
+
12
+ We pledge to act and interact in ways that contribute to an open, welcoming,
13
+ diverse, inclusive, and healthy community.
14
+
15
+ ## Our Standards
16
+
17
+ Examples of behavior that contributes to a positive environment for our
18
+ community include:
19
+
20
+ * Demonstrating empathy and kindness toward other people
21
+ * Being respectful of differing opinions, viewpoints, and experiences
22
+ * Giving and gracefully accepting constructive feedback
23
+ * Accepting responsibility and apologizing to those affected by our mistakes,
24
+ and learning from the experience
25
+ * Focusing on what is best not just for us as individuals, but for the overall
26
+ community
27
+
28
+ Examples of unacceptable behavior include:
29
+
30
+ * The use of sexualized language or imagery, and sexual attention or advances of
31
+ any kind
32
+ * Trolling, insulting or derogatory comments, and personal or political attacks
33
+ * Public or private harassment
34
+ * Publishing others' private information, such as a physical or email address,
35
+ without their explicit permission
36
+ * Other conduct which could reasonably be considered inappropriate in a
37
+ professional setting
38
+
39
+ ## Enforcement Responsibilities
40
+
41
+ Community leaders are responsible for clarifying and enforcing our standards of
42
+ acceptable behavior and will take appropriate and fair corrective action in
43
+ response to any behavior that they deem inappropriate, threatening, offensive,
44
+ or harmful.
45
+
46
+ Community leaders have the right and responsibility to remove, edit, or reject
47
+ comments, commits, code, wiki edits, issues, and other contributions that are
48
+ not aligned to this Code of Conduct, and will communicate reasons for moderation
49
+ decisions when appropriate.
50
+
51
+ ## Scope
52
+
53
+ This Code of Conduct applies within all community spaces, and also applies when
54
+ an individual is officially representing the community in public spaces.
55
+ Examples of representing our community include using an official email address,
56
+ posting via an official social media account, or acting as an appointed
57
+ representative at an online or offline event.
58
+
59
+ ## Enforcement
60
+
61
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
62
+ reported to the community leaders responsible for enforcement at
63
+ [INSERT CONTACT METHOD].
64
+ All complaints will be reviewed and investigated promptly and fairly.
65
+
66
+ All community leaders are obligated to respect the privacy and security of the
67
+ reporter of any incident.
68
+
69
+ ## Enforcement Guidelines
70
+
71
+ Community leaders will follow these Community Impact Guidelines in determining
72
+ the consequences for any action they deem in violation of this Code of Conduct:
73
+
74
+ ### 1. Correction
75
+
76
+ **Community Impact**: Use of inappropriate language or other behavior deemed
77
+ unprofessional or unwelcome in the community.
78
+
79
+ **Consequence**: A private, written warning from community leaders, providing
80
+ clarity around the nature of the violation and an explanation of why the
81
+ behavior was inappropriate. A public apology may be requested.
82
+
83
+ ### 2. Warning
84
+
85
+ **Community Impact**: A violation through a single incident or series of
86
+ actions.
87
+
88
+ **Consequence**: A warning with consequences for continued behavior. No
89
+ interaction with the people involved, including unsolicited interaction with
90
+ those enforcing the Code of Conduct, for a specified period of time. This
91
+ includes avoiding interactions in community spaces as well as external channels
92
+ like social media. Violating these terms may lead to a temporary or permanent
93
+ ban.
94
+
95
+ ### 3. Temporary Ban
96
+
97
+ **Community Impact**: A serious violation of community standards, including
98
+ sustained inappropriate behavior.
99
+
100
+ **Consequence**: A temporary ban from any sort of interaction or public
101
+ communication with the community for a specified period of time. No public or
102
+ private interaction with the people involved, including unsolicited interaction
103
+ with those enforcing the Code of Conduct, is allowed during this period.
104
+ Violating these terms may lead to a permanent ban.
105
+
106
+ ### 4. Permanent Ban
107
+
108
+ **Community Impact**: Demonstrating a pattern of violation of community
109
+ standards, including sustained inappropriate behavior, harassment of an
110
+ individual, or aggression toward or disparagement of classes of individuals.
111
+
112
+ **Consequence**: A permanent ban from any sort of public interaction within the
113
+ community.
114
+
115
+ ## Attribution
116
+
117
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
118
+ version 2.1, available at
119
+ [https://www.contributor-covenant.org/version/2/1/code_of_conduct.html][v2.1].
120
+
121
+ Community Impact Guidelines were inspired by
122
+ [Mozilla's code of conduct enforcement ladder][Mozilla CoC].
123
+
124
+ For answers to common questions about this code of conduct, see the FAQ at
125
+ [https://www.contributor-covenant.org/faq][FAQ]. Translations are available at
126
+ [https://www.contributor-covenant.org/translations][translations].
127
+
128
+ [homepage]: https://www.contributor-covenant.org
129
+ [v2.1]: https://www.contributor-covenant.org/version/2/1/code_of_conduct.html
130
+ [Mozilla CoC]: https://github.com/mozilla/diversity
131
+ [FAQ]: https://www.contributor-covenant.org/faq
132
+ [translations]: https://www.contributor-covenant.org/translations
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Daniel Bub
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 all
13
+ 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 THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,284 @@
1
+ # npm-extractor
2
+
3
+ A Ruby gem to support obtaining assets from npm. Perfect for vendoring frontend assets in Ruby projects without requiring Node.js or npm installed.
4
+
5
+ ## Features
6
+
7
+ - 🚀 Download NPM packages directly from the NPM registry
8
+ - 🎯 Selective file extraction with wildcard support (`*` and `**`)
9
+ - 📦 Automatic version resolution (latest or specific versions)
10
+ - 🔧 Zero external dependencies (uses only Ruby standard library)
11
+ - 🛠️ Easy integration with Rake tasks and build processes
12
+ - 🎨 Perfect for Rails asset pipeline integration
13
+
14
+ ## Installation
15
+
16
+ Add this line to your application's Gemfile:
17
+
18
+ ```ruby
19
+ gem 'npm_extractor'
20
+ ```
21
+
22
+ And then execute:
23
+
24
+ ```bash
25
+ bundle install
26
+ ```
27
+
28
+ Or install it yourself as:
29
+
30
+ ```bash
31
+ gem install npm_extractor
32
+ ```
33
+
34
+ ## Usage
35
+
36
+ ### Basic Usage
37
+
38
+ ```ruby
39
+ require 'npm_extractor'
40
+
41
+ # Define file mappings
42
+ file_mapping = {
43
+ 'package/dist/**/*.css' => './app/assets/stylesheets/vendor/',
44
+ 'package/dist/**/*.js' => './app/assets/javascripts/vendor/'
45
+ }
46
+
47
+ # Extract latest version
48
+ NpmExtractor::Extractor.extract_npm_package('pure-uikit', file_mapping)
49
+
50
+ # Or specify a specific version
51
+ NpmExtractor::Extractor.extract_npm_package('pure-uikit', file_mapping, version: '1.6.38')
52
+ ```
53
+
54
+ ### Wildcard Patterns
55
+
56
+ The gem supports two types of wildcards:
57
+
58
+ - `*` - Matches any characters except `/` (single directory level)
59
+ - `**` - Matches any characters including `/` (recursive, all subdirectories)
60
+
61
+ **Examples:**
62
+
63
+ ```ruby
64
+ file_mapping = {
65
+ # Extract a specific file
66
+ 'package/LICENSE' => './vendor/licenses/pure-uikit-LICENSE',
67
+
68
+ # Extract all CSS files from dist directory (non-recursive)
69
+ 'package/dist/*.css' => './public/css/',
70
+
71
+ # Extract all JS files recursively from dist and subdirectories
72
+ 'package/dist/**/*.js' => './public/js/',
73
+
74
+ # Extract entire directory tree
75
+ 'package/dist/**' => './vendor/pure-uikit/',
76
+
77
+ # Extract all JSON files from anywhere in package
78
+ 'package/**/*.json' => './config/'
79
+ }
80
+ ```
81
+
82
+ ### Rake Task Integration
83
+
84
+ Create a Rake task in your `Rakefile` or `lib/tasks/npm_assets.rake`:
85
+
86
+ ```ruby
87
+ require 'npm_extractor'
88
+
89
+ namespace :assets do
90
+ desc "Download frontend assets from NPM"
91
+ task :download_npm do
92
+ # Download Bootstrap
93
+ bootstrap_mapping = {
94
+ 'package/dist/css/**' => 'app/assets/stylesheets/bootstrap/',
95
+ 'package/dist/js/**' => 'app/assets/javascripts/bootstrap/'
96
+ }
97
+ NpmExtractor::Extractor.extract_npm_package('bootstrap', bootstrap_mapping)
98
+
99
+ # Download Pure UI Kit
100
+ pure_ui_mapping = {
101
+ 'package/dist/**' => 'vendor/assets/pure-uikit/'
102
+ }
103
+ NpmExtractor::Extractor.extract_npm_package('pure-uikit', pure_ui_mapping)
104
+
105
+ puts "✅ All NPM assets downloaded!"
106
+ end
107
+ end
108
+
109
+ # Hook into Rails asset precompilation
110
+ if Rake::Task.task_defined?('assets:precompile')
111
+ Rake::Task['assets:precompile'].enhance(['assets:download_npm'])
112
+ end
113
+ ```
114
+
115
+ Then run:
116
+
117
+ ```bash
118
+ rake assets:download_npm
119
+ ```
120
+
121
+ ### Rails Integration Example
122
+
123
+ For a Rails application, you might want to download assets before deployment:
124
+
125
+ **lib/tasks/frontend_assets.rake:**
126
+
127
+ ```ruby
128
+ namespace :frontend do
129
+ desc "Download all frontend dependencies from NPM"
130
+ task :setup do
131
+ require 'npm_extractor'
132
+
133
+ packages = {
134
+ 'pure-uikit' => {
135
+ 'package/dist/**/*.css' => 'app/assets/stylesheets/vendor/pure-uikit/',
136
+ 'package/dist/**/*.js' => 'app/assets/javascripts/vendor/pure-uikit/'
137
+ },
138
+ 'htmx.org' => {
139
+ 'package/dist/htmx.min.js' => 'app/assets/javascripts/vendor/htmx.js'
140
+ },
141
+ 'alpinejs' => {
142
+ 'package/dist/cdn.min.js' => 'app/assets/javascripts/vendor/alpine.js'
143
+ }
144
+ }
145
+
146
+ packages.each do |package_name, file_mapping|
147
+ puts "\n📦 Downloading #{package_name}..."
148
+ NpmExtractor::Extractor.extract_npm_package(package_name, file_mapping)
149
+ end
150
+
151
+ puts "\n✅ All frontend assets installed!"
152
+ end
153
+ end
154
+ ```
155
+
156
+ ### Standalone Script Example
157
+
158
+ ```ruby
159
+ #!/usr/bin/env ruby
160
+ require 'npm_extractor'
161
+
162
+ # Download specific version of a package
163
+ NpmExtractor::Extractor.extract_npm_package(
164
+ 'lodash',
165
+ {
166
+ 'package/lodash.min.js' => './public/vendor/lodash.js',
167
+ 'package/LICENSE' => './public/vendor/lodash-LICENSE'
168
+ },
169
+ version: '4.17.21'
170
+ )
171
+
172
+ puts "Lodash installed!"
173
+ ```
174
+
175
+ ## Understanding NPM Package Structure
176
+
177
+ NPM packages in tarballs are typically extracted with a `package/` prefix. This means:
178
+
179
+ - Package root becomes: `package/`
180
+ - Package's `dist/` folder becomes: `package/dist/`
181
+ - Package's `LICENSE` file becomes: `package/LICENSE`
182
+
183
+ Always include `package/` in your file mapping patterns!
184
+
185
+ ## Common Use Cases
186
+
187
+ ### 1. Vendor CSS Frameworks
188
+
189
+ ```ruby
190
+ # Tailwind CSS
191
+ NpmExtractor::Extractor.extract_npm_package('tailwindcss', {
192
+ 'package/dist/tailwind.min.css' => './public/css/tailwind.css'
193
+ })
194
+ ```
195
+
196
+ ### 2. Download JavaScript Libraries
197
+
198
+ ```ruby
199
+ # Chart.js
200
+ NpmExtractor::Extractor.extract_npm_package('chart.js', {
201
+ 'package/dist/chart.min.js' => './public/js/chart.js',
202
+ 'package/dist/chart.min.js.map' => './public/js/chart.min.js.map'
203
+ })
204
+ ```
205
+
206
+ ### 3. Extract Documentation or Licenses
207
+
208
+ ```ruby
209
+ # Get all markdown files
210
+ NpmExtractor::Extractor.extract_npm_package('some-package', {
211
+ 'package/**/*.md' => './docs/vendor/some-package/',
212
+ 'package/LICENSE' => './licenses/some-package-LICENSE'
213
+ })
214
+ ```
215
+
216
+ ### 4. Download Web Components
217
+
218
+ ```ruby
219
+ # Shoelace (Web Components)
220
+ NpmExtractor::Extractor.extract_npm_package('@shoelace-style/shoelace', {
221
+ 'package/dist/**' => './public/shoelace/'
222
+ })
223
+ ```
224
+
225
+ ## Error Handling
226
+
227
+ The extractor will raise errors for:
228
+
229
+ - Non-existent packages
230
+ - Network issues
231
+ - Invalid package versions
232
+ - Write permission errors
233
+
234
+ Wrap your calls in begin/rescue blocks for custom error handling:
235
+
236
+ ```ruby
237
+ begin
238
+ NpmExtractor::Extractor.extract_npm_package('my-package', file_mapping)
239
+ rescue StandardError => e
240
+ puts "Failed to download package: #{e.message}"
241
+ exit 1
242
+ end
243
+ ```
244
+
245
+ ## Development
246
+
247
+ After checking out the repo, run:
248
+
249
+ ```bash
250
+ bin/setup # Install dependencies
251
+ bin/test # Run tests
252
+ rake # Run tests and rubocop
253
+ ```
254
+
255
+ To install this gem onto your local machine:
256
+
257
+ ```bash
258
+ bundle exec rake install
259
+ ```
260
+
261
+ To release a new version:
262
+
263
+ 1. Update the version number in `lib/npm_extractor/version.rb`
264
+ 2. Run `bundle exec rake release` to create a git tag and push to RubyGems
265
+
266
+ ## Testing
267
+
268
+ Run the test suite:
269
+
270
+ ```bash
271
+ rake test
272
+ ```
273
+
274
+ Run a specific test:
275
+
276
+ ```bash
277
+ ruby -Ilib:test test/npm_extractor/extractor_test.rb -n test_extract_license_from_acme_package
278
+ ```
279
+
280
+ **Note:** Tests make real network requests to the NPM registry and require an internet connection.
281
+
282
+ ## License
283
+
284
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rake/testtask"
5
+
6
+ Rake::TestTask.new(:test) do |t|
7
+ t.libs << "test"
8
+ t.libs << "lib"
9
+ t.test_files = FileList["test/**/*_test.rb"]
10
+ end
11
+
12
+ require "rubocop/rake_task"
13
+
14
+ RuboCop::RakeTask.new
15
+
16
+ task default: %i[test rubocop]
@@ -0,0 +1,125 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "net/http"
4
+ require "uri"
5
+ require "rubygems/package"
6
+ require "zlib"
7
+ require "fileutils"
8
+ require "json"
9
+
10
+ module NpmExtractor
11
+ # Extractor downloads NPM packages and selectively extract assets from provided release.
12
+ #
13
+ # Example:
14
+ # Extractor.extract_npm_package('example',{'package/LICENSE' => 'vendor/example/LICENSE'})
15
+ class Extractor
16
+ def self.extract_npm_package(package_name, file_mapping, version: "latest")
17
+ new.extract_npm_package(package_name, file_mapping, version: version)
18
+ end
19
+
20
+ def extract_npm_package(package_name, file_mapping, version: "latest")
21
+ registry_url = "https://registry.npmjs.org/#{package_name}/#{version}"
22
+
23
+ puts "Fetching package metadata from #{registry_url}..."
24
+
25
+ package_info = JSON.parse(safe_fetch(registry_url).body)
26
+ tarball_url = package_info["dist"]["tarball"]
27
+ package_version = package_info["version"]
28
+
29
+ puts "Found version #{package_version}"
30
+ puts "Downloading from #{tarball_url}..."
31
+
32
+ extract_tgz_files(tarball_url, file_mapping)
33
+ end
34
+
35
+ def extract_tgz_files(url, file_mapping)
36
+ tgz_file = nil
37
+
38
+ begin
39
+ npm_response = safe_fetch(url)
40
+
41
+ tgz_file = Tempfile.new(["archive", ".tgz"])
42
+ tgz_file.binmode
43
+ tgz_file.write(npm_response.read_body)
44
+ tgz_file.close
45
+
46
+ Zlib::GzipReader.open(tgz_file.path) do |gz|
47
+ Gem::Package::TarReader.new(gz) do |tar|
48
+ tar.each do |entry|
49
+ next if entry.directory?
50
+
51
+ matched_pattern, destination_base = find_matching_pattern(entry.full_name, file_mapping)
52
+ next unless matched_pattern
53
+
54
+ destination = calculate_destination(entry.full_name, matched_pattern, destination_base)
55
+
56
+ FileUtils.mkdir_p(File.dirname(destination))
57
+
58
+ File.binwrite(destination, entry.read)
59
+
60
+ puts "Extracted: #{entry.full_name} -> #{destination}"
61
+ end
62
+ end
63
+ end
64
+
65
+ puts "Extraction complete!"
66
+ rescue StandardError => e
67
+ puts "Error: #{e.message}"
68
+ raise
69
+ ensure
70
+ tgz_file&.unlink
71
+ end
72
+ end
73
+
74
+ private
75
+
76
+ def find_matching_pattern(file_path, file_mapping)
77
+ file_mapping.each do |pattern, destination|
78
+ if pattern.include?("*")
79
+ regex_pattern = pattern.gsub("**", "|DOUBLE|")
80
+ .gsub("*", "[^/]*")
81
+ .gsub("|DOUBLE|", ".*")
82
+ regex = /^#{regex_pattern}$/
83
+
84
+ return [pattern, destination] if file_path.match?(regex)
85
+ elsif file_path == pattern
86
+ return [pattern, destination]
87
+ end
88
+ end
89
+ nil
90
+ end
91
+
92
+ def calculate_destination(file_path, pattern, destination_base)
93
+ if pattern.include?("*")
94
+ prefix = pattern.split("*").first
95
+ relative_path = file_path.sub(/^#{Regexp.escape(prefix)}/, "")
96
+ File.join(destination_base, relative_path)
97
+ else
98
+ destination_base
99
+ end
100
+ end
101
+
102
+ def safe_fetch(uri_str, max_redirects: 5)
103
+ uri = URI.parse(uri_str)
104
+
105
+ redirects = 0
106
+ loop do
107
+ http = Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == "https")
108
+ req = Net::HTTP::Get.new(uri)
109
+ res = http.request(req)
110
+
111
+ case res
112
+ when Net::HTTPSuccess
113
+ return res
114
+ when Net::HTTPRedirection
115
+ raise "Too many redirects" if (redirects += 1) > max_redirects
116
+
117
+ uri = URI.parse(res["location"])
118
+ next
119
+ else
120
+ raise "HTTP error: #{res.code}"
121
+ end
122
+ end
123
+ end
124
+ end
125
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module NpmExtractor
4
+ VERSION = "1.0.0"
5
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "npm_extractor/version"
4
+ require_relative "npm_extractor/extractor"
5
+
6
+ module NpmExtractor
7
+ class Error < StandardError; end
8
+ end
metadata ADDED
@@ -0,0 +1,56 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: npm_extractor
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Daniel Bub
8
+ bindir: bin
9
+ cert_chain: []
10
+ date: 2025-11-10 00:00:00.000000000 Z
11
+ dependencies: []
12
+ description: A Ruby gem that downloads NPM packages and selectively extracts files
13
+ using wildcard patterns.
14
+ email:
15
+ - thebub@users.noreply.github.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".devcontainer/devcontainer.json"
21
+ - ".gitignore"
22
+ - ".rubocop.yml"
23
+ - CHANGELOG.md
24
+ - CODE_OF_CONDUCT.md
25
+ - LICENSE
26
+ - README.md
27
+ - Rakefile
28
+ - lib/npm_extractor.rb
29
+ - lib/npm_extractor/extractor.rb
30
+ - lib/npm_extractor/version.rb
31
+ homepage: https://github.com/thebub/npm_extractor
32
+ licenses:
33
+ - MIT
34
+ metadata:
35
+ homepage_uri: https://github.com/thebub/npm_extractor
36
+ source_code_uri: https://github.com/yourusername/npm_extractor
37
+ changelog_uri: https://github.com/yourusername/npm_extractor/blob/main/CHANGELOG.md
38
+ rubygems_mfa_required: 'true'
39
+ rdoc_options: []
40
+ require_paths:
41
+ - lib
42
+ required_ruby_version: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: 3.1.0
47
+ required_rubygems_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: '0'
52
+ requirements: []
53
+ rubygems_version: 3.6.2
54
+ specification_version: 4
55
+ summary: Extract files from NPM packages with selective file mapping
56
+ test_files: []