Skip to content

Commit 9b5edc7

Browse files
authored
Merge pull request #1531 from naymspace/feature/installer-auto-detect-ts-js-file
Auto-detect app.js or app.ts in `backpex.install` task
2 parents a2dcc9d + a49cda7 commit 9b5edc7

File tree

1 file changed

+27
-7
lines changed

1 file changed

+27
-7
lines changed

lib/mix/tasks/backpex.install.ex

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ defmodule Mix.Tasks.Backpex.Install.Docs do
2121
## What this installer does:
2222
2323
- 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)
2525
- Installs [daisyUI](installation.html#daisyui) via npm (with your permission)
2626
- Sets up the [formatter configuration](installation.html#setup-formatter)
2727
- Adds [Backpex files to Tailwind content](installation.html#add-files-to-tailwind-content)
@@ -38,7 +38,7 @@ defmodule Mix.Tasks.Backpex.Install.Docs do
3838
3939
## Options
4040
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)
4242
* `--app-css-path` - Path to your app.css file (default: "assets/css/app.css")
4343
* `--no-layout` - Skip generating the admin layout
4444
"""
@@ -63,7 +63,6 @@ if Code.ensure_loaded?(Igniter) do
6363
alias Igniter.Util.Warning
6464
alias IgniterJs.Parsers.Javascript.Parser
6565

66-
@default_app_js_path Path.join(["assets", "js", "app.js"])
6766
@default_app_css_path Path.join(["assets", "css", "app.css"])
6867
@hooks "...BackpexHooks"
6968
@imports "import { Hooks as BackpexHooks } from 'backpex'"
@@ -75,7 +74,7 @@ if Code.ensure_loaded?(Igniter) do
7574
adds_deps: [igniter_js: "~> 0.4"],
7675
example: __MODULE__.Docs.example(),
7776
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]
7978
}
8079
end
8180

@@ -133,15 +132,36 @@ if Code.ensure_loaded?(Igniter) do
133132
# Backpex hooks
134133

135134
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)
137136

138137
with {:ok, content} <- IgniterJs.Helpers.read_and_validate_file(app_js_path),
139138
{:ok, _fun, content} <- Parser.insert_imports(content, @imports, :content),
140139
{:ok, _fun, content} <- Parser.extend_hook_object(content, @hooks, :content) do
141140
Igniter.create_new_file(igniter, app_js_path, content, on_exists: :overwrite)
142141
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
145165
end
146166
end
147167

0 commit comments

Comments
 (0)