importmap-cli 0.1.0 → 0.4.0

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: 57b42db4d7acbfe99f5958275e127e60bc63c7515a12a366f96f7cbbb88b05a2
4
- data.tar.gz: 54592dce82bc7209398b75855d0ccf389081f3287127b61c15e83ae37daa88b4
3
+ metadata.gz: 8ba33fe0fd763e34d59b1a53f3b3c84d6e10e744b5fe96b7ee2ed46909cbb52c
4
+ data.tar.gz: a8e8e42b4fd7960755c27bd92647da3eedf46a909615942a6cf4c2d7f9bc2980
5
5
  SHA512:
6
- metadata.gz: 8b771f63ff1a81741682695c3ac66533c14535eff28c195e8a7e515319bd485a226874771661fdcebca63de0d99a41b4cc80e7595321718a3556f92c0e1762af
7
- data.tar.gz: 99e64d4f3e08e15523858bc6aa13a0e5b0a4ce806203d2328c4b24ed6f8b278f15113d5333351c8c190727fcc5e2a4b04149870ebbad822965662830c868665e
6
+ metadata.gz: 7ab3302b8fadc198e50b0823340079c053bb27c4bd72be7027f9af98915c6f884353c565305529d4be20e633ddd272fd987eaa61fc6883e3af46f791c1a7cdf2
7
+ data.tar.gz: 0f9b9942236f35bf08a703f967f6696616f839a8c734acab16c68c86ec631c9e2638277f5639d96c0efa282b64aea289800df68d345ae1c19f33b6cc68f253d4
data/bin/importmap CHANGED
@@ -16,6 +16,7 @@ module ImportmapCLI
16
16
  desc 'add [*PACKAGES]', 'Pin new packages'
17
17
  option :env, type: :string, aliases: :e, default: 'production'
18
18
  option :from, type: :string, aliases: :f, default: 'jspm'
19
+ option :format, type: :string, repeatable: false
19
20
  def add(*packages)
20
21
  main.add(packages:, options:)
21
22
  end
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'uri'
4
+ require 'yaml'
4
5
  require 'json'
5
6
  require 'net/http'
6
7
  require 'fileutils'
@@ -13,8 +14,8 @@ module ImportmapCLI
13
14
  RESOURCE_URL = URI('https://api.jspm.io/generate').freeze
14
15
 
15
16
  def initialize(packages:, options:)
16
- @packages = packages
17
- @options = options
17
+ @options = Hash(options)
18
+ @packages = current_packages + packages
18
19
  end
19
20
 
20
21
  def run
@@ -27,27 +28,26 @@ module ImportmapCLI
27
28
  FileUtils.mkdir_p("#{ImportmapCLI.current_dir}/vendor/javascript")
28
29
 
29
30
  # ensure importmap.rb exists
30
- unless File.exist?("#{ImportmapCLI.current_dir}/config/importmap.rb")
31
- puts "[error] #{ImportmapCLI.current_dir}/config/importmap.rb file does not exist"
31
+ unless File.exist?("#{ImportmapCLI.current_dir}#{filename}")
32
+ puts "[error] #{ImportmapCLI.current_dir}#{filename} file does not exist"
32
33
  exit 1
33
34
  end
34
35
 
35
- @importmap = File.read("#{ImportmapCLI.current_dir}/config/importmap.rb")
36
-
37
- payload.fetch(:imports, {}).each do |package, url|
38
- puts "[info] pinning #{package} to #{url}"
39
-
40
- unless @importmap.match?(/^pin\s+['"]#{package}["']/)
41
- @importmap = "#{@importmap}\npin '#{package}', to: '#{url}' # #{package_version_from(url)}"
42
- end
43
- end
44
-
45
- File.write("#{ImportmapCLI.current_dir}/config/importmap.rb", @importmap)
36
+ importmap_generated = build_importmap(payload)
37
+ File.write("#{ImportmapCLI.current_dir}#{filename}", importmap_generated)
46
38
  end
47
39
  end
48
40
 
49
41
  private
50
42
 
43
+ def filename
44
+ @filename ||= "/config/importmap.#{file_extension}"
45
+ end
46
+
47
+ def file_extension
48
+ @file_extension ||= Hash(@options).fetch('format', 'rb')
49
+ end
50
+
51
51
  def same_filename?(package, filename)
52
52
  "#{package.gsub('/', '--')}.js" == filename
53
53
  end
@@ -56,6 +56,37 @@ module ImportmapCLI
56
56
  url.scan(/@(\d+\.\d+.\d+)/)&.flatten&.first
57
57
  end
58
58
 
59
+ def current_packages
60
+ file_content = File.read("#{ImportmapCLI.current_dir}#{filename}")
61
+
62
+ case file_extension
63
+ when 'json'
64
+ JSON.parse(file_content, symbolize_names: true).fetch(:imports, {}).keys
65
+ when 'yaml'
66
+ YAML.load(file_content).fetch(:imports, {}).keys
67
+ else
68
+ file_content.scan(/pin\s+['"](.+)['"],?.*/).flatten
69
+ end
70
+ end
71
+
72
+ def build_importmap(payload)
73
+ case file_extension
74
+ when 'json'
75
+ JSON.pretty_generate(payload)
76
+ when 'yaml'
77
+ YAML.dump(payload)
78
+ else
79
+ importmap = ''
80
+
81
+ payload.fetch(:imports, {}).each do |package, url|
82
+ puts "[info] pinning #{package} to #{url}"
83
+ importmap += "#{importmap}\npin '#{package}', to: '#{url}' # #{package_version_from(url)}"
84
+ end
85
+
86
+ importmap
87
+ end
88
+ end
89
+
59
90
  def request_body
60
91
  {
61
92
  'install' => Array(@packages),
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ImportmapCLI
4
- VERSION = '0.1.0'
4
+ VERSION = '0.4.0'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: importmap-cli
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel Vinciguerra
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-08-22 00:00:00.000000000 Z
11
+ date: 2024-08-23 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: importmap-cli is a command line tool to manage importmaps inpired from
14
14
  importmap-rails gem
@@ -43,7 +43,6 @@ files:
43
43
  - lib/importmap_cli/main.rb
44
44
  - lib/importmap_cli/roles/http_resource.rb
45
45
  - lib/importmap_cli/version.rb
46
- - sig/importmap/cli.rbs
47
46
  homepage: https://github.com/dvinciguerra/importmap-cli
48
47
  licenses:
49
48
  - MIT
@@ -1,6 +0,0 @@
1
- module Importmap
2
- module Cli
3
- VERSION: String
4
- # See the writing guide of rbs: https://github.com/ruby/rbs#guides
5
- end
6
- end