Skip to content

Commit 1ed0f24

Browse files
committed
Support banner option
1 parent 9c3cbcf commit 1ed0f24

File tree

12 files changed

+86
-30
lines changed

12 files changed

+86
-30
lines changed

CHANGELOG.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,24 @@
11
# Changelog
22

3+
## v2.3.2
4+
5+
* Add `banner` option that allow to control JSDoc on top of generated file. [#324](https://github.com/bogdan/repo/issues/324).
6+
7+
``` ruby
8+
JsRoutes.configure do |c|
9+
c.banner = -> {
10+
commit_hash = `git rev-parse --short HEAD`.strip
11+
12+
<<~DOC
13+
@file Javascript Route helpers of my magic pony app.
14+
@author Bogdan Gusiev
15+
@license MIT
16+
@version #{commit_hash}
17+
DOC
18+
}
19+
end
20+
```
21+
322
## v2.3.1
423

524
* Add timestamp on when routes.js was generated into banner.

Readme.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,17 @@ Options to configure JavaScript file generator. These options are only available
331331
* `optional_definition_params` - make all route paramters in definition optional
332332
* See [related compatibility issue](#optional-definition-params)
333333
* Default: `false`
334+
* `banner` - specify the comment on top of the file.
335+
* It is not stripped by minifiers by default and helps to originate the content when debugging the build.
336+
* You may want to control how much information from backend is exposed to potential attacker at the cost of your own comfort.
337+
* See [JSDoc Guide](https://github.com/shri/JSDoc-Style-Guide/blob/master/README.md#files)
338+
* Supports a lazy generation with `Proc`.
339+
* Default: A string that generates the following:
340+
341+
```
342+
File generated by js-routes 2.3.1 on <TIMESTAMP>
343+
Based on Rails 7.2.0 routes of App
344+
```
334345

335346
<div id="formatter-options"></div>
336347

lib/js_routes/configuration.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
require "pathname"
44
require "js_routes/types"
55
require 'js_routes/utils'
6+
require 'js_routes/version'
67

78
module JsRoutes
89
class Configuration
@@ -39,6 +40,8 @@ class Configuration
3940
attr_accessor :module_type
4041
sig { returns(T::Boolean) }
4142
attr_accessor :optional_definition_params
43+
sig { returns(BannerCaller) }
44+
attr_accessor :banner
4245

4346
sig {params(attributes: T.nilable(Options)).void }
4447
def initialize(attributes = nil)
@@ -57,6 +60,7 @@ def initialize(attributes = nil)
5760
@module_type = T.let('ESM', T.nilable(String))
5861
@documentation = T.let(true, T::Boolean)
5962
@optional_definition_params = T.let(false, T::Boolean)
63+
@banner = T.let(default_banner, BannerCaller)
6064

6165
return unless attributes
6266
assign(attributes)
@@ -158,5 +162,17 @@ def verify
158162
raise "JsRoutes namespace option can only be used if module_type is nil"
159163
end
160164
end
165+
166+
sig { returns(T.proc.returns(String)) }
167+
def default_banner
168+
-> () {
169+
app = application.is_a?(Proc) ? application.call : application
170+
<<~TXT
171+
File generated by js-routes #{JsRoutes::VERSION} on #{Time.now}
172+
Based on Rails #{Rails.version} routes of #{app.class}
173+
TXT
174+
175+
}
176+
end
161177
end
162178
end

lib/js_routes/instance.rb

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ def generate
2929
if named_routes.empty?
3030
if application.is_a?(Rails::Application)
3131
if Rails.version >= "8.0.0"
32-
application.reload_routes_unless_loaded
32+
T.unsafe(application).reload_routes_unless_loaded
3333
else
34-
application.reload_routes!
34+
T.unsafe(application).reload_routes!
3535
end
3636
end
3737
end
@@ -43,7 +43,20 @@ def generate
4343
raise("Missing key #{key} in JS template")
4444
end
4545
end
46-
content + routes_export + prevent_types_export
46+
banner + content + routes_export + prevent_types_export
47+
end
48+
49+
sig { returns(String) }
50+
def banner
51+
banner = @configuration.banner
52+
banner = banner.call if banner.is_a?(Proc)
53+
return "" if banner.blank?
54+
[
55+
"/**",
56+
*banner.split("\n").map { |line| " * #{line}" },
57+
" */",
58+
"",
59+
].join("\n")
4760
end
4861

4962
sig { void }
@@ -79,12 +92,11 @@ def js_variables
7992
prefix = @configuration.prefix
8093
prefix = prefix.call if prefix.is_a?(Proc)
8194
{
82-
'GEM_VERSION' => JsRoutes::VERSION,
83-
'TIMESTAMP' => Time.now.to_s,
95+
# 'GEM_VERSION' => JsRoutes::VERSION,
8496
'ROUTES_OBJECT' => routes_object,
85-
'RAILS_VERSION' => ::Rails.version,
97+
# 'RAILS_VERSION' => ::Rails.version,
8698
'DEPRECATED_FALSE_PARAMETER_BEHAVIOR' => Rails.version < '7.0.0',
87-
'APP_CLASS' => application.class.to_s,
99+
# 'APP_CLASS' => application.class.to_s,
88100
'DEFAULT_URL_OPTIONS' => json(@configuration.default_url_options),
89101
'PREFIX' => json(prefix),
90102
'SPECIAL_OPTIONS_KEY' => json(@configuration.special_options_key),
@@ -187,7 +199,7 @@ def mounted_app_routes(route)
187199
rails_engine_app = T.unsafe(app_from_route(route))
188200
if rails_engine_app.is_a?(Class) &&
189201
rails_engine_app < Rails::Engine && !route.path.anchored
190-
rails_engine_app.routes.named_routes.flat_map do |_, engine_route|
202+
T.unsafe(rails_engine_app).routes.named_routes.flat_map do |_, engine_route|
191203
route_helpers_if_match(engine_route, route)
192204
end
193205
else
@@ -200,7 +212,7 @@ def app_from_route(route)
200212
app = route.app
201213
# Rails Engine can use additional
202214
# ActionDispatch::Routing::Mapper::Constraints, which contain app
203-
if app.is_a?(ActionDispatch::Routing::Mapper::Constraints)
215+
if app.is_a?(T.unsafe(ActionDispatch::Routing::Mapper::Constraints))
204216
app.app
205217
else
206218
app

lib/js_routes/middleware.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ module JsRoutes
66
# whenever routes.rb is modified
77
#
88
# Inspired by
9-
# https://github.com/fnando/i18n-js/blob/main/lib/i18n/js/middleware.rb
9+
# https://github.com/fnando/i18n-js/blob/v3/lib/i18n/js/middleware.rb
1010
class Middleware
1111
include JsRoutes::Types
1212
include RackApp

lib/js_routes/types.rb

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,20 @@ module Types
1616
Literal = T.type_alias { T.any(String, Symbol) }
1717
JourneyRoute = T.type_alias{ActionDispatch::Journey::Route}
1818
RouteSpec = T.type_alias {T.untyped}
19-
Application = T.type_alias { T.any(T::Class[Rails::Engine], Rails::Application) }
20-
ApplicationCaller = T.type_alias { T.any(Application, T.proc.returns(Application)) }
19+
Application = T.type_alias do
20+
T.any(T::Class[Rails::Engine], Rails::Application)
21+
end
22+
ApplicationCaller = T.type_alias do
23+
T.any(Application, T.proc.returns(Application))
24+
end
25+
BannerCaller = T.type_alias do
26+
T.any(String, NilClass, T.proc.returns(T.any(String, NilClass)))
27+
end
2128
Clusivity = T.type_alias { T.any(Regexp, T::Array[Regexp]) }
2229
FileName = T.type_alias { T.any(String, Pathname, NilClass) }
23-
ConfigurationBlock = T.type_alias { T.proc.params(arg0: JsRoutes::Configuration).void }
30+
ConfigurationBlock = T.type_alias do
31+
T.proc.params(arg0: JsRoutes::Configuration).void
32+
end
2433
Prefix = T.type_alias do
2534
T.any(T.proc.returns(String), String, NilClass)
2635
end

lib/js_routes/version.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
# typed: strict
2+
require 'sorbet-runtime'
3+
24
module JsRoutes
3-
VERSION = "2.3.1"
5+
VERSION = T.let("2.3.1", String)
46
end

lib/routes.d.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
/**
2-
* File generated by js-routes RubyVariables.GEM_VERSION on RubyVariables.TIMESTAMP
3-
* Based on Rails RubyVariables.RAILS_VERSION routes of RubyVariables.APP_CLASS
4-
*/
51
declare type Optional<T> = {
62
[P in keyof T]?: T[P] | null;
73
};

lib/routes.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
/**
2-
* File generated by js-routes RubyVariables.GEM_VERSION on RubyVariables.TIMESTAMP
3-
* Based on Rails RubyVariables.RAILS_VERSION routes of RubyVariables.APP_CLASS
4-
*/
51
// eslint-disable-next-line
62
RubyVariables.WRAPPER(
73
// eslint-disable-next-line

lib/routes.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
1-
/**
2-
* File generated by js-routes RubyVariables.GEM_VERSION on RubyVariables.TIMESTAMP
3-
* Based on Rails RubyVariables.RAILS_VERSION routes of RubyVariables.APP_CLASS
4-
*/
5-
61
type Optional<T> = { [P in keyof T]?: T[P] | null };
72
type Collection<T> = Record<string, T>;
83

0 commit comments

Comments
 (0)