Packages
live_svelte
0.1.0-rc3
0.18.0
0.18.0-rc0
0.17.4
0.17.3
0.17.2
0.17.1
0.17.0
0.16.0
0.15.0
0.15.0-rc.6
0.15.0-rc.5
0.15.0-rc.4
0.15.0-rc.3
0.15.0-rc.2
0.15.0-rc.1
0.14.1
0.14.0
0.13.3
0.13.3-rc.1
0.13.2
0.13.1
0.13.0
0.12.0
0.11.0
0.10.2
0.10.1
0.10.0
0.9.0
0.9.0-rc2
0.9.0-rc1
0.9.0-rc0
0.8.0
0.7.1
0.7.0
0.6.0
0.5.1
0.5.0
0.5.0-rc0
0.4.2
0.4.2-rc0
0.4.1
0.4.1-rc0
0.4.0
0.3.5
0.3.5-rc1
0.3.5-rc0
0.3.4
0.3.3
0.3.2
0.3.1
0.3.0
0.3.0-rc1
0.3.0-rc0
0.2.0
0.2.0-rc1
0.2.0-rc0
0.1.0
0.1.0-rc5
0.1.0-rc4
0.1.0-rc3
0.1.0-rc2
0.1.0-rc1
0.1.0-rc0
E2E reactivity for Svelte and LiveView
Current section
Files
Jump to
Current section
Files
lib/mix/tasks/configure_phoenix.ex
defmodule Mix.Tasks.LiveSvelte.ConfigurePhoenix do
@moduledoc """
Configures any necessary code changes inside Phoenix to make LiveSvelte work.
"""
import LiveSvelte.Logger
@watcher_regex ~r/watchers:\s\[(?!\s+node:)/
@esbuild_regex ~r/(?<!# )esbuild: {.*}/
@nodejs_regex ~r/children\s+=\s+\[(?!\s+\{NodeJS)/
def run(_) do
log_info("-- Configuring Phoenix...")
try do
configure_config()
configure_application()
rescue
err -> log_error(err.message)
end
Mix.Task.run("format")
end
defp configure_config() do
config_file = "dev.exs"
config_path = find_file("config/#{config_file}", config_file)
watcher = ~s"""
node: ["build.js", "--watch", cd: Path.expand("../assets", __DIR__)],\
"""
File.read!(config_path)
|> insert(@watcher_regex, watcher, "'#{watcher}' in #{config_file}")
|> comment(@esbuild_regex, "old esbuild watcher in #{config_file}")
|> save(config_path)
end
defp configure_application() do
application_file = "application.ex"
application_path = find_file("lib/**/#{application_file}", application_file)
nodeSupervisor = ~s"""
{NodeJS.Supervisor, [path: "#{File.cwd!()}/assets", pool_size: 4]}, \
"""
File.read!(application_path)
|> insert(@nodejs_regex, nodeSupervisor, "'#{nodeSupervisor}' in #{application_file}")
|> save(application_path)
end
defp find_file(wildcard, file_name) do
with [path] <- Path.wildcard(wildcard) do
path
else
[] -> raise "Could not find #{file_name}"
[_ | _] -> raise "Found multiple #{file_name} files"
end
end
defp insert(source, regex, to_insert, name) do
case Regex.run(regex, source, return: :index) do
[{pos, len}] ->
log_success("Inserted #{name}")
insert_position(source, pos + len, to_insert)
nil ->
log_error("Could not insert #{name}, please do so yourself")
source
end
end
defp comment(source, regex, name) do
case Regex.run(regex, source, return: :index) do
[{pos, _len}] ->
log_success("Commented out #{name}")
insert_position(source, pos, "# ")
nil ->
log_warning("Could not comment out #{name}")
source
end
end
defp insert_position(source, position, to_insert) do
{head, tail} = String.split_at(source, position)
head <> to_insert <> tail
end
defp save(source, target_file), do: File.write!(target_file, source)
end