@@ -21,7 +21,7 @@ defmodule Mix.Tasks.Backpex.Install.Docs do
21
21
## What this installer does:
22
22
23
23
- Sets up [Global Configuration](installation.html#global-configuration) by configuring the PubSub server
24
- - Adds [Backpex Hooks](installation.html#backpex-hooks) to your app.js file
24
+ - Adds [Backpex Hooks](installation.html#backpex-hooks) to your app.js or app.ts file (auto-detected)
25
25
- Installs [daisyUI](installation.html#daisyui) via npm (with your permission)
26
26
- Sets up the [formatter configuration](installation.html#setup-formatter)
27
27
- Adds [Backpex files to Tailwind content](installation.html#add-files-to-tailwind-content)
@@ -38,7 +38,7 @@ defmodule Mix.Tasks.Backpex.Install.Docs do
38
38
39
39
## Options
40
40
41
- * `--app-js-path` - Path to your app.js file (default: "assets/js/app.js" )
41
+ * `--app-js-path` - Path to your app.js or app.ts file (auto-detected by default )
42
42
* `--app-css-path` - Path to your app.css file (default: "assets/css/app.css")
43
43
* `--no-layout` - Skip generating the admin layout
44
44
"""
@@ -63,7 +63,6 @@ if Code.ensure_loaded?(Igniter) do
63
63
alias Igniter.Util.Warning
64
64
alias IgniterJs.Parsers.Javascript.Parser
65
65
66
- @ default_app_js_path Path . join ( [ "assets" , "js" , "app.js" ] )
67
66
@ default_app_css_path Path . join ( [ "assets" , "css" , "app.css" ] )
68
67
@ hooks "...BackpexHooks"
69
68
@ imports "import { Hooks as BackpexHooks } from 'backpex'"
@@ -75,7 +74,7 @@ if Code.ensure_loaded?(Igniter) do
75
74
adds_deps: [ igniter_js: "~> 0.4" ] ,
76
75
example: __MODULE__ . Docs . example ( ) ,
77
76
schema: [ app_js_path: :string , app_css_path: :string , no_layout: :boolean ] ,
78
- defaults: [ app_js_path: @ default_app_js_path , app_css_path: @ default_app_css_path , no_layout: false ]
77
+ defaults: [ app_js_path: nil , app_css_path: @ default_app_css_path , no_layout: false ]
79
78
}
80
79
end
81
80
@@ -133,15 +132,36 @@ if Code.ensure_loaded?(Igniter) do
133
132
# Backpex hooks
134
133
135
134
defp install_backpex_hooks ( igniter ) do
136
- app_js_path = igniter . args . options [ :app_js_path ]
135
+ app_js_path = igniter . args . options [ :app_js_path ] || detect_app_file_path ( igniter )
137
136
138
137
with { :ok , content } <- IgniterJs.Helpers . read_and_validate_file ( app_js_path ) ,
139
138
{ :ok , _fun , content } <- Parser . insert_imports ( content , @ imports , :content ) ,
140
139
{ :ok , _fun , content } <- Parser . extend_hook_object ( content , @ hooks , :content ) do
141
140
Igniter . create_new_file ( igniter , app_js_path , content , on_exists: :overwrite )
142
141
else
143
- { :error , _fun , error } -> Mix . raise ( "Failed to modify app.js: #{ error } " )
144
- { :error , error } -> Mix . raise ( "Could not read app.js: #{ error } " )
142
+ { :error , _fun , error } -> Mix . raise ( "Failed to modify app file: #{ error } " )
143
+ { :error , error } -> Mix . raise ( "Could not read app file: #{ error } " )
144
+ end
145
+ end
146
+
147
+ # Auto-detect app.js or app.ts file
148
+
149
+ defp detect_app_file_path ( igniter ) do
150
+ js_path = Path . join ( [ "assets" , "js" , "app.js" ] )
151
+ ts_path = Path . join ( [ "assets" , "js" , "app.ts" ] )
152
+
153
+ cond do
154
+ Igniter . exists? ( igniter , ts_path ) ->
155
+ Mix . shell ( ) . info ( "Detected TypeScript app file: #{ ts_path } " )
156
+ ts_path
157
+
158
+ Igniter . exists? ( igniter , js_path ) ->
159
+ Mix . shell ( ) . info ( "Detected JavaScript app file: #{ js_path } " )
160
+ js_path
161
+
162
+ true ->
163
+ Mix . shell ( ) . info ( "No app.js or app.ts found, defaulting to: #{ js_path } " )
164
+ js_path
145
165
end
146
166
end
147
167
0 commit comments