webpacker-routes 0.1.1 → 0.1.2

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: e97672c8e8fa59f10e40210007b6620c2a59bf0a257e6172731874f446cfb659
4
- data.tar.gz: ffb9e82fcf9f6ed0bae01e66a7177092737aae410e738bd749b5344efd4f11e5
3
+ metadata.gz: e0ace4cd3e92b5e779788f42c032b8227f8352cc124f72d5725ac08280125097
4
+ data.tar.gz: ca63b493a4a7f3f3115ca19a87883628cfdf864e2862b00d35c9e807af795e63
5
5
  SHA512:
6
- metadata.gz: 3405c23a0877f10fc5b42404fb8b6f0fb2305fc5d158b649dbe83dcbce05151ac89fe2afc1b5032826f9a393a2a0ba7f19fa97e04addc1a76f9f0fc0a3d36abc
7
- data.tar.gz: 1408d938a137462f15ee47f5f06e66d9e47e44478c0cefd57a4a16fb446b2e36c44660fc24d96a8940cc5ef703bbcaef895e98e191829bc354289edf623ffc64
6
+ metadata.gz: b2dec026375850eda7b48f59c0ffa89e17ccb36fc7f4ea87311047416aabd7fdfbaac2f53ec47a552c0e8c39b551a0ad8859df8a7dba5f4106a2bd53f23781de
7
+ data.tar.gz: ccf44fcf0f9e8c02b1de95b392a9992e3ed6646bd08bf11301d36bda1317347b256bcab93a83a197fd094436c8b7f27da742f499304b879b1d5b6662fce4b67d
@@ -24,28 +24,38 @@ module Webpacker
24
24
 
25
25
  default_url_options_var = var_name.call('default_url_options')
26
26
 
27
- File.atomic_write(Webpacker.config.routes_path.join('index.js')) do |file|
28
- file.write(<<-JAVASCRIPT.strip_heredoc)
29
- import { urlFor, pathFor } from 'webpacker-routes'
30
- const #{default_url_options_var} = #{js(default_url_options)}
31
- JAVASCRIPT
32
-
33
- app.routes.named_routes.sort_by(&:first).each do |name, route|
34
- raise `Invalid route name for javascript: ${name}` unless JAVASCRIPT_VARIABLE_NAME_REGEX =~ name
35
-
36
- spec = route.path.spec.to_s
37
- segment_keys = route.segment_keys.uniq
38
- options = route.defaults.except(*IGNORED_OPTIONS)
39
-
40
- spec_var = var_name.call("#{name}_spec")
41
- url_var = var_name.call("#{name}_url")
42
- path_var = var_name.call("#{name}_path")
43
-
44
- file.write(<<-JAVASCRIPT.strip_heredoc)
45
- const #{spec_var} = [#{js(spec)}, #{js(segment_keys)}, { ...#{default_url_options_var}, ...#{js(options)} }]
46
- export const #{url_var} = (...args) => urlFor(#{spec_var}, ...args)
47
- export const #{path_var} = (...args) => pathFor(#{spec_var}, ...args)
27
+ js_file = Webpacker.config.routes_path.join('index.js')
28
+
29
+ catch(:identical) do
30
+ File.atomic_write(js_file) do |temp_file|
31
+ temp_file.write(<<-JAVASCRIPT.strip_heredoc)
32
+ import { urlFor, pathFor } from 'webpacker-routes'
33
+ const #{default_url_options_var} = #{js(default_url_options)}
48
34
  JAVASCRIPT
35
+
36
+ app.routes.named_routes.sort_by(&:first).each do |name, route|
37
+ raise `Invalid route name for javascript: ${name}` unless JAVASCRIPT_VARIABLE_NAME_REGEX =~ name
38
+
39
+ spec = route.path.spec.to_s
40
+ segment_keys = route.segment_keys.uniq
41
+ options = route.defaults.except(*IGNORED_OPTIONS)
42
+
43
+ spec_var = var_name.call("#{name}_spec")
44
+ url_var = var_name.call("#{name}_url")
45
+ path_var = var_name.call("#{name}_path")
46
+
47
+ temp_file.write(<<-JAVASCRIPT.strip_heredoc)
48
+ const #{spec_var} = [#{js(spec)}, #{js(segment_keys)}, { ...#{default_url_options_var}, ...#{js(options)} }]
49
+ export const #{url_var} = (...args) => urlFor(#{spec_var}, ...args)
50
+ export const #{path_var} = (...args) => pathFor(#{spec_var}, ...args)
51
+ JAVASCRIPT
52
+ end
53
+
54
+ temp_file.close
55
+ if identical?(js_file.to_s, temp_file.path)
56
+ temp_file.unlink
57
+ throw :identical
58
+ end
49
59
  end
50
60
  end
51
61
  end
@@ -55,6 +65,12 @@ module Webpacker
55
65
  def js(obj)
56
66
  ERB::Util.json_escape(obj.to_json)
57
67
  end
68
+
69
+ def identical?(path1, path2)
70
+ FileUtils.compare_file(path1, path2)
71
+ rescue Errno::ENOENT
72
+ false
73
+ end
58
74
  end
59
75
  end
60
76
  end
@@ -8,13 +8,12 @@ module Webpacker
8
8
  config.webpacker.routes.camel_case = false
9
9
 
10
10
  config.after_initialize do |app|
11
- generate = -> { Webpacker::Routes.generate(app.tap(&:reload_routes!)) }
12
11
  if Rails::VERSION::MAJOR >= 5
13
- app.reloader.to_run(&generate)
12
+ app.reloader.to_run(:after) { Webpacker::Routes.generate(app) }
14
13
  else
15
- ActionDispatch::Reloader.to_prepare(&generate)
14
+ ActionDispatch::Reloader.to_prepare(:after) { Webpacker::Routes.generate(app) }
16
15
  end
17
- generate.call unless ENV['WEBPACKER_ROUTES_INSTALL'] == 'true'
16
+ Webpacker::Routes.generate(app.tap(&:reload_routes!)) unless ENV['WEBPACKER_ROUTES_INSTALL']
18
17
  end
19
18
  end
20
19
  end
@@ -1,5 +1,5 @@
1
1
  module Webpacker
2
2
  module Routes
3
- VERSION = '0.1.1'
3
+ VERSION = '0.1.2'
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: webpacker-routes
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - David Harsha
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-03-18 00:00:00.000000000 Z
11
+ date: 2019-12-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -100,8 +100,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
100
100
  - !ruby/object:Gem::Version
101
101
  version: '0'
102
102
  requirements: []
103
- rubyforge_project:
104
- rubygems_version: 2.7.6
103
+ rubygems_version: 3.0.3
105
104
  signing_key:
106
105
  specification_version: 4
107
106
  summary: Convert Rails routes to JavaScript modules